| Server IP : 185.252.147.100 / Your IP : 216.73.217.33 Web Server : nginx/1.27.3 System : Linux mitrofanov.ru 6.1.0-37-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.140-1 (2025-05-22) x86_64 User : mitr ( 1000) PHP Version : 8.2.29 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /www/html/fast-franchise.ru/wordpress/wp-content/plugins/code-snippets/js/hooks/ |
Upload File : |
import React, { useCallback, useEffect, useState } from 'react'
import { createContextHook } from '../utils/hooks'
import { isNetworkAdmin } from '../utils/screen'
import { useRestAPI } from './useRestAPI'
import type { PropsWithChildren } from 'react'
import type { Snippet } from '../types/Snippet'
export interface SnippetsListContext {
snippetsList: readonly Snippet[] | undefined
refreshSnippetsList: () => Promise<void>
}
const [SnippetsListContext, useSnippetsList] = createContextHook<SnippetsListContext>('SnippetsList')
export const WithSnippetsListContext: React.FC<PropsWithChildren> = ({ children }) => {
const { snippetsAPI: { fetchAll } } = useRestAPI()
const [snippetsList, setSnippetsList] = useState<Snippet[]>()
const refreshSnippetsList = useCallback(async (): Promise<void> => {
try {
console.info('Fetching snippets list')
const response = await fetchAll(isNetworkAdmin())
setSnippetsList(response)
} catch (error: unknown) {
console.error('Error fetching snippets list', error)
}
}, [fetchAll])
useEffect(() => {
refreshSnippetsList()
.catch(() => undefined)
}, [refreshSnippetsList])
const value: SnippetsListContext = {
snippetsList,
refreshSnippetsList
}
return <SnippetsListContext.Provider value={value}>{children}</SnippetsListContext.Provider>
}
export { useSnippetsList }