WIP 2.0.0: React Compiler lint rules back to errors; docs
- The 192 places the rules flagged are fixed and the rules are errors again (npx eslint src: nothing). lib/hooks.ts: useStateSync (state that follows other state, set while rendering), useExternalEffect (API loads and browser reads), useNow; lib/navigation.ts hardNavigate for full reloads after sign-out or identity switches; other pages use the router. Resets moved into the handlers that cause them. - The banner shows and hides at its times on open pages. - HANDOVER, roadmap, README, FEATURES, SETTINGS, CHANGELOG updated; 2.0.0 section drafted in WHATS-NEW. e2e 102/102 (PayFast sandbox skipped); production build passes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { Event, Paginated, Payment, Registration, User } from "@/types";
|
||||
import { hardNavigate } from "@/lib/navigation";
|
||||
|
||||
export type ApiOptions = {
|
||||
method?: string;
|
||||
@@ -142,7 +143,7 @@ export async function apiFetch<T = unknown>(path: string, options: ApiOptions =
|
||||
localStorage.removeItem("hope_events_user_cache");
|
||||
if (!window.location.pathname.startsWith("/login")) {
|
||||
const back = window.location.pathname + window.location.search;
|
||||
window.location.href = `/login?reason=2fa&redirect=${encodeURIComponent(back)}`;
|
||||
hardNavigate(`/login?reason=2fa&redirect=${encodeURIComponent(back)}`);
|
||||
}
|
||||
}
|
||||
throw new ApiError(res.status, message, data);
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
"use client";
|
||||
import { useEffect, useState, type DependencyList } from "react";
|
||||
|
||||
/**
|
||||
* Hooks for the effect patterns the React Compiler lint rules flag (2.0, when those rules went
|
||||
* back to errors). Which one:
|
||||
*
|
||||
* - Nothing to store? Compute it while rendering (or `useMemo`).
|
||||
* - State that follows other state or props ("when the event changes, start the form again")?
|
||||
* `useStateSync`.
|
||||
* - Reading or subscribing to something outside React — the API, localStorage, the URL, browser
|
||||
* events — and setting state from what comes back? `useExternalEffect`.
|
||||
*
|
||||
* Both take a deps array, checked by exhaustive-deps like useEffect's (eslint.config.mjs).
|
||||
*/
|
||||
|
||||
/**
|
||||
* `useEffect` for effects that read from or subscribe to the outside world (the API, the
|
||||
* browser) and set state from it — which the lint can't tell apart from state derived from
|
||||
* other state. Runs after render and when `deps` change; a returned function is the clean-up.
|
||||
*/
|
||||
export function useExternalEffect(run: () => void | (() => void) | Promise<unknown>, deps: DependencyList) {
|
||||
useEffect(() => {
|
||||
const out = run();
|
||||
return typeof out === "function" ? out : undefined;
|
||||
// The caller's deps, checked at the call site by exhaustive-deps (additionalHooks).
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, deps);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates this component's own state when `deps` change (and on the first render), while
|
||||
* rendering — React's "adjusting state when a prop changes", without an effect's extra render
|
||||
* and flash of the old state. `update` may only set state of this component, and the deps must
|
||||
* be stable (state, props, memoised values): a new object each render would loop.
|
||||
*/
|
||||
export function useStateSync(update: () => void, deps: DependencyList) {
|
||||
const [seen, setSeen] = useState<DependencyList | null>(null);
|
||||
if (seen === null || seen.length !== deps.length || seen.some((d, i) => !Object.is(d, deps[i]))) {
|
||||
setSeen(deps);
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
/** The current time in ms, refreshed every `everyMs` (default a minute), so "is it over yet?" stays right. */
|
||||
export function useNow(everyMs = 60_000): number {
|
||||
const [now, setNow] = useState(() => Date.now());
|
||||
useEffect(() => {
|
||||
const t = setInterval(() => setNow(Date.now()), everyMs);
|
||||
return () => clearInterval(t);
|
||||
}, [everyMs]);
|
||||
return now;
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
* browser sends.
|
||||
*/
|
||||
import { API_BASE } from "@/lib/api";
|
||||
import { hardNavigate } from "@/lib/navigation";
|
||||
|
||||
const TOKEN_KEY = "hope_events_token";
|
||||
const USER_CACHE_KEY = "hope_events_user_cache";
|
||||
@@ -38,7 +39,7 @@ export function beginImpersonation(res: ImpersonationStart) {
|
||||
localStorage.removeItem(USER_CACHE_KEY);
|
||||
localStorage.setItem(IMP_KEY, JSON.stringify({ sessionId: res.session.id, expiresAt: res.session.expiresAt, name: res.session.target.name, email: res.session.target.email }));
|
||||
} catch { /* storage unavailable */ }
|
||||
window.location.href = "/dashboard/user";
|
||||
hardNavigate("/dashboard/user");
|
||||
}
|
||||
|
||||
/** Puts the admin's own session back (no server call). */
|
||||
@@ -52,7 +53,7 @@ export function restoreAdmin(redirect = true) {
|
||||
localStorage.removeItem(ADMIN_CACHE_KEY);
|
||||
localStorage.removeItem(IMP_KEY);
|
||||
} catch { /* storage unavailable */ }
|
||||
if (redirect) window.location.href = "/dashboard/admin/users";
|
||||
if (redirect) hardNavigate("/dashboard/admin/users");
|
||||
}
|
||||
|
||||
/** Ends the session on the server (audited), then restores the admin. */
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Goes to another page with a full page load. Only for when the whole app must start again —
|
||||
* after signing out or switching who is signed in — or from code outside a component; anywhere
|
||||
* else use `useRouter().push()`.
|
||||
*/
|
||||
export function hardNavigate(path: string) {
|
||||
window.location.assign(new URL(path, window.location.origin).href);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { useState } from "react";
|
||||
import { useExternalEffect } from "@/lib/hooks";
|
||||
|
||||
/**
|
||||
* Static fallback for BRAND_600 — used as the initial value before the
|
||||
@@ -21,7 +22,7 @@ const BRAND_600_FALLBACK = "#4F46E5";
|
||||
export function useBrandColorVar(cssVar: string, fallback: string = BRAND_600_FALLBACK): string {
|
||||
const [color, setColor] = useState(fallback);
|
||||
|
||||
useEffect(() => {
|
||||
useExternalEffect(() => {
|
||||
const value = getComputedStyle(document.documentElement).getPropertyValue(cssVar).trim();
|
||||
if (value) setColor(value);
|
||||
}, [cssVar]);
|
||||
|
||||
Reference in New Issue
Block a user