-
Notifications
You must be signed in to change notification settings - Fork 12
/
store.ts
111 lines (100 loc) · 3.79 KB
/
store.ts
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
import { type Editor, type Range } from '@tiptap/core'
import { create } from 'zustand'
import { AIProvider } from '@/types/ai'
import { NodeName, Section } from '@/types/builder'
import { GitRepository } from '@/types/git'
import { JobSection, Queue, Status } from '@/types/queue'
import { ModuleType } from '@/types/sidebar'
import { DEFAULT_INITIAL_SECTIONS, README_SECTIONS_DATA } from './sections'
type FeatureSelected = { title: string; description: string; textColorTitle: string }
type BuilderState = {
listSections: Section[]
gitRepositoryData: GitRepository | undefined
setGitRepositoryData: (data: GitRepository) => void
gitUrlRepository: string
setGitUrlRepository: (url: string) => void
readmeEditor: Editor | undefined
setReadmeEditor: (editor: Editor) => void
moduleSelected: ModuleType
setModuleSelected: (moduleSelected: ModuleType) => void
range: Range | undefined
setRange: (range: Range) => void
sectionsFromTemplates: NodeName[]
setSectionsFromTemplates: (sectionsTemplate: NodeName[]) => void
featureSelected: FeatureSelected
setFeatureSelected: (feature: FeatureSelected) => void
queue: Queue
addJobToQueue: (job: JobSection | JobSection[]) => void
updateStatusJobs: (sectionId: string, status: Status, isProcessing: boolean) => void
clearQueue: () => void
toastId: number | undefined
setToastId: (toastId: number | undefined) => void
providerAISelected: AIProvider | undefined
setProviderAISelected: (provider: AIProvider) => void
}
const INITIAL_QUEUE = { isProcessing: false, jobs: [] }
export const useBuilder = create<BuilderState>()((set, get) => ({
listSections: README_SECTIONS_DATA,
gitRepositoryData: undefined,
gitUrlRepository: '',
readmeEditor: undefined,
moduleSelected: 'templates',
range: undefined,
sectionsFromTemplates: DEFAULT_INITIAL_SECTIONS,
featureSelected: {
title: 'AI',
description: 'Reduce complexity and enhance productivity effortlessly with AI.',
textColorTitle: 'from-yellow-100 to-yellow-700'
},
queue: INITIAL_QUEUE,
toastId: undefined,
providerAISelected: undefined,
setGitRepositoryData: (data) => set({ gitRepositoryData: data }),
setGitUrlRepository: (url) => set({ gitUrlRepository: url }),
setReadmeEditor: (editor) => set({ readmeEditor: editor }),
setModuleSelected: (moduleSelected) => set({ moduleSelected }),
setRange: (range) => set({ range }),
setSectionsFromTemplates: (sections) => set({ sectionsFromTemplates: sections }),
setFeatureSelected: (feature) => set({ featureSelected: feature }),
clearQueue: () => set({ queue: INITIAL_QUEUE }),
addJobToQueue: (job) => {
set((prevValues) => ({
queue: {
isProcessing: prevValues.queue.isProcessing,
jobs: Array.isArray(job)
? [...prevValues.queue.jobs, ...job]
: [...prevValues.queue.jobs, job]
}
}))
},
updateStatusJobs: (sectionId: string, status: Status, isProcessing) => {
const jobsUpdated = get().queue.jobs.map((section) =>
section.id === sectionId ? { ...section, status } : section
)
set({
queue: {
isProcessing,
jobs: jobsUpdated
}
})
},
setToastId: (toastId) => set({ toastId }),
setProviderAISelected: (provider) => set({ providerAISelected: provider })
}))
useBuilder.subscribe((state, prevValues) => {
if (
prevValues.queue.jobs.length !== state.queue.jobs.length ||
prevValues.queue.isProcessing !== state.queue.isProcessing
) {
const { jobs, isProcessing } = state.queue
if (jobs.length === 0) return
if (isProcessing) return
const job = jobs.find((job) => job.status === 'idle')
if (!job) return
const { id, task } = job
state.updateStatusJobs(id, 'loading', true)
Promise.resolve(task()).finally(() => {
state.updateStatusJobs(id, 'completed', false)
})
}
})