1717from reflex .components .radix .themes .layout .flex import Flex
1818from reflex .components .radix .themes .typography .text import Text
1919from reflex .components .sonner .toast import Toaster , ToastProps
20+ from reflex .config import environment
2021from reflex .constants import Dirs , Hooks , Imports
2122from reflex .constants .compiler import CompileVars
2223from reflex .utils .imports import ImportVar
@@ -109,9 +110,41 @@ def add_hooks(self) -> list[str | Var]:
109110 id = toast_id ,
110111 ) # pyright: ignore [reportCallIssue]
111112
113+ if environment .REFLEX_DOES_BACKEND_COLD_START .get ():
114+ loading_message = Var .create ("Backend is starting." )
115+ backend_is_loading_toast_var = Var (
116+ f"toast.loading({ loading_message !s} , {{...toast_props, description: '', closeButton: false, onDismiss: () => setUserDismissed(true)}},)"
117+ )
118+ backend_is_not_responding = Var .create ("Backend is not responding." )
119+ backend_is_down_toast_var = Var (
120+ f"toast.error({ backend_is_not_responding !s} , {{...toast_props, description: '', onDismiss: () => setUserDismissed(true)}},)"
121+ )
122+ toast_var = Var (
123+ f"""
124+ if (waitedForBackend) {{
125+ { backend_is_down_toast_var !s}
126+ }} else {{
127+ { backend_is_loading_toast_var !s} ;
128+ }}
129+ setTimeout(() => {{
130+ if ({ has_too_many_connection_errors !s} ) {{
131+ setWaitedForBackend(true);
132+ }}
133+ }}, { environment .REFLEX_BACKEND_COLD_START_TIMEOUT .get () * 1000 } );
134+ """
135+ )
136+ else :
137+ loading_message = Var .create (
138+ f"Cannot connect to server: { connection_error } ."
139+ )
140+ toast_var = Var (
141+ f"toast.error({ loading_message !s} , {{...toast_props, onDismiss: () => setUserDismissed(true)}},)"
142+ )
143+
112144 individual_hooks = [
113145 f"const toast_props = { LiteralVar .create (props )!s} ;" ,
114146 "const [userDismissed, setUserDismissed] = useState(false);" ,
147+ "const [waitedForBackend, setWaitedForBackend] = useState(false);" ,
115148 FunctionStringVar (
116149 "useEffect" ,
117150 _var_data = VarData (
@@ -127,10 +160,7 @@ def add_hooks(self) -> list[str | Var]:
127160() => {{
128161 if ({ has_too_many_connection_errors !s} ) {{
129162 if (!userDismissed) {{
130- toast.error(
131- `Cannot connect to server: ${{{ connection_error } }}.`,
132- {{...toast_props, onDismiss: () => setUserDismissed(true)}},
133- )
163+ { toast_var !s}
134164 }}
135165 }} else {{
136166 toast.dismiss("{ toast_id } ");
@@ -139,7 +169,7 @@ def add_hooks(self) -> list[str | Var]:
139169}}
140170"""
141171 ),
142- LiteralArrayVar .create ([connect_errors ]),
172+ LiteralArrayVar .create ([connect_errors , Var ( "waitedForBackend" ) ]),
143173 ),
144174 ]
145175
0 commit comments