PluginProbe
Code Snippets / 3.10.2
Code Snippets v3.10.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 / hooks / useSnippetView.ts

useSnippetView.ts in Code Snippets 3.10.2, at js/hooks/useSnippetView.ts

35 lines 1.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useCallback, useState } from 'react'
2 import { DEFAULT_SNIPPET_VIEW } from '../types/SnippetView'
3 import { handleUnknownError } from '../utils/errors'
4 import { REST_BASES } from '../utils/restAPI'
5 import { useRestAPI } from './useRestAPI'
6 import type { SnippetView } from '../types/SnippetView'
7
8 export interface UseSnippetView {
9 snippetView: SnippetView
10 setSnippetView: (view: SnippetView) => void
11 }
12
13 /**
14 * Plugin-wide snippet view preference (cards or table).
15 *
16 * The initial value is localized into the page, and updates are persisted
17 * through the preferences REST endpoint so the choice survives page loads
18 * everywhere snippet lists are displayed. The UI updates optimistically
19 * rather than waiting for the save to complete.
20 */
21 export const useSnippetView = (): UseSnippetView => {
22 const { api } = useRestAPI()
23 const [snippetView, setViewState] = useState<SnippetView>(
24 () => window.CODE_SNIPPETS?.snippetView ?? DEFAULT_SNIPPET_VIEW
25 )
26
27 const setSnippetView = useCallback((view: SnippetView) => {
28 setViewState(view)
29 api.post<{ view: SnippetView }, { view: SnippetView }>(`${REST_BASES.preferences}/snippet-view`, { view })
30 .catch(handleUnknownError)
31 }, [api])
32
33 return { snippetView, setSnippetView }
34 }
35