summaryrefslogtreecommitdiff
path: root/apps/web/src/stores/project-store.ts
blob: 0d0812cd03c64342381eff011e5df09a178b9c2c (plain) (blame)
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
import { TProject } from "@/types/project";
import { create } from "zustand";

interface ProjectStore {
  activeProject: TProject | null;

  // Actions
  createNewProject: (name: string) => void;
  closeProject: () => void;
  updateProjectName: (name: string) => void;
}

export const useProjectStore = create<ProjectStore>((set) => ({
  activeProject: null,

  createNewProject: (name: string) => {
    const newProject: TProject = {
      id: crypto.randomUUID(),
      name,
      createdAt: new Date(),
      updatedAt: new Date(),
    };
    set({ activeProject: newProject });
  },

  closeProject: () => {
    set({ activeProject: null });
  },

  updateProjectName: (name: string) => {
    set((state) => ({
      activeProject: state.activeProject
        ? {
            ...state.activeProject,
            name,
            updatedAt: new Date(),
          }
        : null,
    }));
  },
}));