PluginProbe
Code Snippets / 3.9.6
Code Snippets v3.9.6
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / js / hooks / useSnippetsList.tsx

useSnippetsList.tsx in Code Snippets 3.9.6, at js/hooks/useSnippetsList.tsx

43 lines 1.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import React, { useCallback, useEffect, useState } from 'react'
2 import { createContextHook } from '../utils/hooks'
3 import { isNetworkAdmin } from '../utils/screen'
4 import { useRestAPI } from './useRestAPI'
5 import type { PropsWithChildren } from 'react'
6 import type { Snippet } from '../types/Snippet'
7
8 export interface SnippetsListContext {
9 snippetsList: readonly Snippet[] | undefined
10 refreshSnippetsList: () => Promise<void>
11 }
12
13 const [SnippetsListContext, useSnippetsList] = createContextHook<SnippetsListContext>('SnippetsList')
14
15 export const WithSnippetsListContext: React.FC<PropsWithChildren> = ({ children }) => {
16 const { snippetsAPI: { fetchAll } } = useRestAPI()
17 const [snippetsList, setSnippetsList] = useState<Snippet[]>()
18
19 const refreshSnippetsList = useCallback(async (): Promise<void> => {
20 try {
21 console.info('Fetching snippets list')
22 const response = await fetchAll(isNetworkAdmin())
23 setSnippetsList(response)
24 } catch (error: unknown) {
25 console.error('Error fetching snippets list', error)
26 }
27 }, [fetchAll])
28
29 useEffect(() => {
30 refreshSnippetsList()
31 .catch(() => undefined)
32 }, [refreshSnippetsList])
33
34 const value: SnippetsListContext = {
35 snippetsList,
36 refreshSnippetsList
37 }
38
39 return <SnippetsListContext.Provider value={value}>{children}</SnippetsListContext.Provider>
40 }
41
42 export { useSnippetsList }
43