2026-06-06 23:10:29 +03:00
"use client" ;
2026-07-19 15:58:52 +03:00
import { Clock3 , EllipsisVertical , FileUp , FolderKanban , Grid3X3 , HomeIcon , List , Pencil , Plus , RefreshCw , Search , Settings , SlidersHorizontal , Trash2 , X } from "lucide-react" ;
2026-06-06 23:10:29 +03:00
import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
2026-07-07 23:59:36 +09:00
import { SketchForgeEditor , importedShapeFromStl , importedShapeFromSvg } from "@/components/SketchForgeEditor" ;
2026-07-14 09:43:37 +03:00
import { hydrateEditorHistoryState , type EditorHistoryEntry } from "@/lib/editorHistory" ;
2026-06-14 22:05:58 +03:00
import { createLocalId } from "@/lib/localIds" ;
2026-07-16 12:30:39 +03:00
import { attachProjectAsset , dedupeProjectAssets , projectAssetFromBytes , sourceFormatForFileName } from "@/lib/projectAssets" ;
import { importSkfProject } from "@/lib/skfProject" ;
2026-06-18 08:56:40 +03:00
import { importExtensionSupported } from "@/lib/stlImport" ;
2026-07-11 20:34:13 +03:00
import { DEFAULT_SNAP_GRID , DEFAULT_WORKPLANE_WORKSPACE , normalizeSnapGrid , normalizeWorkspaceSettings , workplaneSettingsFingerprint } from "@/lib/workplaneSettings" ;
2026-07-16 12:30:39 +03:00
import type { GridSize , ProjectAsset , WorkplaneShape , WorkplaneWorkspaceSettings } from "@/types/sketchforge" ;
2026-06-06 23:10:29 +03:00
type AppView = "dashboard" | "editor" ;
type ViewMode = "grid" | "list" ;
2026-07-19 15:58:52 +03:00
type DashboardSection = "home" | "shared" | "challenges" ;
2026-06-06 23:10:29 +03:00
type DownloadMode = "browser" | "folder" ;
type DashboardProject = {
id : string ;
name : string ;
createdAt : number ;
updatedAt : number ;
shapes : number ;
accent : "cyan" | "green" | "gold" | "red" ;
thumbnailUrl? : string | null ;
thumbnailVersion? : number ;
revision? : number ;
2026-06-13 10:26:40 +03:00
workspace? : WorkplaneWorkspaceSettings ;
snapGrid? : GridSize ;
2026-07-16 12:30:39 +03:00
placementElevation? : number ;
2026-07-19 15:58:52 +03:00
sharedProject ?: { fileName : string ; revision : string };
};
type SharedProject = {
fileName : string ;
name : string ;
updatedAt : number ;
size : number ;
revision : string ;
2026-06-06 23:10:29 +03:00
};
type StoredDashboardProject = Partial < DashboardProject > & {
designShapes? : unknown ;
};
type ProjectShapeCacheEntry = {
revision : number ;
shapes : WorkplaneShape [];
2026-07-14 09:43:37 +03:00
history : EditorHistoryEntry [];
historyIndex : number ;
2026-07-16 12:30:39 +03:00
assets : ProjectAsset [];
2026-06-06 23:10:29 +03:00
};
2026-07-14 09:43:37 +03:00
type ProjectShapeRecord = {
2026-06-06 23:10:29 +03:00
id : string ;
2026-07-14 09:43:37 +03:00
revision : number ;
shapes : WorkplaneShape [];
history? : EditorHistoryEntry [];
historyIndex? : number ;
2026-07-16 12:30:39 +03:00
assets? : ProjectAsset [];
2026-06-06 23:10:29 +03:00
updatedAt : number ;
};
const PROJECTS_STORAGE_KEY = "sketchForge.projects" ;
const PROJECT_SHAPES_DB_NAME = "sketchForge.projectShapes" ;
const PROJECT_SHAPES_STORE_NAME = "projectShapes" ;
const DOWNLOAD_MODE_STORAGE_KEY = "sketchForge.downloadMode" ;
const DOWNLOAD_FOLDER_STORAGE_KEY = "sketchForge.downloadFolder" ;
const PROJECT_ACCENTS : DashboardProject [ "accent" ][] = [ "cyan" , "green" , "gold" , "red" ];
2026-06-14 22:05:58 +03:00
const STATIC_EXPORT_BUILD = process . env . NEXT_PUBLIC_STATIC_EXPORT === "true" ;
2026-06-06 23:10:29 +03:00
function formatUpdated ( timestamp : number ) {
const age = Date . now () - timestamp ;
if ( age < 60 _000 ) return "Just now" ;
if ( age < 3 _600_000 ) return ` ${ Math . max ( 1 , Math . round ( age / 60 _000 )) } min ago` ;
if ( age < 86 _400_000 ) return "Today" ;
return new Intl . DateTimeFormat ( undefined , { month : "short" , day : "numeric" }). format ( new Date ( timestamp ));
}
2026-07-19 15:58:52 +03:00
function formatFileSize ( bytes : number ) {
if ( bytes < 1024 ) return ` ${ bytes } B` ;
if ( bytes < 1024 * 1024 ) return ` ${ ( bytes / 1024 ). toFixed ( bytes < 10 * 1024 ? 1 : 0 ) } KB` ;
return ` ${ ( bytes / ( 1024 * 1024 )). toFixed ( bytes < 10 * 1024 * 1024 ? 1 : 0 ) } MB` ;
}
2026-07-14 09:43:37 +03:00
function projectShapeCacheEntry (
revision : number ,
shapes : WorkplaneShape [],
history? : EditorHistoryEntry [],
historyIndex? : number ,
2026-07-16 12:30:39 +03:00
assets : ProjectAsset [] = [],
2026-07-14 09:43:37 +03:00
) : ProjectShapeCacheEntry {
const hydrated = hydrateEditorHistoryState ( shapes , history , historyIndex );
return {
revision ,
shapes : hydrated.entries [ hydrated . index ] ? . shapes ?? shapes ,
history : hydrated.entries ,
historyIndex : hydrated.index ,
2026-07-16 12:30:39 +03:00
assets : dedupeProjectAssets ( assets ),
2026-07-14 09:43:37 +03:00
};
}
2026-06-06 23:10:29 +03:00
function openProjectShapesDb() {
return new Promise < IDBDatabase >(( resolve , reject ) => {
if ( typeof window === "undefined" || ! window . indexedDB ) {
reject ( new Error ( "Project shape storage is unavailable" ));
return ;
}
const request = window . indexedDB . open ( PROJECT_SHAPES_DB_NAME , 1 );
request . onupgradeneeded = () => {
const database = request . result ;
if ( ! database . objectStoreNames . contains ( PROJECT_SHAPES_STORE_NAME )) {
database . createObjectStore ( PROJECT_SHAPES_STORE_NAME , { keyPath : "id" });
}
};
request . onerror = () => reject ( request . error ?? new Error ( "Could not open project shape storage" ));
request . onsuccess = () => resolve ( request . result );
});
}
async function loadProjectShapes ( projectId : string ) {
const database = await openProjectShapesDb ();
return new Promise < ProjectShapeRecord | null >(( resolve , reject ) => {
const transaction = database . transaction ( PROJECT_SHAPES_STORE_NAME , "readonly" );
const request = transaction . objectStore ( PROJECT_SHAPES_STORE_NAME ). get ( projectId );
request . onerror = () => reject ( request . error ?? new Error ( "Could not load project shapes" ));
request . onsuccess = () => resolve (( request . result as ProjectShapeRecord | undefined ) ?? null );
transaction . oncomplete = () => database . close ();
transaction . onerror = () => {
database . close ();
reject ( transaction . error ?? new Error ( "Could not load project shapes" ));
};
});
}
2026-07-14 09:43:37 +03:00
async function saveProjectShapes ( projectId : string , entry : ProjectShapeCacheEntry ) {
2026-06-06 23:10:29 +03:00
const database = await openProjectShapesDb ();
return new Promise < void >(( resolve , reject ) => {
const transaction = database . transaction ( PROJECT_SHAPES_STORE_NAME , "readwrite" );
2026-06-14 22:05:58 +03:00
const store = transaction . objectStore ( PROJECT_SHAPES_STORE_NAME );
const existingRequest = store . get ( projectId );
existingRequest . onerror = () => {
transaction . abort ();
};
existingRequest . onsuccess = () => {
const existing = existingRequest . result as ProjectShapeRecord | undefined ;
2026-07-14 09:43:37 +03:00
if ( existing && existing . revision > entry . revision ) {
2026-06-14 22:05:58 +03:00
return ;
}
2026-07-14 09:43:37 +03:00
store . put ({ id : projectId , ... entry , updatedAt : Date.now () } satisfies ProjectShapeRecord );
2026-06-14 22:05:58 +03:00
};
2026-06-06 23:10:29 +03:00
transaction . oncomplete = () => {
database . close ();
resolve ();
};
transaction . onerror = () => {
database . close ();
reject ( transaction . error ?? new Error ( "Could not save project shapes" ));
};
transaction . onabort = () => {
database . close ();
reject ( transaction . error ?? new Error ( "Could not save project shapes" ));
};
});
}
async function deleteProjectShapes ( projectId : string ) {
const database = await openProjectShapesDb ();
return new Promise < void >(( resolve , reject ) => {
const transaction = database . transaction ( PROJECT_SHAPES_STORE_NAME , "readwrite" );
transaction . objectStore ( PROJECT_SHAPES_STORE_NAME ). delete ( projectId );
transaction . oncomplete = () => {
database . close ();
resolve ();
};
transaction . onerror = () => {
database . close ();
reject ( transaction . error ?? new Error ( "Could not delete project shapes" ));
};
});
}
function readStoredProjects() {
const legacyShapes : Record < string , ProjectShapeCacheEntry > = {};
if ( typeof window === "undefined" ) return { projects : [] as DashboardProject [], legacyShapes };
try {
const parsed = JSON . parse ( window . localStorage . getItem ( PROJECTS_STORAGE_KEY ) ?? "[]" ) as StoredDashboardProject [];
const projects = parsed
. filter (( project ) => typeof project . id === "string" && typeof project . name === "string" )
. map (( project , index ) => {
const id = project . id as string ;
const updatedAt = typeof project . updatedAt === "number" ? project.updatedAt : Date.now ();
const revision = typeof project . revision === "number" ? project.revision : updatedAt ;
const designShapes = Array . isArray ( project . designShapes ) ? ( project . designShapes as WorkplaneShape []) : null ;
if ( designShapes ) {
2026-07-14 09:43:37 +03:00
legacyShapes [ id ] = projectShapeCacheEntry ( revision , designShapes );
2026-06-06 23:10:29 +03:00
}
return {
id ,
name : project.name as string ,
createdAt : typeof project . createdAt === "number" ? project.createdAt : Date.now (),
updatedAt ,
shapes : typeof project . shapes === "number" ? project . shapes : ( designShapes ? . length ?? 0 ),
accent : PROJECT_ACCENTS.includes ( project . accent as DashboardProject [ "accent" ]) ? ( project . accent as DashboardProject [ "accent" ]) : PROJECT_ACCENTS [ index % PROJECT_ACCENTS . length ],
thumbnailUrl : typeof project . thumbnailUrl === "string" ? project.thumbnailUrl : null ,
thumbnailVersion : typeof project . thumbnailVersion === "number" ? project.thumbnailVersion : undefined ,
revision ,
2026-06-13 10:26:40 +03:00
workspace : normalizeWorkspaceSettings ( project . workspace ),
snapGrid : normalizeSnapGrid ( project . snapGrid ),
2026-07-16 12:30:39 +03:00
placementElevation : typeof project . placementElevation === "number" && Number . isFinite ( project . placementElevation ) ? project.placementElevation : 0 ,
2026-07-19 15:58:52 +03:00
sharedProject : typeof project . sharedProject ? . fileName === "string" && typeof project . sharedProject . revision === "string"
? { fileName : project.sharedProject.fileName , revision : project.sharedProject.revision }
: undefined ,
2026-06-06 23:10:29 +03:00
};
});
return { projects , legacyShapes };
} catch {
return { projects : [], legacyShapes };
}
}
function readProjects() {
return readStoredProjects (). projects ;
}
function mergeProjectForStorage ( project : DashboardProject , storedProject? : DashboardProject ) {
if ( ! storedProject ) {
return project ;
}
const projectRevision = project . revision ?? 0 ;
const storedRevision = storedProject . revision ?? 0 ;
if ( storedRevision <= projectRevision ) {
return project ;
}
return {
... project ,
revision : storedProject.revision ,
shapes : storedProject.shapes || project . shapes ,
thumbnailUrl : project.thumbnailUrl ?? storedProject . thumbnailUrl ,
thumbnailVersion : project.thumbnailVersion ?? storedProject . thumbnailVersion ,
updatedAt : Math.max ( project . updatedAt , storedProject . updatedAt ),
2026-06-13 10:26:40 +03:00
workspace : project.workspace ?? storedProject . workspace ,
snapGrid : project.snapGrid ?? storedProject . snapGrid ,
2026-07-16 12:30:39 +03:00
placementElevation : project.placementElevation ?? storedProject . placementElevation ,
2026-07-19 15:58:52 +03:00
sharedProject : project.sharedProject ?? storedProject . sharedProject ,
2026-06-06 23:10:29 +03:00
};
}
function projectForStorage ( project : DashboardProject ) : DashboardProject {
return {
id : project.id ,
name : project.name ,
createdAt : project.createdAt ,
updatedAt : project.updatedAt ,
shapes : project.shapes ,
accent : project.accent ,
thumbnailUrl : project.thumbnailUrl ?? null ,
thumbnailVersion : project.thumbnailVersion ,
revision : project.revision ,
2026-06-13 10:26:40 +03:00
workspace : normalizeWorkspaceSettings ( project . workspace ),
snapGrid : normalizeSnapGrid ( project . snapGrid ),
2026-07-16 12:30:39 +03:00
placementElevation : typeof project . placementElevation === "number" && Number . isFinite ( project . placementElevation ) ? project.placementElevation : 0 ,
2026-07-19 15:58:52 +03:00
sharedProject : project.sharedProject ,
2026-06-06 23:10:29 +03:00
};
}
function mergeProjectsForStorage ( projects : DashboardProject []) {
const storedProjects = readProjects ();
const storedById = new Map ( storedProjects . map (( project ) => [ project . id , project ]));
return projects . map (( project ) => projectForStorage ( mergeProjectForStorage ( project , storedById . get ( project . id ))));
}
function newProject ( name : string , index : number , shapeCount = 0 ) : DashboardProject {
const now = Date . now ();
return {
2026-06-14 22:05:58 +03:00
id : createLocalId ( "project" ),
2026-06-06 23:10:29 +03:00
name ,
createdAt : now ,
updatedAt : now ,
shapes : shapeCount ,
accent : PROJECT_ACCENTS [ index % PROJECT_ACCENTS . length ],
revision : now ,
2026-06-13 10:26:40 +03:00
workspace : DEFAULT_WORKPLANE_WORKSPACE ,
snapGrid : DEFAULT_SNAP_GRID ,
2026-07-16 12:30:39 +03:00
placementElevation : 0 ,
2026-06-06 23:10:29 +03:00
};
}
function projectNameFromFileName ( fileName : string ) {
2026-07-07 23:59:36 +09:00
return fileName . replace ( /\.[^.]+$/ , "" ). trim () || "Imported design" ;
2026-06-06 23:10:29 +03:00
}
export default function Home() {
const [ mounted , setMounted ] = useState ( false );
const [ view , setView ] = useState < AppView >( "dashboard" );
const [ editorStarted , setEditorStarted ] = useState ( false );
const [ activeProjectId , setActiveProjectId ] = useState < string | null >( null );
const [ projects , setProjects ] = useState < DashboardProject [] >([]);
2026-07-05 12:28:23 +03:00
const [ dashboardSection , setDashboardSection ] = useState < DashboardSection >( "home" );
2026-06-06 23:10:29 +03:00
const [ query , setQuery ] = useState ( "" );
const [ viewMode , setViewMode ] = useState < ViewMode >( "grid" );
const [ sortMode , setSortMode ] = useState ( "recent" );
const [ settingsOpen , setSettingsOpen ] = useState ( false );
const [ downloadMode , setDownloadMode ] = useState < DownloadMode >( "browser" );
const [ downloadFolder , setDownloadFolder ] = useState ( "" );
const [ dashboardNotice , setDashboardNotice ] = useState ( "" );
2026-07-19 15:58:52 +03:00
const [ sharedProjects , setSharedProjects ] = useState < SharedProject [] >([]);
const [ sharedProjectsEnabled , setSharedProjectsEnabled ] = useState ( false );
const [ sharedProjectsLoading , setSharedProjectsLoading ] = useState ( false );
2026-06-06 23:10:29 +03:00
const [ projectShapesById , setProjectShapesById ] = useState < Record < string , ProjectShapeCacheEntry >>({});
const projectsJsonRef = useRef ( "" );
2026-07-07 23:59:36 +09:00
const dashboardImportInputRef = useRef < HTMLInputElement | null >( null );
2026-06-14 22:05:58 +03:00
const nextProjectRevisionRef = useRef ( 0 );
const projectShapeSaveQueuesRef = useRef < Record < string , Promise < void >>>({});
2026-06-06 23:10:29 +03:00
2026-07-19 15:58:52 +03:00
const refreshSharedProjects = useCallback ( async () => {
if ( STATIC_EXPORT_BUILD ) return ;
setSharedProjectsLoading ( true );
try {
const response = await fetch ( "/api/shared-projects" , { cache : "no-store" });
const payload = await response . json () as { enabled? : boolean ; projects? : SharedProject []; error? : string };
setSharedProjectsEnabled ( Boolean ( payload . enabled ));
setSharedProjects ( Array . isArray ( payload . projects ) ? payload . projects : []);
if ( ! response . ok && payload . enabled ) setDashboardNotice ( payload . error ?? "Could not load shared projects" );
} catch {
setSharedProjectsEnabled ( false );
setSharedProjects ([]);
} finally {
setSharedProjectsLoading ( false );
}
}, []);
2026-06-06 23:10:29 +03:00
useEffect (() => {
2026-07-11 20:34:13 +03:00
document . documentElement . removeAttribute ( "data-theme" );
document . documentElement . style . colorScheme = "light" ;
try {
window . localStorage . removeItem ( "sketchForge.theme" );
} catch {
// Light mode still applies when browser storage is unavailable.
}
2026-06-06 23:10:29 +03:00
const { projects : storedProjects , legacyShapes } = readStoredProjects ();
2026-06-12 22:00:17 +03:00
setProjects ( storedProjects );
2026-06-06 23:10:29 +03:00
if ( Object . keys ( legacyShapes ). length > 0 ) {
2026-06-12 22:00:17 +03:00
setProjectShapesById ( legacyShapes );
2026-06-06 23:10:29 +03:00
Object . entries ( legacyShapes ). forEach (([ projectId , entry ]) => {
2026-07-14 09:43:37 +03:00
void saveProjectShapes ( projectId , entry ). catch (() => {
2026-06-06 23:10:29 +03:00
setDashboardNotice ( "Could not migrate project shapes to larger storage" );
});
});
}
2026-06-14 22:05:58 +03:00
setDownloadMode ( ! STATIC_EXPORT_BUILD && window . localStorage . getItem ( DOWNLOAD_MODE_STORAGE_KEY ) === "folder" ? "folder" : "browser" );
2026-06-12 22:00:17 +03:00
setDownloadFolder ( window . localStorage . getItem ( DOWNLOAD_FOLDER_STORAGE_KEY ) ?? "" );
2026-06-06 23:10:29 +03:00
const params = new URLSearchParams ( window . location . search );
2026-06-12 22:00:17 +03:00
if ( params . has ( "codexBooleanCase" ) || params . get ( "editor" ) === "1" ) {
2026-06-06 23:10:29 +03:00
const requestedProjectId = params . get ( "project" );
if ( requestedProjectId && storedProjects . some (( project ) => project . id === requestedProjectId )) {
setActiveProjectId ( requestedProjectId );
}
setEditorStarted ( true );
setView ( "editor" );
}
setMounted ( true );
}, []);
2026-07-19 15:58:52 +03:00
useEffect (() => {
if ( ! mounted ) return ;
void refreshSharedProjects ();
}, [ mounted , refreshSharedProjects ]);
2026-06-06 23:10:29 +03:00
useEffect (() => {
if ( ! mounted ) return ;
const localSerialized = JSON . stringify ( projects );
const storageProjects = mergeProjectsForStorage ( projects );
const serialized = JSON . stringify ( storageProjects );
if ( projectsJsonRef . current === serialized ) return ;
try {
window . localStorage . setItem ( PROJECTS_STORAGE_KEY , serialized );
} catch ( error ) {
try {
window . localStorage . removeItem ( PROJECTS_STORAGE_KEY );
window . localStorage . setItem ( PROJECTS_STORAGE_KEY , serialized );
} catch {
setDashboardNotice ( error instanceof Error ? error . message : "Could not save project list" );
return ;
}
}
projectsJsonRef . current = serialized ;
if ( serialized !== localSerialized ) {
setProjects ( storageProjects );
}
}, [ mounted , projects ]);
useEffect (() => {
if ( ! mounted ) return ;
const onStorage = ( event : StorageEvent ) => {
if ( event . key !== PROJECTS_STORAGE_KEY ) return ;
projectsJsonRef . current = event . newValue ?? "[]" ;
setProjects ( readProjects ());
};
window . addEventListener ( "storage" , onStorage );
return () => window . removeEventListener ( "storage" , onStorage );
}, [ mounted ]);
useEffect (() => {
if ( ! activeProjectId ) return ;
if ( projects . some (( project ) => project . id === activeProjectId )) return ;
setActiveProjectId ( null );
setView ( "dashboard" );
if ( typeof window !== "undefined" ) {
window . history . replaceState ( null , "" , "/" );
}
}, [ activeProjectId , projects ]);
useEffect (() => {
if ( ! mounted || ! activeProjectId ) return ;
const activeProject = projects . find (( project ) => project . id === activeProjectId );
if ( ! activeProject ) return ;
const cached = projectShapesById [ activeProjectId ];
if ( cached && cached . revision === activeProject . revision ) return ;
let canceled = false ;
void loadProjectShapes ( activeProjectId )
. then (( record ) => {
if ( canceled ) return ;
const revision = activeProject . revision ?? record ? . revision ?? Date . now ();
2026-07-16 12:30:39 +03:00
const entry = projectShapeCacheEntry ( revision , record ? . shapes ?? [], record ? . history , record ? . historyIndex , record ? . assets );
2026-06-06 23:10:29 +03:00
setProjectShapesById (( current ) => ({
... current ,
2026-07-14 09:43:37 +03:00
[ activeProjectId ] : entry ,
2026-06-06 23:10:29 +03:00
}));
})
. catch (( error ) => {
if ( ! canceled ) {
setDashboardNotice ( error instanceof Error ? error . message : "Could not load project shapes" );
setProjectShapesById (( current ) => ({
... current ,
2026-07-14 09:43:37 +03:00
[ activeProjectId ] : projectShapeCacheEntry ( activeProject . revision ?? Date . now (), []),
2026-06-06 23:10:29 +03:00
}));
}
});
return () => {
canceled = true ;
};
}, [ activeProjectId , mounted , projectShapesById , projects ]);
useEffect (() => {
if ( ! mounted ) return ;
window . localStorage . setItem ( DOWNLOAD_MODE_STORAGE_KEY , downloadMode );
window . localStorage . setItem ( DOWNLOAD_FOLDER_STORAGE_KEY , downloadFolder );
}, [ downloadFolder , downloadMode , mounted ]);
const visibleProjects = useMemo (() => {
const normalizedQuery = query . trim (). toLowerCase ();
const filtered = normalizedQuery ? projects . filter (( project ) => project . name . toLowerCase (). includes ( normalizedQuery )) : projects ;
return sortMode === "name" ? [... filtered ]. sort (( a , b ) => a . name . localeCompare ( b . name )) : [... filtered ]. sort (( a , b ) => b . updatedAt - a . updatedAt );
}, [ projects , query , sortMode ]);
2026-07-19 15:58:52 +03:00
const visibleSharedProjects = useMemo (() => {
const normalizedQuery = query . trim (). toLowerCase ();
const filtered = normalizedQuery ? sharedProjects . filter (( project ) => project . name . toLowerCase (). includes ( normalizedQuery )) : sharedProjects ;
return sortMode === "name" ? [... filtered ]. sort (( a , b ) => a . name . localeCompare ( b . name )) : [... filtered ]. sort (( a , b ) => b . updatedAt - a . updatedAt );
}, [ query , sharedProjects , sortMode ]);
2026-06-06 23:10:29 +03:00
const openEditor = ( projectId : string | null , options : { allowMissingFromStorage? : boolean } = {}) => {
if ( projectId && typeof window !== "undefined" && ! options . allowMissingFromStorage ) {
const storedProjects = readProjects ();
const storedProject = storedProjects . find (( project ) => project . id === projectId );
if ( ! storedProject ) {
setProjects ( storedProjects );
setActiveProjectId ( null );
setView ( "dashboard" );
window . history . replaceState ( null , "" , "/" );
return ;
}
setProjects ( storedProjects . map (( project ) => ( project . id === projectId ? { ... project , updatedAt : Date.now () } : project )));
} else if ( projectId ) {
setProjects (( current ) => current . map (( project ) => ( project . id === projectId ? { ... project , updatedAt : Date.now () } : project )));
}
setActiveProjectId ( projectId );
setEditorStarted ( true );
setView ( "editor" );
if ( typeof window !== "undefined" ) {
const nextUrl = projectId ? `/?editor=1&project= ${ encodeURIComponent ( projectId ) } ` : "/?editor=1" ;
window . history . replaceState ( null , "" , nextUrl );
}
};
const updateProjectSnapshot = useCallback (( snapshot : { image : string ; projectId : string ; shapes : number }) => {
const version = Date . now ();
2026-06-14 22:05:58 +03:00
if ( STATIC_EXPORT_BUILD ) {
setProjects (( current ) =>
current . map (( project ) =>
project . id === snapshot . projectId
? { ... project , shapes : snapshot.shapes , thumbnailUrl : snapshot.image , thumbnailVersion : version , updatedAt : version }
: project ,
),
);
return ;
}
2026-06-06 23:10:29 +03:00
setProjects (( current ) =>
current . map (( project ) => ( project . id === snapshot . projectId ? { ... project , shapes : snapshot.shapes , updatedAt : version } : project )),
);
void fetch ( "/api/project-thumbnail" , {
method : "POST" ,
headers : { "Content-Type" : "application/json" },
body : JSON.stringify ({ dataUrl : snapshot.image , projectId : snapshot.projectId }),
})
. then (( response ) => ( response . ok ? response . json () : null ))
. then (( payload : { version? : number } | null ) => {
const nextVersion = payload ? . version ?? Date . now ();
const thumbnailUrl = `/api/project-thumbnail?projectId= ${ encodeURIComponent ( snapshot . projectId ) } &v= ${ nextVersion } ` ;
setProjects (( current ) =>
current . map (( project ) =>
project . id === snapshot . projectId
? { ... project , shapes : snapshot.shapes , thumbnailUrl , thumbnailVersion : nextVersion , updatedAt : nextVersion }
: project ,
),
);
})
. catch (() => {
setProjects (( current ) =>
current . map (( project ) => ( project . id === snapshot . projectId ? { ... project , shapes : snapshot.shapes , updatedAt : version } : project )),
);
});
}, []);
2026-07-14 09:43:37 +03:00
const updateProjectShapes = useCallback (( snapshot : {
projectId : string ;
shapes : WorkplaneShape [];
history : EditorHistoryEntry [];
historyIndex : number ;
2026-07-16 12:30:39 +03:00
assets : ProjectAsset [];
2026-07-14 09:43:37 +03:00
}) => {
2026-06-14 22:05:58 +03:00
const revision = Math . max ( Date . now (), nextProjectRevisionRef . current + 1 );
nextProjectRevisionRef . current = revision ;
2026-07-16 12:30:39 +03:00
const entry = projectShapeCacheEntry ( revision , snapshot . shapes , snapshot . history , snapshot . historyIndex , snapshot . assets );
2026-06-14 22:05:58 +03:00
setProjectShapesById (( current ) => {
const existing = current [ snapshot . projectId ];
if ( existing && existing . revision > revision ) {
return current ;
}
return {
... current ,
2026-07-14 09:43:37 +03:00
[ snapshot . projectId ] : entry ,
2026-06-14 22:05:58 +03:00
};
});
const previousSave = projectShapeSaveQueuesRef . current [ snapshot . projectId ] ?? Promise . resolve ();
2026-07-14 09:43:37 +03:00
const queuedSave = previousSave . catch (() => undefined ). then (() => saveProjectShapes ( snapshot . projectId , entry ));
2026-06-14 22:05:58 +03:00
projectShapeSaveQueuesRef . current [ snapshot . projectId ] = queuedSave ;
void queuedSave
2026-06-06 23:10:29 +03:00
. then (() => {
setProjects (( current ) =>
current . map (( project ) =>
2026-06-14 22:05:58 +03:00
project . id === snapshot . projectId && ( project . revision ?? 0 ) <= revision
2026-06-06 23:10:29 +03:00
? { ... project , shapes : snapshot.shapes.length , updatedAt : revision , revision }
: project ,
),
);
})
. catch (( error ) => {
2026-06-14 22:05:58 +03:00
if ( projectShapeSaveQueuesRef . current [ snapshot . projectId ] === queuedSave ) {
setDashboardNotice ( error instanceof Error ? error . message : "Could not save project shapes" );
}
})
. finally (() => {
if ( projectShapeSaveQueuesRef . current [ snapshot . projectId ] === queuedSave ) {
delete projectShapeSaveQueuesRef . current [ snapshot . projectId ];
}
2026-06-06 23:10:29 +03:00
});
}, []);
2026-07-16 12:30:39 +03:00
const updateProjectWorkspace = useCallback (( snapshot : { projectId : string ; workspace : WorkplaneWorkspaceSettings ; snap : GridSize ; placementElevation? : number }) => {
2026-06-13 10:26:40 +03:00
const version = Date . now ();
const workspace = normalizeWorkspaceSettings ( snapshot . workspace );
const snapGrid = normalizeSnapGrid ( snapshot . snap );
2026-07-16 12:30:39 +03:00
const placementElevation = typeof snapshot . placementElevation === "number" && Number . isFinite ( snapshot . placementElevation )
? snapshot.placementElevation
: 0 ;
const nextFingerprint = ` ${ workplaneSettingsFingerprint ( workspace , snapGrid ) } : ${ placementElevation } ` ;
2026-07-11 20:34:13 +03:00
setProjects (( current ) => {
let changed = false ;
const next = current . map (( project ) => {
if ( project . id !== snapshot . projectId ) return project ;
2026-07-16 12:30:39 +03:00
const currentFingerprint = ` ${ workplaneSettingsFingerprint (
2026-07-11 20:34:13 +03:00
normalizeWorkspaceSettings ( project . workspace ),
normalizeSnapGrid ( project . snapGrid ),
2026-07-16 12:30:39 +03:00
) } : ${ project . placementElevation ?? 0 } ` ;
2026-07-11 20:34:13 +03:00
if ( currentFingerprint === nextFingerprint ) return project ;
changed = true ;
return {
... project ,
workspace ,
snapGrid ,
2026-07-16 12:30:39 +03:00
placementElevation ,
2026-07-11 20:34:13 +03:00
updatedAt : version ,
};
});
return changed ? next : current ;
});
2026-06-13 10:26:40 +03:00
}, []);
2026-06-06 23:10:29 +03:00
const createAndOpenProject = ( name? : string ) => {
const project = newProject ( name ?? `Untitled design ${ projects . length + 1 } ` , projects . length );
setProjectShapesById (( current ) => ({
... current ,
2026-07-14 09:43:37 +03:00
[ project . id ] : projectShapeCacheEntry ( project . revision ?? project . updatedAt , []),
2026-06-06 23:10:29 +03:00
}));
2026-07-14 09:43:37 +03:00
void saveProjectShapes ( project . id , projectShapeCacheEntry ( project . revision ?? project . updatedAt , [])). catch (() => {
2026-06-06 23:10:29 +03:00
setDashboardNotice ( "Could not prepare project shape storage" );
});
setProjects (( current ) => [ project , ... current ]);
openEditor ( project . id , { allowMissingFromStorage : true });
};
2026-07-19 15:58:52 +03:00
const openSkfProjectFromFile = useCallback ( async ( file : File , sharedProject? : SharedProject ) => {
2026-07-16 12:30:39 +03:00
setDashboardNotice ( `Validating ${ file . name } before opening it as a new project` );
try {
const restored = await importSkfProject ( await file . arrayBuffer ());
const now = Date . now ();
const project : DashboardProject = {
... newProject ( restored . projectName , projects . length , restored . shapes . length ),
createdAt : restored.createdAt ,
updatedAt : now ,
revision : now ,
workspace : restored.workspace ,
snapGrid : restored.snapGrid ,
placementElevation : restored.placementElevation ,
2026-07-19 15:58:52 +03:00
sharedProject : sharedProject ? { fileName : sharedProject.fileName , revision : sharedProject.revision } : undefined ,
2026-07-16 12:30:39 +03:00
};
const entry = projectShapeCacheEntry ( now , restored . shapes , restored . history , restored . historyIndex , restored . assets );
await saveProjectShapes ( project . id , entry );
setProjectShapesById (( current ) => ({ ... current , [ project . id ] : entry }));
setProjects (( current ) => [ project , ... current ]);
2026-07-19 15:58:52 +03:00
setDashboardNotice ( sharedProject ? `Opened shared project ${ sharedProject . name } ; edits autosave locally until you save back to shared` : `Opened ${ file . name } as a new editable local project` );
2026-07-16 12:30:39 +03:00
openEditor ( project . id , { allowMissingFromStorage : true });
2026-07-19 15:58:52 +03:00
return { ok : true , message : sharedProject ? `Opened shared project ${ sharedProject . name } ` : `Opened ${ file . name } as a new editable local project` };
2026-07-16 12:30:39 +03:00
} catch ( error ) {
const message = error instanceof Error ? error . message : "Could not open SketchForge project" ;
setDashboardNotice ( message );
return { ok : false , message };
}
}, [ projects . length ]);
2026-07-19 15:58:52 +03:00
const openSharedProject = useCallback ( async ( sharedProject : SharedProject ) => {
setDashboardNotice ( `Opening shared project ${ sharedProject . name } ` );
try {
const response = await fetch ( `/api/shared-projects?fileName= ${ encodeURIComponent ( sharedProject . fileName ) } ` , { cache : "no-store" });
if ( ! response . ok ) {
const payload = await response . json (). catch (() => ({})) as { error? : string };
throw new Error ( payload . error ?? "Could not download shared project" );
}
const revision = response . headers . get ( "etag" ) ? . replace ( /^W\// , "" ). replace ( /^"|"$/g , "" ) || sharedProject . revision ;
const file = new File ([ await response . blob ()], sharedProject . fileName , { type : "application/vnd.sketchforge.project+zip" });
await openSkfProjectFromFile ( file , { ... sharedProject , revision });
} catch ( error ) {
setDashboardNotice ( error instanceof Error ? error . message : "Could not open shared project" );
}
}, [ openSkfProjectFromFile ]);
const saveActiveProjectToShared = useCallback ( async ({ exportName , bytes } : { exportName : string ; bytes : Uint8Array }) => {
const activeProject = projects . find (( project ) => project . id === activeProjectId );
if ( ! activeProject ) throw new Error ( "Open a local project before saving it to the shared space" );
const normalizedExportName = exportName . trim () || activeProject . name ;
const saveBackToSource = Boolean ( activeProject . sharedProject && normalizedExportName === activeProject . name );
const fileName = saveBackToSource && activeProject . sharedProject
? activeProject . sharedProject . fileName
: ` ${ normalizedExportName . replace ( /\.skf$/i , "" ) } .skf` ;
const headers : Record < string , string > = { "Content-Type" : "application/vnd.sketchforge.project+zip" };
if ( saveBackToSource && activeProject . sharedProject ) headers [ "If-Match" ] = `" ${ activeProject . sharedProject . revision } "` ;
else headers [ "If-None-Match" ] = "*" ;
const body = bytes . buffer . slice ( bytes . byteOffset , bytes . byteOffset + bytes . byteLength ) as ArrayBuffer ;
const response = await fetch ( `/api/shared-projects?fileName= ${ encodeURIComponent ( fileName ) } ` , { method : "POST" , headers , body });
const payload = await response . json (). catch (() => ({})) as { error? : string ; project? : SharedProject };
if ( ! response . ok || ! payload . project ) throw new Error ( payload . error ?? "Could not save the shared project" );
const savedProject = payload . project ;
setProjects (( current ) => current . map (( project ) => project . id === activeProject . id
? { ... project , sharedProject : { fileName : savedProject.fileName , revision : savedProject.revision } }
: project ));
await refreshSharedProjects ();
return `Saved ${ savedProject . name } to the Docker shared project space` ;
}, [ activeProjectId , projects , refreshSharedProjects ]);
2026-07-16 12:30:39 +03:00
const importFilesFromDashboard = useCallback (
async ( files : File []) => {
if ( ! files . length ) return ;
const projectFiles = files . filter (( file ) => /\.skf$/i . test ( file . name ));
if ( projectFiles . length ) {
if ( files . length !== 1 ) {
setDashboardNotice ( "Open one .skf project at a time; import STL, STEP, and SVG geometry separately" );
return ;
}
await openSkfProjectFromFile ( projectFiles [ 0 ]);
return ;
}
const importedShapes : WorkplaneShape [] = [];
const importedAssets : ProjectAsset [] = [];
const importedFileNames : string [] = [];
const failures : Array < { fileName : string ; reason : string } > = [];
for ( let index = 0 ; index < files . length ; index += 1 ) {
const file = files [ index ];
const sourceFormat = sourceFormatForFileName ( file . name ) ?? ( file . type === "image/svg+xml" ? "svg" : null );
const isSvg = sourceFormat === "svg" ;
const isStep = sourceFormat === "step" ;
if ( ! sourceFormat || sourceFormat === "obj" || ( ! isSvg && ! isStep && ! importExtensionSupported ( file . name ))) {
failures . push ({ fileName : file.name , reason : "Unsupported file type" });
continue ;
}
setDashboardNotice ( `Importing ${ index + 1 } of ${ files . length } : ${ file . name } ` );
try {
const bytes = new Uint8Array ( await file . arrayBuffer ());
const buffer = bytes . buffer . slice ( bytes . byteOffset , bytes . byteOffset + bytes . byteLength ) as ArrayBuffer ;
const parsedShape = isStep
? await import ( "@/lib/stepImport" ). then (({ importedShapeFromStep }) => importedShapeFromStep ( file . name , buffer ))
: isSvg
? importedShapeFromSvg ( file . name , new TextDecoder (). decode ( bytes ))
: importedShapeFromStl ( file . name , buffer );
const asset = await projectAssetFromBytes ( file . name , sourceFormat , bytes , file . type );
importedShapes . push ( attachProjectAsset ( parsedShape , asset . id ));
importedAssets . push ( asset );
importedFileNames . push ( file . name );
} catch ( error ) {
failures . push ({
fileName : file.name ,
reason : error instanceof Error ? error . message : "Could not read file" ,
});
}
}
const failureDetails = failures
. slice ( 0 , 3 )
. map (( failure ) => ` ${ failure . fileName } : ${ failure . reason } ` )
. join ( "; " );
const remainingFailureCount = Math . max ( 0 , failures . length - 3 );
const failureSummary = failures . length
? ` Failed: ${ failureDetails }${ remainingFailureCount ? `; plus ${ remainingFailureCount } more` : "" } `
: "" ;
if ( ! importedShapes . length ) {
setDashboardNotice ( files . length === 1 && failures [ 0 ] ? failures [ 0 ]. reason : `Could not import any of the ${ files . length } selected files. ${ failureSummary } ` );
2026-06-06 23:10:29 +03:00
return ;
}
try {
2026-07-16 12:30:39 +03:00
const projectName = importedShapes . length === 1
? projectNameFromFileName ( importedFileNames [ 0 ])
: `Imported design ( ${ importedShapes . length } files)` ;
const project = newProject ( projectName , projects . length , importedShapes . length );
2026-06-06 23:10:29 +03:00
const revision = project . revision ?? project . updatedAt ;
2026-07-16 12:30:39 +03:00
const entry = projectShapeCacheEntry ( revision , importedShapes , undefined , undefined , dedupeProjectAssets ( importedAssets ));
2026-07-14 09:43:37 +03:00
await saveProjectShapes ( project . id , entry );
2026-06-06 23:10:29 +03:00
setProjectShapesById (( current ) => ({
... current ,
2026-07-14 09:43:37 +03:00
[ project . id ] : entry ,
2026-06-06 23:10:29 +03:00
}));
2026-07-16 12:30:39 +03:00
const successSummary = importedShapes . length === 1 && files . length === 1
? `Imported ${ files [ 0 ]. name } `
: `Imported ${ importedShapes . length } of ${ files . length } files` ;
setDashboardNotice ( ` ${ successSummary } . ${ failureSummary } ` . trim ());
2026-06-06 23:10:29 +03:00
setProjects (( current ) => [ project , ... current ]);
openEditor ( project . id , { allowMissingFromStorage : true });
} catch ( error ) {
2026-07-16 12:30:39 +03:00
setDashboardNotice ( error instanceof Error ? error . message : "Could not create a project for the imported files" );
2026-06-06 23:10:29 +03:00
}
},
2026-07-16 12:30:39 +03:00
[ openSkfProjectFromFile , projects . length ],
2026-06-06 23:10:29 +03:00
);
const openLatestProject = () => {
const latest = [... projects ]. sort (( a , b ) => b . updatedAt - a . updatedAt )[ 0 ];
if ( latest ) {
openEditor ( latest . id );
return ;
}
createAndOpenProject ();
};
const openDashboard = () => {
if ( activeProjectId ) {
setProjects (( current ) => current . map (( project ) => ( project . id === activeProjectId ? { ... project , updatedAt : Date.now () } : project )));
}
2026-07-05 12:28:23 +03:00
setDashboardSection ( "home" );
2026-06-06 23:10:29 +03:00
setView ( "dashboard" );
if ( typeof window !== "undefined" ) {
window . history . replaceState ( null , "" , "/" );
}
};
const deleteProject = ( projectId : string ) => {
setProjects (( current ) => current . filter (( project ) => project . id !== projectId ));
setProjectShapesById (( current ) => {
const next = { ... current };
delete next [ projectId ];
return next ;
});
if ( activeProjectId === projectId ) {
setActiveProjectId ( null );
}
2026-06-14 22:05:58 +03:00
if ( ! STATIC_EXPORT_BUILD ) {
void fetch ( `/api/project-thumbnail?projectId= ${ encodeURIComponent ( projectId ) } ` , { method : "DELETE" });
}
2026-06-06 23:10:29 +03:00
void deleteProjectShapes ( projectId ). catch (() => {
setDashboardNotice ( "Could not delete project shapes from local storage" );
});
};
2026-06-20 10:07:33 +03:00
const renameProject = ( projectId : string , name : string ) => {
const nextName = name . trim (). slice ( 0 , 80 );
if ( ! nextName ) return ;
setProjects (( current ) =>
current . map (( project ) => ( project . id === projectId ? { ... project , name : nextName , updatedAt : Date.now () } : project )),
);
};
2026-06-06 23:10:29 +03:00
if ( ! mounted ) {
return null ;
}
const activeProject = activeProjectId ? projects . find (( project ) => project . id === activeProjectId ) ?? null : null ;
const activeProjectShapeEntry = activeProjectId ? projectShapesById [ activeProjectId ] : null ;
const canRenderEditor = ! activeProjectId || ( Boolean ( activeProject ) && Boolean ( activeProjectShapeEntry ));
const projectDebugSummary = projects . map (( project ) => ({
id : project.id ,
revision : project.revision ,
shapes : project.shapes ,
designShapes : projectShapesById [ project . id ] ? . shapes . length ?? null ,
thumbnail : Boolean ( project . thumbnailUrl ),
2026-06-13 10:26:40 +03:00
workspace : Boolean ( project . workspace ),
snapGrid : project.snapGrid ?? null ,
2026-06-06 23:10:29 +03:00
}));
return (
<>
< pre data - codex - projects hidden >
{ JSON . stringify ( projectDebugSummary )}
</ pre >
< input
2026-07-07 23:59:36 +09:00
ref = { dashboardImportInputRef }
2026-06-06 23:10:29 +03:00
className = "hidden-file-input"
type = "file"
2026-07-16 12:30:39 +03:00
multiple
accept = ".skf,.stl,.step,.stp,.svg,image/svg+xml"
2026-06-06 23:10:29 +03:00
onChange = {( event ) => {
2026-07-16 12:30:39 +03:00
const files = event . currentTarget . files ? Array . from ( event . currentTarget . files ) : [];
if ( files . length ) {
void importFilesFromDashboard ( files );
2026-06-06 23:10:29 +03:00
}
event . currentTarget . value = "" ;
}}
/>
2026-06-12 22:00:17 +03:00
{ view === "dashboard" ? (
2026-06-06 23:10:29 +03:00
< Dashboard
2026-07-05 12:28:23 +03:00
dashboardSection = { dashboardSection }
2026-06-06 23:10:29 +03:00
dashboardNotice = { dashboardNotice }
downloadFolder = { downloadFolder }
downloadMode = { downloadMode }
projects = { visibleProjects }
query = { query }
settingsOpen = { settingsOpen }
2026-07-19 15:58:52 +03:00
sharedProjects = { visibleSharedProjects }
sharedProjectsEnabled = { sharedProjectsEnabled }
sharedProjectsLoading = { sharedProjectsLoading }
2026-06-14 22:05:58 +03:00
staticExportBuild = { STATIC_EXPORT_BUILD }
2026-06-06 23:10:29 +03:00
sortMode = { sortMode }
viewMode = { viewMode }
onCloseSettings = {() => setSettingsOpen ( false )}
onCreate = {() => createAndOpenProject ()}
onDeleteProject = { deleteProject }
onDownloadFolderChange = { setDownloadFolder }
onDownloadModeChange = { setDownloadMode }
2026-07-07 23:59:36 +09:00
onImportFile = {() => dashboardImportInputRef . current ? . click ()}
2026-07-05 12:28:23 +03:00
onChallenges = {() => {
setDashboardSection ( "challenges" );
setDashboardNotice ( "" );
}}
onDashboardHome = {() => setDashboardSection ( "home" )}
2026-07-19 15:58:52 +03:00
onOpenSharedProject = {( project ) => void openSharedProject ( project )}
2026-06-06 23:10:29 +03:00
onOpenProject = { openEditor }
onOpenSettings = {() => setSettingsOpen ( true )}
onQueryChange = { setQuery }
2026-06-20 10:07:33 +03:00
onRenameProject = { renameProject }
2026-07-19 15:58:52 +03:00
onRefreshSharedProjects = {() => void refreshSharedProjects ()}
onSharedProjects = {() => {
setDashboardSection ( "shared" );
setDashboardNotice ( "" );
void refreshSharedProjects ();
}}
2026-06-06 23:10:29 +03:00
onSortModeChange = { setSortMode }
onViewModeChange = { setViewMode }
onWorkspace = { openLatestProject }
/>
) : null }
{ editorStarted && canRenderEditor ? (
< div className = { view === "editor" ? "editor-stage active" : "editor-stage" } aria-hidden = { view !== "editor" }>
< SketchForgeEditor
2026-07-16 12:30:39 +03:00
initialAssets = { activeProjectShapeEntry ? . assets ?? []}
2026-06-06 23:10:29 +03:00
initialShapes = { activeProjectShapeEntry ? . shapes ?? []}
2026-07-14 09:43:37 +03:00
initialHistory = { activeProjectShapeEntry ? . history }
initialHistoryIndex = { activeProjectShapeEntry ? . historyIndex }
2026-06-13 10:26:40 +03:00
initialSnap = { activeProject ? . snapGrid ?? DEFAULT_SNAP_GRID }
initialWorkspace = { activeProject ? . workspace ?? DEFAULT_WORKPLANE_WORKSPACE }
2026-07-16 12:30:39 +03:00
initialPlacementElevation = { activeProject ? . placementElevation ?? 0 }
2026-06-12 22:00:17 +03:00
onHome = { openDashboard }
2026-07-16 12:30:39 +03:00
onOpenSkfProjectFile = { openSkfProjectFromFile }
2026-07-19 15:58:52 +03:00
onSaveSharedProject = { saveActiveProjectToShared }
2026-06-06 23:10:29 +03:00
onProjectShapesChange = { updateProjectShapes }
2026-06-12 22:00:17 +03:00
onProjectSnapshot = { updateProjectSnapshot }
2026-06-13 10:26:40 +03:00
onProjectWorkspaceChange = { updateProjectWorkspace }
2026-06-06 23:10:29 +03:00
projectId = { activeProjectId }
2026-06-20 10:07:33 +03:00
projectName = { activeProject ? . name }
2026-07-16 12:30:39 +03:00
projectCreatedAt = { activeProject ? . createdAt }
projectModifiedAt = { activeProject ? . updatedAt }
2026-06-06 23:10:29 +03:00
projectRevision = { activeProjectShapeEntry ? . revision ?? activeProject ? . revision ?? 0 }
2026-07-19 15:58:52 +03:00
sharedProjectsEnabled = { sharedProjectsEnabled }
2026-06-06 23:10:29 +03:00
/>
</ div >
) : null }
</>
);
}
function Dashboard ({
2026-07-05 12:28:23 +03:00
dashboardSection ,
2026-06-06 23:10:29 +03:00
dashboardNotice ,
downloadFolder ,
downloadMode ,
projects ,
query ,
settingsOpen ,
2026-07-19 15:58:52 +03:00
sharedProjects ,
sharedProjectsEnabled ,
sharedProjectsLoading ,
2026-06-14 22:05:58 +03:00
staticExportBuild ,
2026-06-06 23:10:29 +03:00
sortMode ,
viewMode ,
onCloseSettings ,
onCreate ,
onDeleteProject ,
onDownloadFolderChange ,
onDownloadModeChange ,
2026-07-07 23:59:36 +09:00
onImportFile ,
2026-07-05 12:28:23 +03:00
onChallenges ,
onDashboardHome ,
2026-07-19 15:58:52 +03:00
onOpenSharedProject ,
2026-06-06 23:10:29 +03:00
onOpenProject ,
onOpenSettings ,
onQueryChange ,
2026-06-20 10:07:33 +03:00
onRenameProject ,
2026-07-19 15:58:52 +03:00
onRefreshSharedProjects ,
onSharedProjects ,
2026-06-06 23:10:29 +03:00
onSortModeChange ,
onViewModeChange ,
onWorkspace ,
} : {
2026-07-05 12:28:23 +03:00
dashboardSection : DashboardSection ;
2026-06-06 23:10:29 +03:00
dashboardNotice : string ;
downloadFolder : string ;
downloadMode : DownloadMode ;
projects : DashboardProject [];
query : string ;
settingsOpen : boolean ;
2026-07-19 15:58:52 +03:00
sharedProjects : SharedProject [];
sharedProjectsEnabled : boolean ;
sharedProjectsLoading : boolean ;
2026-06-14 22:05:58 +03:00
staticExportBuild : boolean ;
2026-06-06 23:10:29 +03:00
sortMode : string ;
viewMode : ViewMode ;
onCloseSettings : () => void ;
onCreate : () => void ;
onDeleteProject : ( projectId : string ) => void ;
onDownloadFolderChange : ( value : string ) => void ;
onDownloadModeChange : ( value : DownloadMode ) => void ;
2026-07-07 23:59:36 +09:00
onImportFile : () => void ;
2026-07-05 12:28:23 +03:00
onChallenges : () => void ;
onDashboardHome : () => void ;
2026-07-19 15:58:52 +03:00
onOpenSharedProject : ( project : SharedProject ) => void ;
2026-06-06 23:10:29 +03:00
onOpenProject : ( projectId : string ) => void ;
onOpenSettings : () => void ;
onQueryChange : ( value : string ) => void ;
2026-06-20 10:07:33 +03:00
onRenameProject : ( projectId : string , name : string ) => void ;
2026-07-19 15:58:52 +03:00
onRefreshSharedProjects : () => void ;
onSharedProjects : () => void ;
2026-06-06 23:10:29 +03:00
onSortModeChange : ( value : string ) => void ;
onViewModeChange : ( value : ViewMode ) => void ;
onWorkspace : () => void ;
}) {
2026-06-20 10:07:33 +03:00
const [ openProjectMenuId , setOpenProjectMenuId ] = useState < string | null >( null );
2026-06-06 23:10:29 +03:00
const [ projectPendingDeleteId , setProjectPendingDeleteId ] = useState < string | null >( null );
2026-06-20 10:07:33 +03:00
const [ projectPendingRenameId , setProjectPendingRenameId ] = useState < string | null >( null );
const [ projectNameDraft , setProjectNameDraft ] = useState ( "" );
2026-06-06 23:10:29 +03:00
const projectPendingDelete = projects . find (( project ) => project . id === projectPendingDeleteId ) ?? null ;
2026-06-20 10:07:33 +03:00
const projectPendingRename = projects . find (( project ) => project . id === projectPendingRenameId ) ?? null ;
2026-06-06 23:10:29 +03:00
useEffect (() => {
if ( ! projectPendingDeleteId ) return ;
if ( projects . some (( project ) => project . id === projectPendingDeleteId )) return ;
setProjectPendingDeleteId ( null );
}, [ projectPendingDeleteId , projects ]);
const confirmProjectDelete = () => {
if ( ! projectPendingDelete ) return ;
onDeleteProject ( projectPendingDelete . id );
setProjectPendingDeleteId ( null );
};
2026-06-20 10:07:33 +03:00
const startProjectRename = ( project : DashboardProject ) => {
setOpenProjectMenuId ( null );
setProjectPendingRenameId ( project . id );
setProjectNameDraft ( project . name );
};
const closeProjectRename = () => {
setProjectPendingRenameId ( null );
setProjectNameDraft ( "" );
};
const confirmProjectRename = () => {
if ( ! projectPendingRename || ! projectNameDraft . trim ()) return ;
onRenameProject ( projectPendingRename . id , projectNameDraft );
closeProjectRename ();
};
2026-06-06 23:10:29 +03:00
return (
< main className = "dashboard-shell" >
< header className = "dashboard-topbar" >
< button className = "dashboard-brand" type = "button" aria-label = "Home" >
< img src = "assets/sketchforge/sketchforge-logo.png" alt = "" />
< span > SketchForge </ span >
</ button >
< div className = "dashboard-search" >
< Search size = { 18 } strokeWidth = { 2.4 } />
< input value = { query } onChange = {( event ) => onQueryChange ( event . currentTarget . value )} placeholder = "Search projects" aria-label = "Search projects" />
</ div >
< button className = "dashboard-primary" type = "button" onClick = { onCreate }>
< Plus size = { 20 } strokeWidth = { 2.6 } />
< span > Create </ span >
</ button >
</ header >
< div className = "dashboard-layout" >
< aside className = "dashboard-sidebar" >
< div className = "dashboard-nav-stack" >
2026-07-05 12:28:23 +03:00
< button className = { `dashboard-nav-item ${ dashboardSection === "home" ? "active" : "" } ` } type = "button" aria-label = "Home" title = "Home" onClick = { onDashboardHome }>
2026-06-06 23:10:29 +03:00
< HomeIcon size = { 20 } />
< span > Home </ span >
</ button >
2026-07-19 15:58:52 +03:00
{ sharedProjectsEnabled ? (
< button className = { `dashboard-nav-item ${ dashboardSection === "shared" ? "active" : "" } ` } type = "button" aria-label = "Shared projects" title = "Shared projects" onClick = { onSharedProjects }>
< FolderKanban size = { 20 } />
< span > Shared </ span >
</ button >
) : null }
2026-07-05 12:28:23 +03:00
< button className = { `dashboard-nav-item ${ dashboardSection === "challenges" ? "active" : "" } ` } type = "button" aria-label = "Challenges" title = "Challenges" onClick = { onChallenges }>
< SlidersHorizontal size = { 20 } />
< span > Challenges </ span >
</ button >
2026-06-06 23:10:29 +03:00
</ div >
< button className = "dashboard-nav-item dashboard-settings-button" type = "button" aria-label = "Download settings" title = "Download settings" onClick = { onOpenSettings }>
< Settings size = { 20 } />
< span > Settings </ span >
</ button >
</ aside >
2026-07-19 15:58:52 +03:00
< section className = "dashboard-main" aria-label = { dashboardSection === "challenges" ? "Challenges" : dashboardSection === "shared" ? "Shared projects" : "Dashboard" }>
2026-07-05 12:28:23 +03:00
{ dashboardSection === "challenges" ? (
< div className = "dashboard-coming-soon" role = "status" >
< strong > Coming soon </ strong >
2026-06-06 23:10:29 +03:00
</ div >
2026-07-19 15:58:52 +03:00
) : dashboardSection === "shared" ? (
<>
{ dashboardNotice ? < div className = "dashboard-import-notice" role = "status" >{ dashboardNotice }</ div > : null }
< div className = "dashboard-section-header shared-projects-header" >
< div >
< h1 > Shared projects </ h1 >
< span >{ sharedProjects . length } available from Docker storage </ span >
</ div >
< button className = "shared-projects-refresh" type = "button" onClick = { onRefreshSharedProjects } disabled = { sharedProjectsLoading }>
< RefreshCw size = { 16 } className = { sharedProjectsLoading ? "spinning" : undefined } />
< span > Refresh </ span >
</ button >
</ div >
{ sharedProjects . length > 0 ? (
< div className = { viewMode === "grid" ? "project-grid" : "project-list" }>
{ sharedProjects . map (( project , index ) => (
< article className = "project-card shared-project-card" key = { project . fileName }>
< button className = "project-card-open" type = "button" onClick = {() => onOpenSharedProject ( project )}>
< ProjectPreview accent = { PROJECT_ACCENTS [ index % PROJECT_ACCENTS . length ]} />
< span className = "project-card-title" >{ project . name }</ span >
< span className = "project-card-meta" >{ formatUpdated ( project . updatedAt )} - { formatFileSize ( project . size )}</ span >
</ button >
< span className = "shared-project-badge" > Shared </ span >
</ article >
))}
</ div >
) : (
< div className = "project-empty" >
< strong >{ sharedProjectsLoading ? "Loading shared projects" : "No shared projects yet" }</ strong >
< span > Save an SKF project to the shared space from the Export window .</ span >
</ div >
)}
</>
2026-06-06 23:10:29 +03:00
) : (
2026-07-05 12:28:23 +03:00
<>
< div className = "dashboard-actions-band" >
< button className = "dashboard-action-tile create" type = "button" onClick = { onCreate }>
< span className = "dashboard-action-icon" >
< Plus size = { 25 } strokeWidth = { 2.8 } />
</ span >
< span > Create new 3 D design </ span >
</ button >
2026-07-07 23:59:36 +09:00
< button className = "dashboard-action-tile" type = "button" onClick = { onImportFile }>
2026-07-05 12:28:23 +03:00
< span className = "dashboard-action-icon" >
< FileUp size = { 24 } strokeWidth = { 2.4 } />
</ span >
2026-07-16 12:30:39 +03:00
< span > Open SKF or import geometry </ span >
2026-07-05 12:28:23 +03:00
</ button >
< button className = "dashboard-action-tile" type = "button" onClick = { onWorkspace }>
< span className = "dashboard-action-icon" >
< Clock3 size = { 24 } strokeWidth = { 2.4 } />
</ span >
< span > Continue workplane </ span >
</ button >
</ div >
{ dashboardNotice ? (
< div className = "dashboard-import-notice" role = "status" >
{ dashboardNotice }
</ div >
) : null }
< div className = "dashboard-section-header" >
< div >
< h1 > Projects </ h1 >
< span >{ projects . length } visible </ span >
</ div >
< div className = "dashboard-controls" >
< label className = "dashboard-select" >
< SlidersHorizontal size = { 17 } />
< select value = { sortMode } onChange = {( event ) => onSortModeChange ( event . currentTarget . value )} aria-label = "Sort projects" >
< option value = "recent" > Recent </ option >
< option value = "name" > Name </ option >
</ select >
</ label >
< div className = "dashboard-segmented" aria-label = "Project view" >
< button className = { viewMode === "grid" ? "active" : "" } type = "button" aria-label = "Grid view" onClick = {() => onViewModeChange ( "grid" )}>
< Grid3X3 size = { 17 } />
</ button >
< button className = { viewMode === "list" ? "active" : "" } type = "button" aria-label = "List view" onClick = {() => onViewModeChange ( "list" )}>
< List size = { 18 } />
</ button >
</ div >
</ div >
</ div >
{ projects . length > 0 ? (
< div className = { viewMode === "grid" ? "project-grid" : "project-list" }>
{ projects . map (( project ) => (
< article className = "project-card" key = { project . id }>
< button className = "project-card-open" type = "button" onClick = {() => onOpenProject ( project . id )}>
< ProjectPreview accent = { project . accent } thumbnailUrl = { project . thumbnailUrl } />
< span className = "project-card-title" >{ project . name }</ span >
< span className = "project-card-meta" >
{ formatUpdated ( project . updatedAt )} - { project . shapes } shapes
</ span >
</ button >
< button
className = "project-menu-trigger"
type = "button"
aria-label = { `Project options for ${ project . name } ` }
aria-expanded = { openProjectMenuId === project . id }
title = "Project options"
onClick = {() => setOpenProjectMenuId (( current ) => ( current === project . id ? null : project . id ))}
>
< EllipsisVertical size = { 19 } strokeWidth = { 2.5 } />
</ button >
{ openProjectMenuId === project . id ? (
< div className = "project-card-menu" role = "menu" aria-label = { `Options for ${ project . name } ` }>
< button type = "button" role = "menuitem" onClick = {() => startProjectRename ( project )}>
< Pencil size = { 16 } />
< span > Rename </ span >
</ button >
< button
className = "delete"
type = "button"
role = "menuitem"
onClick = {() => {
setOpenProjectMenuId ( null );
setProjectPendingDeleteId ( project . id );
}}
>
< Trash2 size = { 16 } />
< span > Delete </ span >
</ button >
</ div >
) : null }
</ article >
))}
</ div >
) : (
< div className = "project-empty" >
< strong > No projects yet </ strong >
< span > Create a 3 D design and it will appear here .</ span >
</ div >
)}
</>
2026-06-06 23:10:29 +03:00
)}
</ section >
</ div >
{ projectPendingDelete ? (
< section className = "dashboard-confirm-overlay" role = "dialog" aria-modal = "true" aria-labelledby = "delete-project-title" >
< div className = "dashboard-confirm-dialog" >
< header >
< strong id = "delete-project-title" > Delete project ? </ strong >
< button type = "button" aria-label = "Cancel project deletion" onClick = {() => setProjectPendingDeleteId ( null )}>
< X size = { 18 } />
</ button >
</ header >
< p >
Do you actually want the project < span >{ projectPendingDelete . name }</ span > to be deleted ?
</ p >
< div className = "dashboard-confirm-actions" >
< button className = "dashboard-confirm-cancel" type = "button" onClick = {() => setProjectPendingDeleteId ( null )}>
Cancel
</ button >
< button className = "dashboard-confirm-delete" type = "button" onClick = { confirmProjectDelete }>
Delete
</ button >
</ div >
</ div >
</ section >
) : null }
2026-06-20 10:07:33 +03:00
{ projectPendingRename ? (
< section className = "dashboard-confirm-overlay" role = "dialog" aria-modal = "true" aria-labelledby = "rename-project-title" >
< form
className = "dashboard-confirm-dialog dashboard-rename-dialog"
onSubmit = {( event ) => {
event . preventDefault ();
confirmProjectRename ();
}}
>
< header >
< strong id = "rename-project-title" > Rename project </ strong >
< button type = "button" aria-label = "Cancel project rename" onClick = { closeProjectRename }>
< X size = { 18 } />
</ button >
</ header >
< label >
< span > Project name </ span >
< input
autoFocus
maxLength = { 80 }
value = { projectNameDraft }
onChange = {( event ) => setProjectNameDraft ( event . currentTarget . value )}
aria-label = "Project name"
/>
</ label >
< div className = "dashboard-confirm-actions" >
< button className = "dashboard-confirm-cancel" type = "button" onClick = { closeProjectRename }>
Cancel
</ button >
< button className = "dashboard-confirm-save" type = "submit" disabled = { ! projectNameDraft . trim ()}>
Save
</ button >
</ div >
</ form >
</ section >
) : null }
2026-06-06 23:10:29 +03:00
{ settingsOpen ? (
< section className = "dashboard-settings-panel" role = "dialog" aria-modal = "true" aria-label = "Download settings" >
< header >
< strong > Download settings </ strong >
< button type = "button" aria-label = "Close download settings" onClick = { onCloseSettings }>
< X size = { 18 } />
</ button >
</ header >
< label className = "dashboard-setting-row" >
< span > Save method </ span >
2026-06-14 22:05:58 +03:00
< select
value = { downloadMode }
onChange = {( event ) => onDownloadModeChange ( ! staticExportBuild && event . currentTarget . value === "folder" ? "folder" : "browser" )}
>
2026-06-06 23:10:29 +03:00
< option value = "browser" > Browser downloads </ option >
2026-06-14 22:05:58 +03:00
{ ! staticExportBuild ? < option value = "folder" > Save to folder </ option > : null }
2026-06-06 23:10:29 +03:00
</ select >
</ label >
< label className = "dashboard-setting-row" >
< span > Folder path </ span >
< input
2026-06-14 22:05:58 +03:00
disabled = { staticExportBuild || downloadMode !== "folder" }
2026-06-06 23:10:29 +03:00
value = { downloadFolder }
onChange = {( event ) => onDownloadFolderChange ( event . currentTarget . value )}
2026-07-19 16:44:45 +03:00
placeholder = "C:\\Users\\username\\Downloads"
2026-06-06 23:10:29 +03:00
/>
</ label >
</ section >
) : null }
</ main >
);
}
function ProjectPreview ({ accent , thumbnailUrl } : { accent : DashboardProject [ "accent" ]; thumbnailUrl? : string | null }) {
2026-06-15 12:31:22 +03:00
const [ failedThumbnailUrl , setFailedThumbnailUrl ] = useState < string | null >( null );
const showThumbnail = Boolean ( thumbnailUrl && thumbnailUrl !== failedThumbnailUrl );
useEffect (() => {
setFailedThumbnailUrl ( null );
}, [ thumbnailUrl ]);
2026-06-06 23:10:29 +03:00
return (
< span className = { `project-preview accent- ${ accent } ` } aria-hidden = "true" >
2026-06-15 12:31:22 +03:00
{ showThumbnail ? (
< img className = "project-thumbnail-image" src = { thumbnailUrl ?? "" } alt = "" onError = {() => setFailedThumbnailUrl ( thumbnailUrl ?? null )} />
2026-06-06 23:10:29 +03:00
) : (
<>
< span className = "preview-grid" />
< span className = "preview-empty-mark" > No snapshot yet </ span >
</>
)}
</ span >
);
}