@@ -11,6 +11,49 @@ import { StockfishStatus, StockfishEngine } from 'src/types'
1111import Engine from 'src/lib/engine/stockfish'
1212
1313const STOCKFISH_LOADING_TOAST_DELAY_MS = 800
14+ const STOCKFISH_DEBUG_LOADING_KEY = 'maia.stockfishDebugLoading'
15+
16+ const isTruthy = ( value : string | null | undefined ) : boolean => {
17+ if ( ! value ) return false
18+ return [ '1' , 'true' , 'yes' , 'on' ] . includes ( value . toLowerCase ( ) )
19+ }
20+
21+ const isStockfishDebugLoadingEnabled = ( ) : boolean => {
22+ if ( typeof window !== 'undefined' ) {
23+ try {
24+ const localValue = window . localStorage . getItem ( STOCKFISH_DEBUG_LOADING_KEY )
25+ if ( localValue !== null ) return isTruthy ( localValue )
26+ } catch {
27+ // ignore localStorage access failures
28+ }
29+ }
30+
31+ return isTruthy ( process . env . NEXT_PUBLIC_STOCKFISH_DEBUG_LOADING )
32+ }
33+
34+ const getStockfishLoadingLabel = (
35+ engine : Engine | null ,
36+ debugLoadingEnabled : boolean ,
37+ ) : string => {
38+ if ( ! debugLoadingEnabled ) {
39+ return 'Loading Stockfish...'
40+ }
41+
42+ if ( ! engine ) return 'Loading Stockfish...'
43+
44+ switch ( engine . initializationPhase ) {
45+ case 'checking-cache' :
46+ return 'Checking local Stockfish cache...'
47+ case 'downloading-nnue' :
48+ return 'Downloading Stockfish model weights...'
49+ case 'loading-nnue' :
50+ return 'Loading Stockfish from local cache...'
51+ case 'loading-module' :
52+ return 'Starting Stockfish engine...'
53+ default :
54+ return 'Loading Stockfish...'
55+ }
56+ }
1457
1558let sharedClientStockfishEngine : Engine | null = null
1659
@@ -63,6 +106,12 @@ export const StockfishEngineContextProvider: React.FC<{
63106 const [ error , setError ] = useState < string | null > (
64107 ( ) => engineRef . current ?. initializationError ?? null ,
65108 )
109+ const [ debugLoadingEnabled ] = useState < boolean > ( ( ) =>
110+ isStockfishDebugLoadingEnabled ( ) ,
111+ )
112+ const [ loadingLabel , setLoadingLabel ] = useState < string > ( ( ) =>
113+ getStockfishLoadingLabel ( engineRef . current , debugLoadingEnabled ) ,
114+ )
66115 const toastId = useRef < string | null > ( null )
67116 const loadingToastTimerRef = useRef < number | null > ( null )
68117
@@ -90,6 +139,11 @@ export const StockfishEngineContextProvider: React.FC<{
90139 const engine = engineRef . current
91140 if ( ! engine ) return
92141
142+ setLoadingLabel ( ( prev ) => {
143+ const next = getStockfishLoadingLabel ( engine , debugLoadingEnabled )
144+ return prev === next ? prev : next
145+ } )
146+
93147 if ( engine . initializationError ) {
94148 setStatus ( 'error' )
95149 setError ( engine . initializationError )
@@ -105,7 +159,7 @@ export const StockfishEngineContextProvider: React.FC<{
105159 const interval = setInterval ( checkEngineStatus , 100 )
106160
107161 return ( ) => clearInterval ( interval )
108- } , [ ] )
162+ } , [ debugLoadingEnabled ] )
109163
110164 // Toast notifications for Stockfish engine status
111165 useEffect ( ( ) => {
@@ -123,18 +177,25 @@ export const StockfishEngineContextProvider: React.FC<{
123177
124178 useEffect ( ( ) => {
125179 if ( status === 'loading' ) {
126- if (
127- toastId . current ||
128- loadingToastTimerRef . current !== null ||
129- typeof window === 'undefined'
130- ) {
180+ if ( typeof window === 'undefined' ) {
181+ return
182+ }
183+
184+ if ( toastId . current ) {
185+ toastId . current = toast . loading ( loadingLabel , { id : toastId . current } )
186+ return
187+ }
188+
189+ if ( loadingToastTimerRef . current !== null ) {
131190 return
132191 }
133192
134193 loadingToastTimerRef . current = window . setTimeout ( ( ) => {
135194 loadingToastTimerRef . current = null
136195 if ( ! toastId . current && engineRef . current && ! engineRef . current . ready ) {
137- toastId . current = toast . loading ( 'Loading Stockfish Engine...' )
196+ toastId . current = toast . loading (
197+ getStockfishLoadingLabel ( engineRef . current , debugLoadingEnabled ) ,
198+ )
138199 }
139200 } , STOCKFISH_LOADING_TOAST_DELAY_MS )
140201 return
@@ -166,7 +227,7 @@ export const StockfishEngineContextProvider: React.FC<{
166227 toast . error ( message )
167228 }
168229 }
169- } , [ status , error ] )
230+ } , [ status , error , loadingLabel , debugLoadingEnabled ] )
170231
171232 const contextValue = useMemo (
172233 ( ) => ( {
0 commit comments