import React, { useCallback, useState } from 'react' import { createContextHook } from '../../../utils/bootstrap' import type { PropsWithChildren } from 'react' import type { CloudSnippetSchema } from '../../../types/schema/CloudSnippetSchema' export interface CloudSnippetDownloadRecord { isDownloading: boolean localId?: number } interface CloudSnippetDownloadsContext { downloadRecords: Partial> updateDownloadRecord: ( snippetId: CloudSnippetSchema['id'], update: Partial ) => void } const [Context, useCloudSnippetDownloads] = createContextHook('useCloudSnippetDownloads') export const WithCloudSnippetDownloadsContext: React.FC = ({ children }) => { const [downloadRecords, setDownloadRecords] = useState({}) const updateDownloadRecord = useCallback( (snippetId, update) => setDownloadRecords(previous => ({ ...previous, [snippetId]: { isDownloading: false, ...previous[snippetId], ...update } })), [] ) return ( {children} ) } export { useCloudSnippetDownloads }