From 8379522e4cbd11d3ce09e86e644d0da71f342818 Mon Sep 17 00:00:00 2001 From: sisantaChhatoi Date: Thu, 3 Dec 2026 14:47:14 +0530 Subject: [PATCH] refactor(web): useReducer for chat, dashboard, shared browser and auth pages Converts the chat page, chat view and draft chat, the dashboard, the shared browser page, and the login and setup pages to typed reducers in _state/. The selected conversation and approval focus stay in the URL. The chat list poll (2 s) and the dashboard live (5 s) and system (15 s) polls return the same state when the data is unchanged. Part of #546. Co-Authored-By: Claude Opus 5.5 --- .../(auth)/login/_state/login-page.reducer.ts | 65 +++++ web/src/app/(auth)/login/page.tsx | 52 ++-- .../(auth)/setup/_state/setup-page.reducer.ts | 59 +++++ web/src/app/(auth)/setup/page.tsx | 39 ++- .../chat/_state/chat-page.reducer.test.mjs | 79 ++++++ .../(main)/chat/_state/chat-page.reducer.ts | 193 ++++++++++++++ .../chat/_state/chat-view.reducer.test.mjs | 54 ++++ .../(main)/chat/_state/chat-view.reducer.ts | 129 +++++++++ .../(main)/chat/_state/draft-chat.reducer.ts | 76 ++++++ web/src/app/(main)/chat/page.tsx | 249 +++++++----------- .../_state/dashboard-page.reducer.test.mjs | 25 ++ .../_state/dashboard-page.reducer.ts | 106 ++++++++ web/src/app/(main)/dashboard/page.tsx | 99 +++---- .../_state/shared-browser-page.reducer.ts | 59 +++++ web/src/app/(main)/function/browser/page.tsx | 53 ++-- 15 files changed, 1047 insertions(+), 290 deletions(-) create mode 100644 web/src/app/(auth)/login/_state/login-page.reducer.ts create mode 100644 web/src/app/(auth)/setup/_state/setup-page.reducer.ts create mode 100644 web/src/app/(main)/chat/_state/chat-page.reducer.test.mjs create mode 100644 web/src/app/(main)/chat/_state/chat-page.reducer.ts create mode 100644 web/src/app/(main)/chat/_state/chat-view.reducer.test.mjs create mode 100644 web/src/app/(main)/chat/_state/chat-view.reducer.ts create mode 100644 web/src/app/(main)/chat/_state/draft-chat.reducer.ts create mode 100644 web/src/app/(main)/dashboard/_state/dashboard-page.reducer.test.mjs create mode 100644 web/src/app/(main)/dashboard/_state/dashboard-page.reducer.ts create mode 100644 web/src/app/(main)/function/browser/_state/shared-browser-page.reducer.ts diff --git a/web/src/app/(auth)/login/_state/login-page.reducer.ts b/web/src/app/(auth)/login/_state/login-page.reducer.ts new file mode 100644 index 00000000..555c2f67 --- /dev/null +++ b/web/src/app/(auth)/login/_state/login-page.reducer.ts @@ -0,0 +1,65 @@ +import { mergePatch, setSlice } from "@/lib/reducers/slice"; + +export type LoginForm = { + password: string; + agreed: boolean; +}; + +export type LoginTerms = { + open: boolean; + readToEnd: boolean; +}; + +export type LoginStatus = { + checking: boolean; + submitting: boolean; + error: string; +}; + +export type LoginPageState = { + form: LoginForm; + terms: LoginTerms; + status: LoginStatus; +}; + +export type LoginPageAction = + | { type: "formChanged"; patch: Partial } + | { type: "termsChanged"; patch: Partial } + | { type: "termsAgreed" } + | { type: "statusCheckFailed"; error: string } + | { type: "statusChecked" } + | { type: "validationFailed"; error: string } + | { type: "submitStarted" } + | { type: "submitFailed"; error: string } + | { type: "submitFinished" }; + +export const initialLoginPageState: LoginPageState = { + form: { password: "", agreed: false }, + terms: { open: false, readToEnd: false }, + status: { checking: true, submitting: false, error: "" }, +}; + +function withStatus(state: LoginPageState, patch: Partial): LoginPageState { + return setSlice(state, "status", mergePatch(state.status, patch)); +} + +export function loginPageReducer(state: LoginPageState, action: LoginPageAction): LoginPageState { + switch (action.type) { + case "formChanged": + return setSlice(state, "form", mergePatch(state.form, action.patch)); + case "termsChanged": + return setSlice(state, "terms", mergePatch(state.terms, action.patch)); + case "termsAgreed": + return { ...state, form: { ...state.form, agreed: true }, status: { ...state.status, error: "" } }; + case "statusCheckFailed": + case "validationFailed": + case "submitFailed": + return withStatus(state, { error: action.error }); + case "statusChecked": + return withStatus(state, { checking: false }); + case "submitStarted": + return withStatus(state, { submitting: true, error: "" }); + case "submitFinished": + return withStatus(state, { submitting: false }); + } +} diff --git a/web/src/app/(auth)/login/page.tsx b/web/src/app/(auth)/login/page.tsx index a994fd4d..399ede01 100644 --- a/web/src/app/(auth)/login/page.tsx +++ b/web/src/app/(auth)/login/page.tsx @@ -1,6 +1,6 @@ "use client"; -import { useEffect, useRef, useState } from "react"; +import { useEffect, useReducer, useRef } from "react"; import { useRouter } from "next/navigation"; @@ -15,30 +15,30 @@ import { t } from "@/i18n"; import { api } from "@/lib/api"; import { auth } from "@/lib/auth"; +import { initialLoginPageState, loginPageReducer } from "./_state/login-page.reducer"; + export default function LoginPage() { const router = useRouter(); - const [password, setPassword] = useState(""); - const [error, setError] = useState(""); - const [loading, setLoading] = useState(false); - const [checking, setChecking] = useState(true); - const [agreed, setAgreed] = useState(false); - const [termsOpen, setTermsOpen] = useState(false); - const [readToEnd, setReadToEnd] = useState(false); + const [state, dispatch] = useReducer(loginPageReducer, initialLoginPageState); + const { password, agreed } = state.form; + const { open: termsOpen, readToEnd } = state.terms; + const { checking, submitting: loading, error } = state.status; const termsBodyRef = useRef(null); // Scroll to the bottom of the terms (including when everything fits without scrolling) to unlock "Agree". function handleTermsScroll() { const el = termsBodyRef.current; if (!el) return; - if (el.scrollTop + el.clientHeight >= el.scrollHeight - 8) setReadToEnd(true); + if (el.scrollTop + el.clientHeight >= el.scrollHeight - 8) + dispatch({ type: "termsChanged", patch: { readToEnd: true } }); } useEffect(() => { if (!termsOpen) return; // Reset on open, and cover the case where the content is shorter than one screen and never fires a scroll. - setReadToEnd(false); + dispatch({ type: "termsChanged", patch: { readToEnd: false } }); const el = termsBodyRef.current; - if (el && el.scrollHeight <= el.clientHeight + 8) setReadToEnd(true); + if (el && el.scrollHeight <= el.clientHeight + 8) dispatch({ type: "termsChanged", patch: { readToEnd: true } }); }, [termsOpen]); useEffect(() => { @@ -56,26 +56,25 @@ export default function LoginPage() { .then(({ initialized }) => { if (!initialized) router.replace("/setup"); }) - .catch(() => setError(t("Unable to connect to the backend service"))) - .finally(() => setChecking(false)); + .catch(() => dispatch({ type: "statusCheckFailed", error: t("Unable to connect to the backend service") })) + .finally(() => dispatch({ type: "statusChecked" })); }, [router]); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!agreed) { - setError(t("Please read and agree to the Terms of Use first")); + dispatch({ type: "validationFailed", error: t("Please read and agree to the Terms of Use first") }); return; } - setLoading(true); - setError(""); + dispatch({ type: "submitStarted" }); try { const { token } = await api.login("ARTEX", password); auth.setToken(token); window.location.replace("/function/tasks"); } catch { - setError(t("Incorrect username or password")); + dispatch({ type: "submitFailed", error: t("Incorrect username or password") }); } finally { - setLoading(false); + dispatch({ type: "submitFinished" }); } } @@ -131,7 +130,7 @@ export default function LoginPage() { id="password" type="password" value={password} - onChange={(e) => setPassword(e.target.value)} + onChange={(e) => dispatch({ type: "formChanged", patch: { password: e.target.value } })} placeholder={t("Enter your password")} autoFocus autoComplete="current-password" @@ -141,14 +140,14 @@ export default function LoginPage() { setAgreed(v === true)} + onCheckedChange={(v) => dispatch({ type: "formChanged", patch: { agreed: v === true } })} className="mt-0.5" />