|
1 | | -import React, {type ReactNode} from 'react'; |
| 1 | +import React, {useEffect, useState, type ReactNode} from 'react'; |
2 | 2 | import {AskAIWidget} from '@0gfoundation/ask-ai-widget'; |
3 | 3 | import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; |
4 | 4 | import '@0gfoundation/ask-ai-widget/styles.css'; |
5 | 5 |
|
| 6 | +type SiteTheme = 'light' | 'dark' | 'auto'; |
| 7 | + |
| 8 | +// Docusaurus writes the chosen colour mode to <html data-theme="...">. Root |
| 9 | +// sits above the theme's providers, so useColorMode() isn't available here; |
| 10 | +// watching the attribute is what keeps the widget in step with the site's |
| 11 | +// toggle rather than the OS setting. |
| 12 | +function useSiteTheme(): SiteTheme { |
| 13 | + const [theme, setTheme] = useState<SiteTheme>('auto'); |
| 14 | + useEffect(() => { |
| 15 | + const root = document.documentElement; |
| 16 | + const read = () => { |
| 17 | + const value = root.getAttribute('data-theme'); |
| 18 | + setTheme(value === 'dark' || value === 'light' ? value : 'auto'); |
| 19 | + }; |
| 20 | + read(); |
| 21 | + const observer = new MutationObserver(read); |
| 22 | + observer.observe(root, {attributes: true, attributeFilter: ['data-theme']}); |
| 23 | + return () => observer.disconnect(); |
| 24 | + }, []); |
| 25 | + return theme; |
| 26 | +} |
| 27 | + |
6 | 28 | // Docusaurus Root wrapper — renders once around every page, so each page gets |
7 | 29 | // the floating "Ask AI" chat button (bottom-right). The widget talks to the |
8 | 30 | // Ask Zed chat backend at 0g.ai/zed (0G Compute, same RAG knowledge index |
9 | 31 | // as build.0g.ai/ask); docs.0g.ai is in that backend's CORS allowlist. |
10 | 32 | export default function Root({children}: {children: ReactNode}): ReactNode { |
11 | 33 | const {siteConfig} = useDocusaurusContext(); |
12 | 34 | const turnstileSiteKey = siteConfig.customFields?.turnstileSiteKey as string; |
| 35 | + const theme = useSiteTheme(); |
13 | 36 | return ( |
14 | 37 | <> |
15 | 38 | {children} |
16 | 39 | <AskAIWidget |
17 | 40 | apiUrl="https://0g.ai/zed/api/chat" |
18 | 41 | turnstileSiteKey={turnstileSiteKey} |
| 42 | + theme={theme} |
19 | 43 | /> |
20 | 44 | </> |
21 | 45 | ); |
|
0 commit comments