File size: 1,606 Bytes
35765b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { createContext, useContext, useState, useEffect, type ReactNode } from 'react';
import type { Project } from '../types';

interface ProjectContextType {
  currentProject: Project | null;
  setCurrentProject: (project: Project | null) => void;
  clearProject: () => void;
}

const ProjectContext = createContext<ProjectContextType | null>(null);

const PROJECT_STORAGE_KEY = 'project_memory_current_project';

export function ProjectProvider({ children }: { children: ReactNode }) {
  const [currentProject, setCurrentProjectState] = useState<Project | null>(null);

  // Load from localStorage on mount
  useEffect(() => {
    const stored = localStorage.getItem(PROJECT_STORAGE_KEY);
    if (stored) {
      try {
        setCurrentProjectState(JSON.parse(stored));
      } catch {
        localStorage.removeItem(PROJECT_STORAGE_KEY);
      }
    }
  }, []);

  const setCurrentProject = (project: Project | null) => {
    setCurrentProjectState(project);
    if (project) {
      localStorage.setItem(PROJECT_STORAGE_KEY, JSON.stringify(project));
    } else {
      localStorage.removeItem(PROJECT_STORAGE_KEY);
    }
  };

  const clearProject = () => {
    setCurrentProjectState(null);
    localStorage.removeItem(PROJECT_STORAGE_KEY);
  };

  return (
    <ProjectContext.Provider value={{ currentProject, setCurrentProject, clearProject }}>
      {children}
    </ProjectContext.Provider>
  );
}

export function useProject() {
  const context = useContext(ProjectContext);
  if (!context) {
    throw new Error('useProject must be used within a ProjectProvider');
  }
  return context;
}