Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/hooks/use-mobile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import * as React from 'react';
const MOBILE_BREAKPOINT = 768;

export function useIsMobile() {
const [isMobile, setIsMobile] = React.useState<boolean>(false);
const [isMobile, setIsMobile] = React.useState<boolean>(() => {
if (typeof window === 'undefined') {
return false; // Default for SSR
}
return window.innerWidth < MOBILE_BREAKPOINT; // Initial value for client
});
Comment on lines +6 to +11
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

cat -n src/hooks/use-mobile.ts

Repository: Authormaton/authormaton

Length of output: 1049


🏁 Script executed:

rg -n "useIsMobile" --type=ts --type=tsx -C3

Repository: Authormaton/authormaton

Length of output: 94


🏁 Script executed:

rg -n "useIsMobile" --type=ts -C3

Repository: Authormaton/authormaton

Length of output: 6544


🏁 Script executed:

rg -n "useIsMobile" -C3

Repository: Authormaton/authormaton

Length of output: 6544


🏁 Script executed:

sed -n '55,80p' src/components/ui/sidebar.tsx

Repository: Authormaton/authormaton

Length of output: 958


🏁 Script executed:

sed -n '14,40p' src/components/models/projects/ProjectsTable.tsx

Repository: Authormaton/authormaton

Length of output: 908


🏁 Script executed:

sed -n '8,30p' src/components/layouts/GeneralLayout.tsx

Repository: Authormaton/authormaton

Length of output: 913


🏁 Script executed:

sed -n '14,60p' src/components/models/projects/ProjectsTable.tsx

Repository: Authormaton/authormaton

Length of output: 1433


🏁 Script executed:

rg -n "suppressHydrationWarning" --type=ts

Repository: Authormaton/authormaton

Length of output: 143


🏁 Script executed:

rg -n "suppressHydrationWarning"

Repository: Authormaton/authormaton

Length of output: 143


Hydration mismatch from lazy useState initializer reading window

The lazy initializer with typeof window === 'undefined' is runtime-safe for SSR, but it causes server and initial client renders to diverge:

  • On the server, isMobile is always false.
  • On the client during hydration, the initializer may return true if innerWidth < 768.

This causes hydration mismatches in components that conditionally render based on isMobile:

  • GeneralLayout.tsx renders <Sheet> on mobile but <div> on desktop based on this value
  • ProjectsTable.tsx renders a grid on mobile but <BasicTable> on desktop based on this value

Both components will render different markup on the server versus the client, triggering hydration warnings and potential layout shifts after the effect updates.

Initialize to a constant value (e.g., useState(false)) and rely on the effect to update after mount, ensuring server and first hydration render are consistent. Alternatively, make these components client-only or add suppressHydrationWarning if the brief mismatch is acceptable.

🤖 Prompt for AI Agents
In src/hooks/use-mobile.ts around lines 6 to 11, the lazy useState initializer
reads window causing server vs initial client render divergence; change the
initial state to a constant (e.g., useState(false)) so SSR and first hydration
produce the same output, then in a useEffect run only on mount to read
window.innerWidth and setIsMobile accordingly, and also add a resize listener
inside that effect to update on viewport changes with proper cleanup on unmount.


React.useEffect(() => {
if (typeof window === 'undefined') {
Expand All @@ -14,8 +19,6 @@ export function useIsMobile() {
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
};

checkIsMobile(); // Set initial value

const resizeObserver = new ResizeObserver(() => {
checkIsMobile();
});
Expand Down
Loading