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 / demo / useDemoSeen.ts

useDemoSeen.ts in Code Snippets 4.0.0-beta.2, at js/components/common/demo/useDemoSeen.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, useRef } from 'react'
2 import { useRestAPI } from '../../../hooks/useRestAPI'
3 import { handleUnknownError } from '../../../utils/errors'
4 import { REST_BASES } from '../../../utils/restAPI'
5 import type { SubpageName } from '../../ManageMenu/subpages'
6
7 export type DemoName = 'ai-agent' | 'blueprints'
8
9 // Widened to any subpage so the toolbar can ask about a tab without first
10 // narrowing it: a tab with no recorded demo simply never appears in the list.
11 export const hasSeenDemo = (demo: SubpageName): boolean =>
12 window.CODE_SNIPPETS?.demosSeen?.includes(demo) ?? false
13
14 /**
15 * Records that a feature demo has been watched through to the end, so its
16 * toolbar tab stops advertising itself as new.
17 *
18 * The walkthrough can be replayed, so the recording is made at most once per
19 * page load and is a no-op when the demo has already been seen.
20 */
21 export const useMarkDemoSeen = (demo: DemoName): VoidFunction => {
22 const { api } = useRestAPI()
23 const recorded = useRef(false)
24
25 return useCallback(() => {
26 if (recorded.current || hasSeenDemo(demo)) {
27 return
28 }
29
30 recorded.current = true
31 api.post<{ demos: DemoName[] }, { demo: DemoName }>(REST_BASES.preferences.demosSeen, { demo })
32 .catch(handleUnknownError)
33 }, [api, demo])
34 }
35