diff --git a/src/features/guides/components/Frame.tsx b/src/features/guides/components/Frame.tsx index 686581f..c62362b 100644 --- a/src/features/guides/components/Frame.tsx +++ b/src/features/guides/components/Frame.tsx @@ -1,6 +1,7 @@ import "./Frame.css"; import type { FrameType, StepType } from "../../../lib/schemas"; import Step from "./Step"; +import { useEffect, useMemo } from "react"; export default function Frame({ frame, @@ -21,6 +22,15 @@ export default function Frame({ updateTitle: (frameId: string, title: string) => void; reOrder: (startIndex: number, endIndex: number) => void; }) { + const objectUrl = useMemo( + () => URL.createObjectURL(frame.file), + [frame.file], + ); + + useEffect(() => { + return () => URL.revokeObjectURL(objectUrl); + }, [objectUrl]); + return (
@@ -38,7 +48,6 @@ export default function Frame({ > Down -
- - {frame.title} + {frame.title} -
{frame.steps.map((step: StepType, index: number) => ( g.id === id); - +function GuideFormInner({ initial }: { initial?: CompiledGuideType }) { const { guide, frames, @@ -23,8 +20,7 @@ export default function GuideForm() { handleRemoveStep, handleUpdateStep, handleSaveGuide, - isEditing, - } = useGuideForm(g); + } = useGuideForm(initial); return (
@@ -46,13 +42,11 @@ export default function GuideForm() { onChange={(e) => handleUpdateDescription(e.target.value)} /> - - {frames.length > 0 && frames.map((frame, index) => ( ))} - {frames.length === 0 &&

No images added yet.

} - {frames.length > 0 && } -
); } + +export default function GuideForm() { + const { id } = useParams(); + const { guides, loading } = useGuides(); + + if (loading) return null; + + const initial = id ? guides.find((g) => g.id === id) : undefined; + + return ; +} diff --git a/src/lib/contexts/guide.context.tsx b/src/lib/contexts/guide.context.tsx index 24f5c94..d88d3ab 100644 --- a/src/lib/contexts/guide.context.tsx +++ b/src/lib/contexts/guide.context.tsx @@ -12,28 +12,30 @@ import { useIDB } from "./dbinit.context"; interface GuideContextValue { guides: CompiledGuideType[]; setGuides: React.Dispatch>; + loading: boolean; } const GuideContext = createContext(undefined); export function GuideProvider({ children }: { children: ReactNode }) { const [guides, setGuides] = useState([]); + const [loading, setLoading] = useState(true); const { db } = useIDB(); useEffect(() => { if (!db) return; db.getAll("guides").then((fetchedGuides) => { - const sortedGuides = [...fetchedGuides].sort((a, b) => { - return ( - new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime() - ); - }); + const sortedGuides = [...fetchedGuides].sort( + (a, b) => + new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(), + ); setGuides(sortedGuides); + setLoading(false); }); }, [db]); return ( - + {children} );