Give useIsMobile an initial state

This commit is contained in:
Alex Gleason committed 2025-09-13 18:34:30 -05:00
1 parent 0edef42bf2
commit 787b0bb777
4 files changed
+20 -39

No files matched your search

+1 -8
View File
@@ -27,14 +27,7 @@ export function AppLayout({
const navigate = useNavigate();
const isMobile = useIsMobile();
const [isSidebarVisible, setIsSidebarVisible] = useState(() => {
// On mobile, always start hidden
if (typeof window !== 'undefined' && window.innerWidth < 768) {
return false;
}
// On desktop, we'll set the initial state after projects load
return false; // Default to closed initially
});
const [isSidebarVisible, setIsSidebarVisible] = useState(!isMobile);
// Track if we've set the initial state based on projects
const [hasSetInitialState, setHasSetInitialState] = useState(false);
+1 -7
View File
@@ -48,13 +48,7 @@ export function SettingsLayout() {
const projectsManager = useProjectsManager();
const [_projects, setProjects] = useState<Project[]>([]);
const [isSidebarVisible, setIsSidebarVisible] = useState(() => {
// On mobile, always start hidden
if (typeof window !== 'undefined' && window.innerWidth < 768) {
return false;
}
return false; // Default to closed initially
});
const [isSidebarVisible, setIsSidebarVisible] = useState(!isMobile);
const [hasSetInitialState, setHasSetInitialState] = useState(false);
const loadProjects = useCallback(async () => {
+12 -12
View File
@@ -1,19 +1,19 @@
import * as React from "react"
import { useEffect, useState } from "react"
const MOBILE_BREAKPOINT = 768
const MOBILE_BREAKPOINT = 768;
export function useIsMobile() {
const [isMobile, setIsMobile] = React.useState<boolean | undefined>(undefined)
export function useIsMobile(): boolean {
const [isMobile, setIsMobile] = useState(window.innerWidth < MOBILE_BREAKPOINT);
React.useEffect(() => {
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`)
useEffect(() => {
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
const onChange = () => {
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT)
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
}
mql.addEventListener("change", onChange)
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT)
return () => mql.removeEventListener("change", onChange)
}, [])
mql.addEventListener("change", onChange);
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
return () => mql.removeEventListener("change", onChange);
}, []);
return !!isMobile
return !!isMobile;
}
+6 -12
View File
@@ -39,17 +39,11 @@ export function ProjectView() {
const chatPaneRef = useRef<ChatPaneRef>(null);
const navigate = useNavigate();
const isMobile = useIsMobile();
const { fs: browserFS } = useFS();
const { fs } = useFS();
const { user } = useCurrentUser();
const { config } = useAppContext();
const { toast } = useToast();
const [isSidebarVisible, setIsSidebarVisible] = useState(() => {
// Check if we're on mobile immediately to set correct initial state
if (typeof window !== 'undefined') {
return window.innerWidth >= 768; // Desktop starts open, mobile starts closed
}
return false; // Default to closed for SSR
});
const [isSidebarVisible, setIsSidebarVisible] = useState(!isMobile);
// Keep-alive functionality to prevent tab throttling during AI processing
const { updateMetadata } = useKeepAlive({
@@ -83,14 +77,14 @@ export function ProjectView() {
}, [loadProject]);
const runBuild = async () => {
if (isBuildLoading || !project || !browserFS) return;
if (isBuildLoading || !project) return;
setIsBuildLoading(true);
updateMetadata('Shakespeare', `Building ${project.name}...`);
try {
const result = await buildProject({
fs: browserFS,
fs,
projectPath: `/projects/${project.id}`,
domParser: new DOMParser(),
});
@@ -104,7 +98,7 @@ export function ProjectView() {
};
const runDeploy = async () => {
if (isDeployLoading || !project || !browserFS) return;
if (isDeployLoading || !project) return;
// Check if user is logged in
if (!user || !user.signer) {
@@ -123,7 +117,7 @@ export function ProjectView() {
const result = await deployProject({
projectId: project.id,
deployServer: config.deployServer,
fs: browserFS,
fs,
projectPath: `/projects/${project.id}`,
signer: user.signer,
});