11import { Flexbox , Highlighter , Hotkey , Segmented , Snippet } from '@lobehub/ui' ;
22import { BentoCard , BentoGrid } from '@lobehub/ui/awesome' ;
33import { useTheme } from 'next-themes' ;
4- import { useEffect , useState } from 'react' ;
4+ import { useEffect , useState , useSyncExternalStore } from 'react' ;
55
66import DemoPreview from './DemoPreview.mdx' ;
77import { renderLink } from './renderLink' ;
@@ -12,10 +12,16 @@ description: Button triggers an action.
1212category: General
1313---` ;
1414
15+ const subscribeToNothing = ( ) => ( ) => { } ;
16+
1517function ThemeSwitchTile ( ) {
1618 const { setTheme, theme } = useTheme ( ) ;
17- const [ mounted , setMounted ] = useState ( false ) ;
18- useEffect ( ( ) => setMounted ( true ) , [ ] ) ;
19+ // false while prerendering and hydrating, true afterwards: the stored theme is only known in the browser
20+ const mounted = useSyncExternalStore (
21+ subscribeToNothing ,
22+ ( ) => true ,
23+ ( ) => false ,
24+ ) ;
1925 const preference = mounted && ( theme === 'light' || theme === 'dark' ) ? theme : 'system' ;
2026
2127 return (
@@ -35,18 +41,16 @@ function LlmsTxtTile() {
3541 const [ excerpt , setExcerpt ] = useState ( 'Loading /llms.txt…' ) ;
3642
3743 useEffect ( ( ) => {
38- let cancelled = false ;
39- fetch ( '/llms.txt' )
44+ const controller = new AbortController ( ) ;
45+ fetch ( '/llms.txt' , { signal : controller . signal } )
4046 . then ( ( response ) => ( response . ok ? response . text ( ) : Promise . reject ( response . status ) ) )
41- . then ( ( text ) => {
42- if ( ! cancelled ) setExcerpt ( text . split ( '\n' ) . slice ( 0 , 7 ) . join ( '\n' ) . trim ( ) ) ;
43- } )
47+ . then ( ( text ) => setExcerpt ( text . split ( '\n' ) . slice ( 0 , 7 ) . join ( '\n' ) . trim ( ) ) )
4448 . catch ( ( ) => {
45- if ( ! cancelled ) setExcerpt ( 'Open /llms.txt to read the index of this site.' ) ;
49+ if ( ! controller . signal . aborted ) {
50+ setExcerpt ( 'Open /llms.txt to read the index of this site.' ) ;
51+ }
4652 } ) ;
47- return ( ) => {
48- cancelled = true ;
49- } ;
53+ return ( ) => controller . abort ( ) ;
5054 } , [ ] ) ;
5155
5256 return (
0 commit comments