File size: 1,924 Bytes
f201243
8158a5c
f201243
 
 
 
 
 
 
8158a5c
f201243
 
 
 
 
8158a5c
f201243
 
 
 
 
 
 
 
 
 
 
8158a5c
f201243
 
8158a5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
import { create } from "zustand";
import { persist } from "zustand/middleware";
import type { GenerateResponse, MatrixGenerateResponse, GenerationProgress } from "../types";

interface GenerationState {
  currentGeneration: GenerateResponse | MatrixGenerateResponse | null;
  progress: GenerationProgress;
  isGenerating: boolean;
  error: string | null;
  generationStartTime: number | null;
  
  setCurrentGeneration: (ad: GenerateResponse | MatrixGenerateResponse | null) => void;
  setProgress: (progress: GenerationProgress) => void;
  setIsGenerating: (isGenerating: boolean) => void;
  setError: (error: string | null) => void;
  setGenerationStartTime: (time: number | null) => void;
  reset: () => void;
}

const initialState = {
  currentGeneration: null as GenerateResponse | MatrixGenerateResponse | null,
  progress: {
    step: "idle" as const,
    progress: 0,
  },
  isGenerating: false,
  error: null as string | null,
  generationStartTime: null as number | null,
};

// Storage key for session persistence
const STORAGE_KEY = "generation-state";

export const useGenerationStore = create<GenerationState>()(
  persist(
    (set) => ({
      ...initialState,
      
      setCurrentGeneration: (ad) => set({ currentGeneration: ad }),
      
      setProgress: (progress) => set({ progress }),
      
      setIsGenerating: (isGenerating) => set({ isGenerating }),
      
      setError: (error) => set({ error }),
      
      setGenerationStartTime: (time) => set({ generationStartTime: time }),
      
      reset: () => set(initialState),
    }),
    {
      name: STORAGE_KEY,
      // Only persist when generating to avoid stale data
      partialize: (state) => ({
        progress: state.progress,
        isGenerating: state.isGenerating,
        generationStartTime: state.generationStartTime,
        currentGeneration: state.isGenerating ? state.currentGeneration : null,
      }),
    }
  )
);