From 7c49ff0edd5818347dda2e3489ae54cb0fc5db7b Mon Sep 17 00:00:00 2001 From: Scrum4Me Agent <30029041+madhura68@users.noreply.github.com> Date: Thu, 7 May 2026 18:44:53 +0200 Subject: [PATCH] feat(PBI-59): Zustand store useJobsStore voor jobs-pagina Co-Authored-By: Claude Sonnet 4.6 --- stores/jobs-store.ts | 59 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 stores/jobs-store.ts diff --git a/stores/jobs-store.ts b/stores/jobs-store.ts new file mode 100644 index 0000000..fe0cc40 --- /dev/null +++ b/stores/jobs-store.ts @@ -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 & { id: string; status: string }): void +} + +export const useJobsStore = create()( + 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) + } + } + }) + }, + })) +)