2026-04-29 14:04:02 +02:00
|
|
|
'use client';
|
|
|
|
|
|
fix(compiler): React Compiler safety triage — 5 categories cleared
Cleared 4 rule buckets (37 violations, including 5 real bugs) and
silenced 1 informational bucket from the Next 16 / react-hooks v7
upgrade. Cleared rules promoted from `warn` back to `error` so new
regressions block CI.
Real bug fixes:
- `interest-contact-log-tab.tsx`: `useMemo` used for side effects
(5 setState calls inside a memo body); converted to `useEffect`.
- `PieChart.tsx`: cumulative `let angle` mutation in a render-phase
`map`; converted to `reduce` so the slice array is built without
re-assignment.
- `documents-hub.tsx`: `useMemo(() => ({ count: 0 }))` used as a
mutable drag counter; converted to `useRef`.
- `notes-list.tsx`: `Date.now()` read during render for note-edit
countdown (impure) → pinned to a `now` state ticked every 30s.
- `onboarding-checklist.tsx` / `user-profile.tsx` /
`user-settings.tsx`: `useEffect(() => void load(), [])` with the
`load` function declared AFTER the effect — relied on hoisting,
trips Compiler's "access before declared" rule. Declared inside
the effect.
Pattern fixes (intentional cache-via-ref → state or layout-effect):
- 6 `ref.current = x` writes during render moved into layout
effects (`use-realtime-invalidation`, `settings-form-card`,
`inbox`).
- 3 `ref.current` reads during render (search totals cache,
scanner file ref) rewritten to backed-by-state.
- `use-is-mobile.ts` rewritten on `useSyncExternalStore` to avoid
the SSR-then-rehydrate setState dance.
- `use-notifications.ts` rewritten to write socket pushes directly
into the React Query cache via `setQueryData`, removing a local
state mirror.
Rule config (`eslint.config.mjs`):
- `react-hooks/purity` → error (was warn, cleared)
- `react-hooks/set-state-in-render` → error (was warn, cleared)
- `react-hooks/immutability` → error (was warn, cleared)
- `react-hooks/refs` → error (was warn, cleared)
- `react-hooks/incompatible-library` → off (informational only)
- `react-hooks/set-state-in-effect` → warn (51 remaining, all the
useEffect→fetch→setState data-fetch pattern; migration to
useQuery tracked in BACKLOG)
Verified: tsc clean, eslint 0 errors / 69 warnings (down from 105),
vitest 1315/1315, next build green.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 23:14:16 +02:00
|
|
|
import { useSyncExternalStore } from 'react';
|
2026-04-29 14:04:02 +02:00
|
|
|
|
2026-05-22 13:37:23 +02:00
|
|
|
// 3-tier breakpoints aligned with Tailwind:
|
|
|
|
|
// mobile : < 768px (sm and below)
|
|
|
|
|
// tablet : 768-1023 (md)
|
|
|
|
|
// desktop : >= 1024 (lg and up)
|
|
|
|
|
//
|
|
|
|
|
// Previously the app used a binary `(max-width: 1023.98px)` split, which
|
|
|
|
|
// rendered the mobile shell on iPad portrait AND on a half-screen 13"
|
|
|
|
|
// Mac browser — neither is really "mobile." The tablet tier fills that
|
|
|
|
|
// gap so the desktop shell can render with a hidden-but-accessible
|
|
|
|
|
// sidebar at those widths.
|
2026-04-29 14:04:02 +02:00
|
|
|
|
2026-05-22 13:37:23 +02:00
|
|
|
export type ViewportTier = 'mobile' | 'tablet' | 'desktop';
|
|
|
|
|
|
|
|
|
|
const TABLET_QUERY = '(min-width: 768px) and (max-width: 1023.98px)';
|
|
|
|
|
const DESKTOP_QUERY = '(min-width: 1024px)';
|
|
|
|
|
|
|
|
|
|
function subscribeTier(callback: () => void): () => void {
|
|
|
|
|
const mqTablet = window.matchMedia(TABLET_QUERY);
|
|
|
|
|
const mqDesktop = window.matchMedia(DESKTOP_QUERY);
|
|
|
|
|
mqTablet.addEventListener('change', callback);
|
|
|
|
|
mqDesktop.addEventListener('change', callback);
|
|
|
|
|
return () => {
|
|
|
|
|
mqTablet.removeEventListener('change', callback);
|
|
|
|
|
mqDesktop.removeEventListener('change', callback);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getTierSnapshot(): ViewportTier {
|
|
|
|
|
if (window.matchMedia(DESKTOP_QUERY).matches) return 'desktop';
|
|
|
|
|
if (window.matchMedia(TABLET_QUERY).matches) return 'tablet';
|
|
|
|
|
return 'mobile';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getTierServerSnapshot(): ViewportTier {
|
|
|
|
|
// Server has no window — default to desktop so the desktop shell
|
|
|
|
|
// mounts on first paint. Client re-evaluates immediately on hydration
|
|
|
|
|
// (useSyncExternalStore is server-mismatch-safe).
|
|
|
|
|
return 'desktop';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the active viewport tier. Backed by useSyncExternalStore so
|
|
|
|
|
* render reads stay pure (no useEffect → setState cascade); React
|
|
|
|
|
* Compiler-safe.
|
|
|
|
|
*/
|
|
|
|
|
export function useViewportTier(): ViewportTier {
|
|
|
|
|
return useSyncExternalStore(subscribeTier, getTierSnapshot, getTierServerSnapshot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Back-compat alias ──────────────────────────────────────────────────────
|
|
|
|
|
// Every existing call site treats "mobile OR tablet" as one bucket (e.g.
|
|
|
|
|
// "show short labels", "stack vertically"). Returning `tier !== 'desktop'`
|
|
|
|
|
// preserves that behaviour so the tier rollout doesn't have to touch
|
|
|
|
|
// dozens of components in lockstep.
|
|
|
|
|
|
|
|
|
|
const LEGACY_QUERY = '(max-width: 1023.98px)';
|
|
|
|
|
|
|
|
|
|
function subscribeLegacy(callback: () => void): () => void {
|
|
|
|
|
const mq = window.matchMedia(LEGACY_QUERY);
|
fix(compiler): React Compiler safety triage — 5 categories cleared
Cleared 4 rule buckets (37 violations, including 5 real bugs) and
silenced 1 informational bucket from the Next 16 / react-hooks v7
upgrade. Cleared rules promoted from `warn` back to `error` so new
regressions block CI.
Real bug fixes:
- `interest-contact-log-tab.tsx`: `useMemo` used for side effects
(5 setState calls inside a memo body); converted to `useEffect`.
- `PieChart.tsx`: cumulative `let angle` mutation in a render-phase
`map`; converted to `reduce` so the slice array is built without
re-assignment.
- `documents-hub.tsx`: `useMemo(() => ({ count: 0 }))` used as a
mutable drag counter; converted to `useRef`.
- `notes-list.tsx`: `Date.now()` read during render for note-edit
countdown (impure) → pinned to a `now` state ticked every 30s.
- `onboarding-checklist.tsx` / `user-profile.tsx` /
`user-settings.tsx`: `useEffect(() => void load(), [])` with the
`load` function declared AFTER the effect — relied on hoisting,
trips Compiler's "access before declared" rule. Declared inside
the effect.
Pattern fixes (intentional cache-via-ref → state or layout-effect):
- 6 `ref.current = x` writes during render moved into layout
effects (`use-realtime-invalidation`, `settings-form-card`,
`inbox`).
- 3 `ref.current` reads during render (search totals cache,
scanner file ref) rewritten to backed-by-state.
- `use-is-mobile.ts` rewritten on `useSyncExternalStore` to avoid
the SSR-then-rehydrate setState dance.
- `use-notifications.ts` rewritten to write socket pushes directly
into the React Query cache via `setQueryData`, removing a local
state mirror.
Rule config (`eslint.config.mjs`):
- `react-hooks/purity` → error (was warn, cleared)
- `react-hooks/set-state-in-render` → error (was warn, cleared)
- `react-hooks/immutability` → error (was warn, cleared)
- `react-hooks/refs` → error (was warn, cleared)
- `react-hooks/incompatible-library` → off (informational only)
- `react-hooks/set-state-in-effect` → warn (51 remaining, all the
useEffect→fetch→setState data-fetch pattern; migration to
useQuery tracked in BACKLOG)
Verified: tsc clean, eslint 0 errors / 69 warnings (down from 105),
vitest 1315/1315, next build green.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 23:14:16 +02:00
|
|
|
mq.addEventListener('change', callback);
|
|
|
|
|
return () => mq.removeEventListener('change', callback);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 13:37:23 +02:00
|
|
|
function getLegacySnapshot(): boolean {
|
|
|
|
|
return window.matchMedia(LEGACY_QUERY).matches;
|
fix(compiler): React Compiler safety triage — 5 categories cleared
Cleared 4 rule buckets (37 violations, including 5 real bugs) and
silenced 1 informational bucket from the Next 16 / react-hooks v7
upgrade. Cleared rules promoted from `warn` back to `error` so new
regressions block CI.
Real bug fixes:
- `interest-contact-log-tab.tsx`: `useMemo` used for side effects
(5 setState calls inside a memo body); converted to `useEffect`.
- `PieChart.tsx`: cumulative `let angle` mutation in a render-phase
`map`; converted to `reduce` so the slice array is built without
re-assignment.
- `documents-hub.tsx`: `useMemo(() => ({ count: 0 }))` used as a
mutable drag counter; converted to `useRef`.
- `notes-list.tsx`: `Date.now()` read during render for note-edit
countdown (impure) → pinned to a `now` state ticked every 30s.
- `onboarding-checklist.tsx` / `user-profile.tsx` /
`user-settings.tsx`: `useEffect(() => void load(), [])` with the
`load` function declared AFTER the effect — relied on hoisting,
trips Compiler's "access before declared" rule. Declared inside
the effect.
Pattern fixes (intentional cache-via-ref → state or layout-effect):
- 6 `ref.current = x` writes during render moved into layout
effects (`use-realtime-invalidation`, `settings-form-card`,
`inbox`).
- 3 `ref.current` reads during render (search totals cache,
scanner file ref) rewritten to backed-by-state.
- `use-is-mobile.ts` rewritten on `useSyncExternalStore` to avoid
the SSR-then-rehydrate setState dance.
- `use-notifications.ts` rewritten to write socket pushes directly
into the React Query cache via `setQueryData`, removing a local
state mirror.
Rule config (`eslint.config.mjs`):
- `react-hooks/purity` → error (was warn, cleared)
- `react-hooks/set-state-in-render` → error (was warn, cleared)
- `react-hooks/immutability` → error (was warn, cleared)
- `react-hooks/refs` → error (was warn, cleared)
- `react-hooks/incompatible-library` → off (informational only)
- `react-hooks/set-state-in-effect` → warn (51 remaining, all the
useEffect→fetch→setState data-fetch pattern; migration to
useQuery tracked in BACKLOG)
Verified: tsc clean, eslint 0 errors / 69 warnings (down from 105),
vitest 1315/1315, next build green.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 23:14:16 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-22 13:37:23 +02:00
|
|
|
function getLegacyServerSnapshot(): boolean {
|
fix(compiler): React Compiler safety triage — 5 categories cleared
Cleared 4 rule buckets (37 violations, including 5 real bugs) and
silenced 1 informational bucket from the Next 16 / react-hooks v7
upgrade. Cleared rules promoted from `warn` back to `error` so new
regressions block CI.
Real bug fixes:
- `interest-contact-log-tab.tsx`: `useMemo` used for side effects
(5 setState calls inside a memo body); converted to `useEffect`.
- `PieChart.tsx`: cumulative `let angle` mutation in a render-phase
`map`; converted to `reduce` so the slice array is built without
re-assignment.
- `documents-hub.tsx`: `useMemo(() => ({ count: 0 }))` used as a
mutable drag counter; converted to `useRef`.
- `notes-list.tsx`: `Date.now()` read during render for note-edit
countdown (impure) → pinned to a `now` state ticked every 30s.
- `onboarding-checklist.tsx` / `user-profile.tsx` /
`user-settings.tsx`: `useEffect(() => void load(), [])` with the
`load` function declared AFTER the effect — relied on hoisting,
trips Compiler's "access before declared" rule. Declared inside
the effect.
Pattern fixes (intentional cache-via-ref → state or layout-effect):
- 6 `ref.current = x` writes during render moved into layout
effects (`use-realtime-invalidation`, `settings-form-card`,
`inbox`).
- 3 `ref.current` reads during render (search totals cache,
scanner file ref) rewritten to backed-by-state.
- `use-is-mobile.ts` rewritten on `useSyncExternalStore` to avoid
the SSR-then-rehydrate setState dance.
- `use-notifications.ts` rewritten to write socket pushes directly
into the React Query cache via `setQueryData`, removing a local
state mirror.
Rule config (`eslint.config.mjs`):
- `react-hooks/purity` → error (was warn, cleared)
- `react-hooks/set-state-in-render` → error (was warn, cleared)
- `react-hooks/immutability` → error (was warn, cleared)
- `react-hooks/refs` → error (was warn, cleared)
- `react-hooks/incompatible-library` → off (informational only)
- `react-hooks/set-state-in-effect` → warn (51 remaining, all the
useEffect→fetch→setState data-fetch pattern; migration to
useQuery tracked in BACKLOG)
Verified: tsc clean, eslint 0 errors / 69 warnings (down from 105),
vitest 1315/1315, next build green.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 23:14:16 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-29 14:04:02 +02:00
|
|
|
/**
|
2026-05-22 13:37:23 +02:00
|
|
|
* Returns true when the viewport is below the `lg` Tailwind breakpoint
|
|
|
|
|
* (i.e. mobile OR tablet). Kept as an alias for backwards compatibility
|
|
|
|
|
* with call sites that don't care about the mobile-vs-tablet distinction.
|
|
|
|
|
* New code should prefer `useViewportTier()` for explicit tier checks.
|
2026-04-29 14:04:02 +02:00
|
|
|
*/
|
|
|
|
|
export function useIsMobile(): boolean {
|
2026-05-22 13:37:23 +02:00
|
|
|
return useSyncExternalStore(subscribeLegacy, getLegacySnapshot, getLegacyServerSnapshot);
|
2026-04-29 14:04:02 +02:00
|
|
|
}
|