(store: PromptStore, setStore: SetStoreFunction<PromptStore>)
| 231 | } |
| 232 | |
| 233 | function createPromptStateValue(store: PromptStore, setStore: SetStoreFunction<PromptStore>) { |
| 234 | const actions = createPromptActions(setStore) |
| 235 | |
| 236 | const value = { |
| 237 | current: () => store.prompt, |
| 238 | cursor: createMemo(() => store.cursor), |
| 239 | dirty: () => !isPromptEqual(store.prompt, DEFAULT_PROMPT), |
| 240 | context: { |
| 241 | items: createMemo(() => store.context.items), |
| 242 | add(item: ContextItem) { |
| 243 | const key = contextItemKey(item) |
| 244 | if (store.context.items.find((x) => x.key === key)) return |
| 245 | setStore("context", "items", (items) => [...items, { key, ...item }]) |
| 246 | }, |
| 247 | remove(key: string) { |
| 248 | setStore("context", "items", (items) => items.filter((x) => x.key !== key)) |
| 249 | }, |
| 250 | removeComment(path: string, commentID: string) { |
| 251 | setStore("context", "items", (items) => |
| 252 | items.filter((item) => !(item.type === "file" && item.path === path && item.commentID === commentID)), |
| 253 | ) |
| 254 | }, |
| 255 | updateComment(path: string, commentID: string, next: Partial<FileContextItem> & { comment?: string }) { |
| 256 | setStore("context", "items", (items) => |
| 257 | items.map((item) => { |
| 258 | if (item.type !== "file" || item.path !== path || item.commentID !== commentID) return item |
| 259 | const value = { ...item, ...next } |
| 260 | return { ...value, key: contextItemKey(value) } |
| 261 | }), |
| 262 | ) |
| 263 | }, |
| 264 | replaceComments(items: FileContextItem[]) { |
| 265 | setStore("context", "items", (current) => [ |
| 266 | ...current.filter((item) => !isCommentItem(item)), |
| 267 | ...items.map((item) => ({ ...item, key: contextItemKey(item) })), |
| 268 | ]) |
| 269 | }, |
| 270 | }, |
| 271 | set: actions.set, |
| 272 | reset: actions.reset, |
| 273 | capture: () => value, |
| 274 | } |
| 275 | return value |
| 276 | } |
| 277 | |
| 278 | export function createPromptState() { |
| 279 | const [store, setStore] = createStore<PromptStore>(promptStore()) |
no test coverage detected