From 7bf0984698e43b9aa2dd879f65951318ed410390 Mon Sep 17 00:00:00 2001 From: Maze <167211895+mazeincoding@users.noreply.github.com> Date: Sat, 31 Jan 2026 00:20:04 +0100 Subject: codebase overhaul (#697) --- apps/web/src/components/storage-provider.tsx | 111 ++++++++++++++------------- 1 file changed, 56 insertions(+), 55 deletions(-) (limited to 'apps/web/src/components/storage-provider.tsx') diff --git a/apps/web/src/components/storage-provider.tsx b/apps/web/src/components/storage-provider.tsx index 69105d0f..144790dc 100644 --- a/apps/web/src/components/storage-provider.tsx +++ b/apps/web/src/components/storage-provider.tsx @@ -1,80 +1,81 @@ "use client"; -import { createContext, useContext, useEffect, useState } from "react"; -import { useProjectStore } from "@/stores/project-store"; -import { useMediaStore } from "@/stores/media-store"; -import { storageService } from "@/lib/storage/storage-service"; +import { createContext, useContext, useEffect, useRef, useState } from "react"; import { toast } from "sonner"; +import { useEditor } from "@/hooks/use-editor"; +import { storageService } from "@/services/storage/storage-service"; interface StorageContextType { - isInitialized: boolean; - isLoading: boolean; - hasSupport: boolean; - error: string | null; + isInitialized: boolean; + isLoading: boolean; + hasSupport: boolean; + error: string | null; } const StorageContext = createContext(null); export function useStorage() { - const context = useContext(StorageContext); - if (!context) { - throw new Error("useStorage must be used within StorageProvider"); - } - return context; + const context = useContext(StorageContext); + if (!context) { + throw new Error("useStorage must be used within StorageProvider"); + } + return context; } interface StorageProviderProps { - children: React.ReactNode; + children: React.ReactNode; } export function StorageProvider({ children }: StorageProviderProps) { - const [status, setStatus] = useState({ - isInitialized: false, - isLoading: true, - hasSupport: false, - error: null, - }); + const [status, setStatus] = useState({ + isInitialized: false, + isLoading: true, + hasSupport: false, + error: null, + }); - const loadAllProjects = useProjectStore((state) => state.loadAllProjects); + const editor = useEditor(); + const hasInitialized = useRef(false); - useEffect(() => { - const initializeStorage = async () => { - setStatus((prev) => ({ ...prev, isLoading: true })); + useEffect(() => { + if (hasInitialized.current) return; + hasInitialized.current = true; - try { - // Check browser support - const hasSupport = storageService.isFullySupported(); + const initializeStorage = async () => { + setStatus((prev) => ({ ...prev, isLoading: true })); - if (!hasSupport) { - toast.warning( - "Storage not fully supported. Some features may not work." - ); - } + try { + const hasSupport = storageService.isFullySupported(); - // Load saved projects (media will be loaded when a project is loaded) - await loadAllProjects(); + if (!hasSupport) { + toast.warning( + "Storage not fully supported. Some features may not work.", + ); + } - setStatus({ - isInitialized: true, - isLoading: false, - hasSupport, - error: null, - }); - } catch (error) { - console.error("Failed to initialize storage:", error); - setStatus({ - isInitialized: false, - isLoading: false, - hasSupport: storageService.isFullySupported(), - error: error instanceof Error ? error.message : "Unknown error", - }); - } - }; + await editor.project.loadAllProjects(); - initializeStorage(); - }, [loadAllProjects]); + setStatus({ + isInitialized: true, + isLoading: false, + hasSupport, + error: null, + }); + } catch (error) { + console.error("Failed to initialize storage:", error); + setStatus({ + isInitialized: false, + isLoading: false, + hasSupport: storageService.isFullySupported(), + error: error instanceof Error ? error.message : "Unknown error", + }); + } + }; - return ( - {children} - ); + initializeStorage(); + }, [editor.project.loadAllProjects]); + + return ( + {children} + ); } -- cgit v1.3.1