PluginProbe
Code Snippets / 4.0.0-beta.2
Code Snippets v4.0.0-beta.2
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 / components / common / cloud / WithCloudSnippetDownloadsContext.tsx

WithCloudSnippetDownloadsContext.tsx in Code Snippets 4.0.0-beta.2, at js/components/common/cloud/WithCloudSnippetDownloadsContext.tsx

45 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, useState } from 'react'
2 import { createContextHook } from '../../../utils/bootstrap'
3 import type { PropsWithChildren } from 'react'
4 import type { CloudSnippetSchema } from '../../../types/schema/CloudSnippetSchema'
5
6 export interface CloudSnippetDownloadRecord {
7 isDownloading: boolean
8 localId?: number
9 }
10
11 interface CloudSnippetDownloadsContext {
12 downloadRecords: Partial<Record<CloudSnippetSchema['id'], CloudSnippetDownloadRecord>>
13 updateDownloadRecord: (
14 snippetId: CloudSnippetSchema['id'],
15 update: Partial<CloudSnippetDownloadRecord>
16 ) => void
17 }
18
19 const [Context, useCloudSnippetDownloads] =
20 createContextHook<CloudSnippetDownloadsContext>('useCloudSnippetDownloads')
21
22 export const WithCloudSnippetDownloadsContext: React.FC<PropsWithChildren> = ({ children }) => {
23 const [downloadRecords, setDownloadRecords] =
24 useState<CloudSnippetDownloadsContext['downloadRecords']>({})
25 const updateDownloadRecord = useCallback<CloudSnippetDownloadsContext['updateDownloadRecord']>(
26 (snippetId, update) => setDownloadRecords(previous => ({
27 ...previous,
28 [snippetId]: {
29 isDownloading: false,
30 ...previous[snippetId],
31 ...update
32 }
33 })),
34 []
35 )
36
37 return (
38 <Context.Provider value={{ downloadRecords, updateDownloadRecord }}>
39 {children}
40 </Context.Provider>
41 )
42 }
43
44 export { useCloudSnippetDownloads }
45