feat(PBI-59): Zustand store useJobsStore voor jobs-pagina
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
6b64869cf3
commit
7c49ff0edd
1 changed files with 59 additions and 0 deletions
59
stores/jobs-store.ts
Normal file
59
stores/jobs-store.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import { create } from 'zustand'
|
||||
import { immer } from 'zustand/middleware/immer'
|
||||
import type { JobWithRelations } from '@/actions/jobs-page'
|
||||
|
||||
type JobsState = {
|
||||
activeJobs: JobWithRelations[]
|
||||
doneJobs: JobWithRelations[]
|
||||
selectedJobId: string | null
|
||||
}
|
||||
|
||||
type JobsActions = {
|
||||
initJobs(active: JobWithRelations[], done: JobWithRelations[]): void
|
||||
setSelectedJobId(id: string | null): void
|
||||
upsertJob(job: Partial<JobWithRelations> & { id: string; status: string }): void
|
||||
}
|
||||
|
||||
export const useJobsStore = create<JobsState & JobsActions>()(
|
||||
immer((set) => ({
|
||||
activeJobs: [],
|
||||
doneJobs: [],
|
||||
selectedJobId: null,
|
||||
|
||||
initJobs(active, done) {
|
||||
set((state) => {
|
||||
state.activeJobs = active
|
||||
state.doneJobs = done
|
||||
})
|
||||
},
|
||||
|
||||
setSelectedJobId(id) {
|
||||
set((state) => {
|
||||
state.selectedJobId = id
|
||||
})
|
||||
},
|
||||
|
||||
upsertJob(job) {
|
||||
set((state) => {
|
||||
const isDone = job.status.toUpperCase() === 'DONE'
|
||||
|
||||
if (isDone) {
|
||||
state.activeJobs = state.activeJobs.filter(j => j.id !== job.id)
|
||||
if (!state.doneJobs.find(j => j.id === job.id)) {
|
||||
state.doneJobs.unshift(job as JobWithRelations)
|
||||
if (state.doneJobs.length > 100) {
|
||||
state.doneJobs = state.doneJobs.slice(0, 100)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const idx = state.activeJobs.findIndex(j => j.id === job.id)
|
||||
if (idx !== -1) {
|
||||
Object.assign(state.activeJobs[idx], job)
|
||||
} else {
|
||||
state.activeJobs.push(job as JobWithRelations)
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
}))
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue