| 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 |
|