forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish-store.ts
More file actions
141 lines (119 loc) · 3.62 KB
/
Copy pathpublish-store.ts
File metadata and controls
141 lines (119 loc) · 3.62 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { create } from 'zustand'
import { immer } from 'zustand/middleware/immer'
export type PublishStep = 'selection' | 'confirmation' | 'success' | 'error'
export interface PublishSuccessResult {
publisherId: string
agents: Array<{
id: string
version: string
displayName: string
}>
}
export interface PublishErrorResult {
error: string
details?: string
hint?: string
}
interface PublishState {
publishMode: boolean
selectedAgentIds: Set<string>
searchQuery: string
currentStep: PublishStep
focusedIndex: number
isPublishing: boolean
successResult: PublishSuccessResult | null
errorResult: PublishErrorResult | null
/** Whether to include agents that spawn the selected agents (reverse dependencies) */
includeDependents: boolean
}
interface PublishActions {
openPublishMode: () => void
closePublish: () => void
toggleAgentSelection: (agentId: string) => void
setSearchQuery: (query: string) => void
goToConfirmation: () => void
goBackToSelection: () => void
setFocusedIndex: (index: number) => void
preSelectAgents: (agentIds: string[]) => void
setIsPublishing: (publishing: boolean) => void
setSuccessResult: (result: PublishSuccessResult) => void
setErrorResult: (result: PublishErrorResult) => void
setIncludeDependents: (include: boolean) => void
reset: () => void
}
type PublishStore = PublishState & PublishActions
const createInitialState = (publishMode = false): PublishState => ({
publishMode,
selectedAgentIds: new Set(),
searchQuery: '',
currentStep: 'selection',
focusedIndex: 0,
isPublishing: false,
successResult: null,
errorResult: null,
includeDependents: false,
})
const initialState: PublishState = createInitialState()
export const usePublishStore = create<PublishStore>()(
immer((set) => ({
...initialState,
openPublishMode: () => set(() => createInitialState(true)),
closePublish: () => set(() => createInitialState(false)),
toggleAgentSelection: (agentId) =>
set((state) => {
if (state.selectedAgentIds.has(agentId)) {
state.selectedAgentIds.delete(agentId)
} else {
state.selectedAgentIds.add(agentId)
}
}),
setSearchQuery: (query) =>
set((state) => {
state.searchQuery = query
state.focusedIndex = 0 // Reset focus when search changes
}),
goToConfirmation: () =>
set((state) => {
state.currentStep = 'confirmation'
state.focusedIndex = 0
}),
goBackToSelection: () =>
set((state) => {
state.currentStep = 'selection'
state.focusedIndex = 0
}),
setFocusedIndex: (index) =>
set((state) => {
state.focusedIndex = index
}),
preSelectAgents: (agentIds) =>
set(() => {
const nextState = createInitialState(true)
nextState.selectedAgentIds = new Set(agentIds)
// Stay on selection step so user can review/modify before confirming
nextState.currentStep = 'selection'
return nextState
}),
setIsPublishing: (publishing) =>
set((state) => {
state.isPublishing = publishing
}),
setSuccessResult: (result) =>
set((state) => {
state.successResult = result
state.currentStep = 'success'
state.isPublishing = false
}),
setErrorResult: (result) =>
set((state) => {
state.errorResult = result
state.currentStep = 'error'
state.isPublishing = false
}),
setIncludeDependents: (include) =>
set((state) => {
state.includeDependents = include
}),
reset: () => set(() => createInitialState(false)),
})),
)