File size: 3,246 Bytes
5da4770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { create } from 'zustand';
import { devtools, persist } from 'zustand/middleware';
import { AgentVersion, VersionComparison } from '../types';

interface VersionState {
  currentVersion: AgentVersion | null;
  compareVersion: AgentVersion | null;
  versions: AgentVersion[];
  versionComparison: VersionComparison | null;
  
  isViewingVersion: boolean;
  isComparingVersions: boolean;
  hasUnsavedChanges: boolean;
  isLoading: boolean;
  error: string | null;
  
  setCurrentVersion: (version: AgentVersion | null) => void;
  setCompareVersion: (version: AgentVersion | null) => void;
  setVersions: (versions: AgentVersion[]) => void;
  setVersionComparison: (comparison: VersionComparison | null) => void;
  
  setIsViewingVersion: (viewing: boolean) => void;
  setIsComparingVersions: (comparing: boolean) => void;
  setHasUnsavedChanges: (hasChanges: boolean) => void;
  setIsLoading: (loading: boolean) => void;
  setError: (error: string | null) => void;
  
  clearVersionState: () => void;
  isViewingOldVersion: (currentVersionId?: string) => boolean;
}

export const useVersionStore = create<VersionState>()(
  devtools(
    persist(
      (set, get) => ({
        currentVersion: null,
        compareVersion: null,
        versions: [],
        versionComparison: null,
        
        isViewingVersion: false,
        isComparingVersions: false,
        hasUnsavedChanges: false,
        isLoading: false,
        error: null,
        
        setCurrentVersion: (version) => set({ 
          currentVersion: version,
          isViewingVersion: version !== null
        }),
        
        setCompareVersion: (version) => set({ 
          compareVersion: version,
          isComparingVersions: version !== null
        }),
        
        setVersions: (versions) => set({ versions }),
        
        setVersionComparison: (comparison) => set({ 
          versionComparison: comparison 
        }),
        
        setIsViewingVersion: (viewing) => set({ isViewingVersion: viewing }),
        
        setIsComparingVersions: (comparing) => set({ 
          isComparingVersions: comparing,
          compareVersion: comparing ? get().compareVersion : null
        }),
        
        setHasUnsavedChanges: (hasChanges) => set({ hasUnsavedChanges: hasChanges }),
        setIsLoading: (loading) => set({ isLoading: loading }),
        setError: (error) => set({ error }),
        
        clearVersionState: () => set({
          currentVersion: null,
          compareVersion: null,
          versions: [],
          versionComparison: null,
          isViewingVersion: false,
          isComparingVersions: false,
          hasUnsavedChanges: false,
          isLoading: false,
          error: null
        }),
        
        isViewingOldVersion: (currentVersionId?: string) => {
          const state = get();
          return state.isViewingVersion && 
                 state.currentVersion !== null && 
                 state.currentVersion.versionId.value !== currentVersionId;
        }
      }),
      {
        name: 'agent-version-store',
        partialize: (state) => ({
          hasUnsavedChanges: state.hasUnsavedChanges
        })
      }
    ),
    {
      name: 'agent-version-store'
    }
  )
);