| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | import React, { useCallback, useEffect, useState } from 'react' |
| 2 | -import { createContextHook } from '../utils/hooks' | |
| 2 | +import { createContextHook } from '../utils/bootstrap' | |
| 3 | 3 | import { isNetworkAdmin } from '../utils/screen' |
| 4 | -import { useRestAPI } from './useRestAPI' | |
| 4 | +import { parseSnippetObject } from '../utils/snippets/objects' | |
| 5 | +import { useSnippetsAPI } from './useSnippetsAPI' | |
| 5 | 6 | import type { PropsWithChildren } from 'react' |
| 6 | 7 | import type { Snippet } from '../types/Snippet' |
| 7 | 8 | |
| 8 | 9 | export interface SnippetsListContext { |
| @@ -9,13 +10,15 @@ | ||
| 9 | 10 | snippetsList: readonly Snippet[] | undefined |
| 10 | 11 | refreshSnippetsList: () => Promise<void> |
| 11 | 12 | } |
| 12 | 13 | |
| 13 | -const [SnippetsListContext, useSnippetsList] = createContextHook<SnippetsListContext>('SnippetsList') | |
| 14 | +const [Context, useSnippetsList] = createContextHook<SnippetsListContext>('useSnippetsList') | |
| 14 | 15 | |
| 15 | 16 | export const WithSnippetsListContext: React.FC<PropsWithChildren> = ({ children }) => { |
| 16 | - const { snippetsAPI: { fetchAll } } = useRestAPI() | |
| 17 | - const [snippetsList, setSnippetsList] = useState<Snippet[]>() | |
| 17 | + const { fetchAll } = useSnippetsAPI() | |
| 18 | + const [snippetsList, setSnippetsList] = useState<Snippet[] | undefined>( | |
| 19 | + () => window.CODE_SNIPPETS_MANAGE?.snippetsList?.map(parseSnippetObject) | |
| 20 | + ) | |
| 18 | 21 | |
| 19 | 22 | const refreshSnippetsList = useCallback(async (): Promise<void> => { |
| 20 | 23 | try { |
| 21 | 24 | console.info('Fetching snippets list') |
| @@ -35,8 +38,8 @@ | ||
| 35 | 38 | snippetsList, |
| 36 | 39 | refreshSnippetsList |
| 37 | 40 | } |
| 38 | 41 | |
| 39 | - return <SnippetsListContext.Provider value={value}>{children}</SnippetsListContext.Provider> | |
| 42 | + return <Context.Provider value={value}>{children}</Context.Provider> | |
| 40 | 43 | } |
| 41 | 44 | |
| 42 | 45 | export { useSnippetsList } |