@@ -10,6 +10,8 @@ import React, {
1010} from 'react'
1111import toast from 'react-hot-toast'
1212
13+ const MAIA_LOADING_TOAST_DELAY_MS = 800
14+
1315export const MaiaEngineContext = React . createContext < MaiaEngine > ( {
1416 maia : undefined ,
1517 status : 'loading' ,
@@ -28,6 +30,7 @@ export const MaiaEngineContextProvider: React.FC<{ children: ReactNode }> = ({
2830 const [ progress , setProgress ] = useState ( 0 )
2931 const [ error , setError ] = useState < string | null > ( null )
3032 const toastId = useRef < string | null > ( null )
33+ const loadingToastTimerRef = useRef < number | null > ( null )
3134
3235 const maia = useMemo ( ( ) => {
3336 const model = new Maia ( {
@@ -63,33 +66,77 @@ export const MaiaEngineContextProvider: React.FC<{ children: ReactNode }> = ({
6366 // Toast notifications for Maia model status
6467 useEffect ( ( ) => {
6568 return ( ) => {
66- toast . dismiss ( )
69+ if (
70+ loadingToastTimerRef . current !== null &&
71+ typeof window !== 'undefined'
72+ ) {
73+ window . clearTimeout ( loadingToastTimerRef . current )
74+ loadingToastTimerRef . current = null
75+ }
76+ if ( toastId . current ) {
77+ toast . dismiss ( toastId . current )
78+ toastId . current = null
79+ }
6780 }
6881 } , [ ] )
6982
7083 useEffect ( ( ) => {
71- if ( status === 'loading' && ! toastId . current ) {
72- toastId . current = toast . loading ( 'Loading Maia Model...' )
73- } else if ( status === 'ready' ) {
84+ if ( status === 'loading' ) {
85+ if (
86+ typeof window === 'undefined' ||
87+ toastId . current ||
88+ loadingToastTimerRef . current !== null
89+ ) {
90+ return
91+ }
92+
93+ loadingToastTimerRef . current = window . setTimeout ( ( ) => {
94+ loadingToastTimerRef . current = null
95+ if ( ! toastId . current ) {
96+ toastId . current = toast . loading ( 'Loading Maia...' )
97+ }
98+ } , MAIA_LOADING_TOAST_DELAY_MS )
99+ return
100+ }
101+
102+ if (
103+ loadingToastTimerRef . current !== null &&
104+ typeof window !== 'undefined'
105+ ) {
106+ window . clearTimeout ( loadingToastTimerRef . current )
107+ loadingToastTimerRef . current = null
108+ }
109+
110+ if ( status === 'no-cache' || status === 'downloading' ) {
111+ if ( toastId . current ) {
112+ toast . dismiss ( toastId . current )
113+ toastId . current = null
114+ }
115+ return
116+ }
117+
118+ if ( status === 'ready' ) {
119+ // Only show success if a loading toast was visible.
74120 if ( toastId . current ) {
75121 toast . success ( 'Loaded Maia! Analysis is ready' , {
76122 id : toastId . current ,
77123 } )
78124 toastId . current = null
79- } else {
80- toast . success ( 'Loaded Maia! Analysis is ready' )
81125 }
82126 } else if ( status === 'error' ) {
127+ const message = error
128+ ? `Failed to load Maia model: ${ error } `
129+ : 'Failed to load Maia model'
83130 if ( toastId . current ) {
84- toast . error ( 'Failed to load Maia model' , {
131+ toast . error ( message , {
85132 id : toastId . current ,
86133 } )
87134 toastId . current = null
88135 } else {
89- toast . error ( 'Failed to load Maia model' )
136+ toast . error ( message )
90137 }
91138 }
92- } , [ status ] )
139+ } , [ status , error ] )
93140
94141 return (
95142 < MaiaEngineContext . Provider
0 commit comments