()
| 15 | }); |
| 16 | |
| 17 | export function NewSession() { |
| 18 | const navigate = useNavigate(); |
| 19 | const location = useLocation(); |
| 20 | const toast = useToast(); |
| 21 | const hasStartedRef = useRef(false); |
| 22 | |
| 23 | const state = useMemo(() => { |
| 24 | const parsed = newSessionStateSchema.safeParse(location.state); |
| 25 | return parsed.success ? parsed.data : null; |
| 26 | }, [location.state]) |
| 27 | |
| 28 | // Guard: if navigated here directly without state, go home |
| 29 | useEffect(() => { |
| 30 | if (!state) { |
| 31 | navigate("/", { replace: true }); |
| 32 | } |
| 33 | }, [state, navigate]); |
| 34 | |
| 35 | // Create the session on mount — this screen exists to do this |
| 36 | useEffect(() => { |
| 37 | if (!state || hasStartedRef.current) return; |
| 38 | |
| 39 | hasStartedRef.current = true; |
| 40 | |
| 41 | let ignore = false; |
| 42 | const createSession = async () => { |
| 43 | try { |
| 44 | const res = await apiClient.sessions.$post({ |
| 45 | json: { |
| 46 | title: state.message.slice(0, 100), |
| 47 | }, |
| 48 | }); |
| 49 | |
| 50 | if (ignore) return; |
| 51 | if (!res.ok) { |
| 52 | throw new Error(await getErrorMessage(res)); |
| 53 | } |
| 54 | const session = await res.json(); |
| 55 | navigate( |
| 56 | `/sessions/${session.id}`, |
| 57 | { replace: true, state: { session, initialPrompt: state } } |
| 58 | ); |
| 59 | } catch (error) { |
| 60 | if (ignore) return; |
| 61 | toast.show({ |
| 62 | variant: "error", |
| 63 | message: error instanceof Error ? error.message : "Failed to create session", |
| 64 | }); |
| 65 | navigate("/", { replace: true }); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | createSession(); |
| 70 | return () => { |
| 71 | ignore = true; |
| 72 | }; |
| 73 | }, [state, navigate, toast]); |
| 74 |
nothing calls this directly
no test coverage detected