TypeScript
feat: add Zustand stores for app data, calendar view and overlays
Vipul Kumar02e6f87b8963
diff --git a/src/hooks/use-today.ts b/src/hooks/use-today.tsnew file mode 100644index 0000000..b8b40a4--- /dev/null+++ b/src/hooks/use-today.ts@@ -0,0 +1,51 @@+"use client";++import { useSyncExternalStore } from "react";+import { toKey } from "@/lib/dates";+import { SERVER_TODAY } from "@/lib/seed";++// "Today" and "now" are read on the client only (the server renders a fixed+// date) and refresh every 30s so the now-line and relative times stay live.++let today = "";+let now = 0;+const listeners = new Set<() => void>();+let timer: ReturnType<typeof setInterval> | null = null;++function tick() {+ const t = Date.now();+ now = Math.floor(t / 30_000) * 30_000;+ today = toKey(new Date(t));+}++function subscribe(listener: () => void) {+ listeners.add(listener);+ timer ??= setInterval(() => {+ tick();+ listeners.forEach((l) => l());+ }, 30_000);+ return () => {+ listeners.delete(listener);+ if (!listeners.size && timer) {+ clearInterval(timer);+ timer = null;+ }+ };+}++export function useToday(): string {+ return useSyncExternalStore(+ subscribe,+ () => (today || (tick(), today)),+ () => SERVER_TODAY,+ );+}++/** Epoch ms rounded to 30s; 0 during server rendering. */+export function useNow(): number {+ return useSyncExternalStore(+ subscribe,+ () => (now || (tick(), now)),+ () => 0,+ );+}diff --git a/src/stores/app-store.ts b/src/stores/app-store.tsnew file mode 100644index 0000000..70f6675--- /dev/null+++ b/src/stores/app-store.ts@@ -0,0 +1,396 @@+"use client";++import { toast } from "sonner";+import { create } from "zustand";+import { createJSONStorage, persist } from "zustand/middleware";+import { formatShort, rangeKeys } from "@/lib/dates";+import { durationLabel, LEAVE_COLORS, WORKDAY_MINUTES, workingDays } from "@/lib/leave";+import { createSeed, STATE_VERSION } from "@/lib/seed";+import type {+ AppNotification,+ AppState,+ Benefit,+ CalEvent,+ DocumentFile,+ Invoice,+ LeaveRequest,+ LeaveStatus,+ LeaveType,+ Profile,+ Settings,+ TimeEntry,+ WithdrawalMethod,+} from "@/lib/types";++export function uid(prefix = "id"): string {+ return `${prefix}-${crypto.randomUUID().slice(0, 8)}`;+}++export type RequestInput = {+ type: LeaveType;+ from: string;+ to: string;+ allDay: boolean;+ start: number;+ end: number;+ notes: string;+};++type Actions = {+ // workspace+ switchWorkspace: (id: string) => void;+ addWorkspace: (name: string) => void;+ // events+ saveEvent: (event: Omit<CalEvent, "id"> & { id?: string }) => CalEvent;+ moveEvent: (id: string, patch: Pick<CalEvent, "date" | "start" | "end">) => void;+ deleteEvent: (id: string) => void;+ duplicateEvent: (id: string) => void;+ // leave+ measureRequest: (input: RequestInput) => { days: number; label: string };+ submitRequest: (input: RequestInput, existingId?: string) => LeaveRequest;+ setRequestStatus: (id: string, status: LeaveStatus) => void;+ deleteRequest: (id: string) => void;+ addHoliday: (date: string, name: string) => void;+ removeHoliday: (id: string) => void;+ // notifications+ notify: (n: Omit<AppNotification, "id" | "createdAt" | "read">) => void;+ markNotificationRead: (id: string) => void;+ markAllNotificationsRead: () => void;+ clearNotifications: () => void;+ // profile & settings+ updateProfile: (patch: Partial<Profile>) => void;+ updateSettings: (patch: Partial<Settings>) => void;+ // documents+ addDocument: (doc: Omit<DocumentFile, "id" | "uploadedAt">) => void;+ updateDocument: (id: string, patch: Partial<DocumentFile>) => void;+ deleteDocument: (id: string) => void;+ // time tracking+ startTimer: (project: string, task: string) => void;+ stopTimer: () => void;+ updateTimer: (patch: { project?: string; task?: string }) => void;+ saveTimeEntry: (entry: Omit<TimeEntry, "id"> & { id?: string }) => void;+ deleteTimeEntry: (id: string) => void;+ // invoices+ saveInvoice: (invoice: Omit<Invoice, "id"> & { id?: string }) => Invoice;+ setInvoiceStatus: (id: string, status: Invoice["status"]) => void;+ deleteInvoice: (id: string) => void;+ // payouts+ addWithdrawalMethod: (m: Omit<WithdrawalMethod, "id" | "addedAt" | "isDefault">) => void;+ setDefaultWithdrawalMethod: (id: string) => void;+ removeWithdrawalMethod: (id: string) => void;+ // benefits+ toggleBenefit: (id: string) => void;+ claimBenefit: (id: string, amount: number) => void;+ updateBenefit: (id: string, patch: Partial<Benefit>) => void;+ // misc+ resetDemoData: () => void;+};++export type AppStore = AppState & Actions;++const bySubmitted = (a: LeaveRequest, b: LeaveRequest) => b.submittedAt.localeCompare(a.submittedAt);++function eventsForRequest(req: LeaveRequest): CalEvent[] {+ const color = LEAVE_COLORS[req.type];+ const note = req.status === "approved" ? "Approved" : "Pending";+ if (!req.allDay) {+ return [+ {+ id: uid("ev"),+ title: req.type,+ date: req.from,+ start: req.start ?? 540,+ end: req.end ?? 1020,+ allDay: false,+ color,+ note,+ requestId: req.id,+ },+ ];+ }+ return rangeKeys(req.from, req.to).map((date) => ({+ id: uid("ev"),+ title: req.type,+ date,+ start: 0,+ end: 1440,+ allDay: true,+ color,+ requestId: req.id,+ }));+}++const whenLabel = (from: string, to: string) =>+ from === to ? formatShort(from) : `${formatShort(from)} – ${formatShort(to)}`;++export const useAppStore = create<AppStore>()(+ persist(+ (set, get) => ({+ ...createSeed(),++ // ------------------------------------------------------------ workspace+ switchWorkspace: (id) => set({ workspaceId: id }),+ addWorkspace: (name) => {+ const ws = { id: uid("ws"), name };+ set((s) => ({ workspaces: [...s.workspaces, ws], workspaceId: ws.id }));+ toast.success(`Switched to ${name}`);+ },++ // ------------------------------------------------------------ events+ saveEvent: (event) => {+ const saved: CalEvent = { ...event, id: event.id ?? uid("ev") };+ set((s) => ({+ events: s.events.some((e) => e.id === saved.id)+ ? s.events.map((e) => (e.id === saved.id ? saved : e))+ : [...s.events, saved],+ }));+ return saved;+ },+ moveEvent: (id, patch) => set((s) => ({ events: s.events.map((e) => (e.id === id ? { ...e, ...patch } : e)) })),+ deleteEvent: (id) => {+ const removed = get().events.find((e) => e.id === id);+ if (!removed) return;+ set((s) => ({ events: s.events.filter((e) => e.id !== id) }));+ toast("Event deleted", {+ description: removed.title,+ action: { label: "Undo", onClick: () => set((s) => ({ events: [...s.events, removed] })) },+ });+ },+ duplicateEvent: (id) => {+ const ev = get().events.find((e) => e.id === id);+ if (!ev) return;+ const copy = { ...ev, id: uid("ev"), title: `${ev.title} (copy)`, requestId: undefined };+ set((s) => ({ events: [...s.events, copy] }));+ toast.success("Event duplicated", { description: copy.title });+ },++ // ------------------------------------------------------------ leave+ measureRequest: (input) => {+ if (!input.allDay) {+ const minutes = Math.max(0, input.end - input.start);+ const hours = Math.round((minutes / 60) * 10) / 10;+ return { days: minutes / WORKDAY_MINUTES, label: durationLabel(0, hours) };+ }+ const n = workingDays(input.from, input.to, get().holidays).length;+ return { days: n, label: durationLabel(n, 0) };+ },+ submitRequest: (input, existingId) => {+ const m = get().measureRequest(input);+ const req: LeaveRequest = {+ id: existingId ?? uid("rq"),+ status: "pending",+ type: input.type,+ duration: m.label,+ days: m.days,+ from: input.from,+ to: input.allDay ? input.to : input.from,+ allDay: input.allDay,+ start: input.allDay ? undefined : input.start,+ end: input.allDay ? undefined : input.end,+ notes: input.notes.trim() || input.type,+ submittedAt: new Date().toISOString(),+ };+ set((s) => ({+ requests: existingId ? s.requests.map((r) => (r.id === existingId ? req : r)) : [req, ...s.requests],+ events: [...s.events.filter((e) => e.requestId !== req.id), ...eventsForRequest(req)],+ }));+ const when = whenLabel(req.from, req.to);+ toast.success(existingId ? "Request updated" : "Request submitted", {+ description: `${req.type} · ${when} is awaiting approval`,+ });+ get().notify({+ title: `${req.type} request submitted`,+ body: `${when} · ${req.duration}. Your manager has been notified.`,+ href: "/time-off",+ });+ return req;+ },+ setRequestStatus: (id, status) => {+ const req = get().requests.find((r) => r.id === id);+ if (!req) return;+ set((s) => {+ const keep = status === "approved" || status === "pending";+ const note = status === "approved" ? "Approved" : "Pending";+ return {+ requests: s.requests.map((r) => (r.id === id ? { ...r, status } : r)),+ events: keep+ ? s.events.map((e) => (e.requestId === id && !e.allDay ? { ...e, note } : e))+ : s.events.filter((e) => e.requestId !== id),+ };+ });+ const verb = { approved: "approved", declined: "declined", cancelled: "cancelled", pending: "reopened" }[status];+ const message = `Request ${verb}`;+ const description = `${req.type} · ${formatShort(req.from)}`;+ if (status === "approved") toast.success(message, { description });+ else if (status === "declined") toast.error(message, { description });+ else toast(message, { description });+ if (status === "approved" || status === "declined") {+ get().notify({+ title: `${req.type} ${verb}`,+ body: `Your request for ${whenLabel(req.from, req.to)} was ${verb}.`,+ href: "/time-off",+ });+ }+ },+ deleteRequest: (id) => {+ const s0 = get();+ const req = s0.requests.find((r) => r.id === id);+ if (!req) return;+ const events = s0.events.filter((e) => e.requestId === id);+ set((s) => ({+ requests: s.requests.filter((r) => r.id !== id),+ events: s.events.filter((e) => e.requestId !== id),+ }));+ toast("Request deleted", {+ description: req.notes,+ action: {+ label: "Undo",+ onClick: () =>+ set((s) => ({ requests: [...s.requests, req].sort(bySubmitted), events: [...s.events, ...events] })),+ },+ });+ },+ addHoliday: (date, name) => {+ set((s) => ({+ holidays: [...s.holidays, { id: uid("ho"), date, name }].sort((a, b) => a.date.localeCompare(b.date)),+ }));+ toast.success("Holiday added", { description: `${name} · ${formatShort(date)}` });+ },+ removeHoliday: (id) => set((s) => ({ holidays: s.holidays.filter((h) => h.id !== id) })),++ // ------------------------------------------------------------ notifications+ notify: (n) =>+ set((s) => ({+ notifications: [{ ...n, id: uid("no"), createdAt: new Date().toISOString(), read: false }, ...s.notifications],+ })),+ markNotificationRead: (id) =>+ set((s) => ({ notifications: s.notifications.map((n) => (n.id === id ? { ...n, read: true } : n)) })),+ markAllNotificationsRead: () => set((s) => ({ notifications: s.notifications.map((n) => ({ ...n, read: true })) })),+ clearNotifications: () => set({ notifications: [] }),++ // ------------------------------------------------------------ profile & settings+ updateProfile: (patch) => set((s) => ({ profile: { ...s.profile, ...patch } })),+ updateSettings: (patch) => set((s) => ({ settings: { ...s.settings, ...patch } })),++ // ------------------------------------------------------------ documents+ addDocument: (doc) =>+ set((s) => ({ documents: [{ ...doc, id: uid("doc"), uploadedAt: new Date().toISOString() }, ...s.documents] })),+ updateDocument: (id, patch) =>+ set((s) => ({ documents: s.documents.map((d) => (d.id === id ? { ...d, ...patch } : d)) })),+ deleteDocument: (id) => {+ const doc = get().documents.find((d) => d.id === id);+ if (!doc) return;+ set((s) => ({ documents: s.documents.filter((d) => d.id !== id) }));+ toast("Document deleted", {+ description: doc.name,+ action: { label: "Undo", onClick: () => set((s) => ({ documents: [doc, ...s.documents] })) },+ });+ },++ // ------------------------------------------------------------ time tracking+ startTimer: (project, task) => set({ timer: { running: true, startedAt: Date.now(), project, task } }),+ stopTimer: () => {+ const { timer } = get();+ if (!timer.running || !timer.startedAt) return;+ const minutes = Math.max(1, Math.round((Date.now() - timer.startedAt) / 60000));+ const d = new Date();+ const date = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;+ set((s) => ({+ timer: { ...s.timer, running: false, startedAt: null },+ timeEntries: [+ { id: uid("te"), date, project: timer.project, task: timer.task || "Untitled task", minutes, billable: true },+ ...s.timeEntries,+ ],+ }));+ toast.success("Time logged", { description: `${minutes} min · ${timer.project}` });+ },+ updateTimer: (patch) => set((s) => ({ timer: { ...s.timer, ...patch } })),+ saveTimeEntry: (entry) => {+ const saved = { ...entry, id: entry.id ?? uid("te") };+ set((s) => ({+ timeEntries: s.timeEntries.some((e) => e.id === saved.id)+ ? s.timeEntries.map((e) => (e.id === saved.id ? saved : e))+ : [saved, ...s.timeEntries],+ }));+ },+ deleteTimeEntry: (id) => {+ const entry = get().timeEntries.find((e) => e.id === id);+ if (!entry) return;+ set((s) => ({ timeEntries: s.timeEntries.filter((e) => e.id !== id) }));+ toast("Entry deleted", {+ description: entry.task,+ action: { label: "Undo", onClick: () => set((s) => ({ timeEntries: [entry, ...s.timeEntries] })) },+ });+ },++ // ------------------------------------------------------------ invoices+ saveInvoice: (invoice) => {+ const saved: Invoice = { ...invoice, id: invoice.id ?? uid("in") };+ set((s) => ({+ invoices: s.invoices.some((i) => i.id === saved.id)+ ? s.invoices.map((i) => (i.id === saved.id ? saved : i))+ : [saved, ...s.invoices],+ }));+ return saved;+ },+ setInvoiceStatus: (id, status) =>+ set((s) => ({ invoices: s.invoices.map((i) => (i.id === id ? { ...i, status } : i)) })),+ deleteInvoice: (id) => {+ const inv = get().invoices.find((i) => i.id === id);+ if (!inv) return;+ set((s) => ({ invoices: s.invoices.filter((i) => i.id !== id) }));+ toast("Invoice deleted", {+ description: inv.number,+ action: { label: "Undo", onClick: () => set((s) => ({ invoices: [inv, ...s.invoices] })) },+ });+ },++ // ------------------------------------------------------------ payouts+ addWithdrawalMethod: (m) =>+ set((s) => ({+ withdrawalMethods: [+ ...s.withdrawalMethods,+ { ...m, id: uid("wm"), addedAt: new Date().toISOString(), isDefault: s.withdrawalMethods.length === 0 },+ ],+ })),+ setDefaultWithdrawalMethod: (id) =>+ set((s) => ({ withdrawalMethods: s.withdrawalMethods.map((m) => ({ ...m, isDefault: m.id === id })) })),+ removeWithdrawalMethod: (id) =>+ set((s) => {+ const remaining = s.withdrawalMethods.filter((m) => m.id !== id);+ if (remaining.length && !remaining.some((m) => m.isDefault)) remaining[0] = { ...remaining[0], isDefault: true };+ return { withdrawalMethods: remaining };+ }),++ // ------------------------------------------------------------ benefits+ toggleBenefit: (id) => set((s) => ({ benefits: s.benefits.map((b) => (b.id === id ? { ...b, enrolled: !b.enrolled } : b)) })),+ claimBenefit: (id, amount) =>+ set((s) => ({+ benefits: s.benefits.map((b) => (b.id === id ? { ...b, used: Math.min(b.budget, b.used + amount) } : b)),+ })),+ updateBenefit: (id, patch) => set((s) => ({ benefits: s.benefits.map((b) => (b.id === id ? { ...b, ...patch } : b)) })),++ // ------------------------------------------------------------ misc+ resetDemoData: () => {+ set(createSeed());+ toast.success("Demo data restored");+ },+ }),+ {+ name: "calendar-app:state",+ version: STATE_VERSION,+ storage: createJSONStorage(() => localStorage),+ // Hydrate after mount so the server-rendered markup (the seed) matches.+ skipHydration: true,+ partialize: (s) => {+ const data: Partial<AppStore> = {};+ for (const [k, v] of Object.entries(s)) if (typeof v !== "function") (data as Record<string, unknown>)[k] = v;+ return data as AppState;+ },+ },+ ),+);++/** Read-only selector for the active workspace name. */+export const selectWorkspaceName = (s: AppStore) =>+ s.workspaces.find((w) => w.id === s.workspaceId)?.name ?? "Workspace";diff --git a/src/stores/calendar-store.ts b/src/stores/calendar-store.tsnew file mode 100644index 0000000..8f6fdab--- /dev/null+++ b/src/stores/calendar-store.ts@@ -0,0 +1,36 @@+"use client";++import { create } from "zustand";+import { addDays, addMonths } from "@/lib/dates";+import { DESIGN_DATE } from "@/lib/seed";++export type CalendarView = "day" | "week" | "month";++type CalendarStore = {+ view: CalendarView;+ /** Focused day: drives the visible range and the accent date badge. */+ date: string;+ /** Asks the grid to scroll a specific event into view. */+ focusEventId: string | null;+ setView: (view: CalendarView) => void;+ setDate: (date: string) => void;+ step: (dir: 1 | -1) => void;+ focusEvent: (id: string, date: string) => void;+ clearFocus: () => void;+};++export const useCalendarStore = create<CalendarStore>()((set, get) => ({+ view: "week",+ date: DESIGN_DATE,+ focusEventId: null,+ setView: (view) => set({ view }),+ setDate: (date) => set({ date }),+ step: (dir) => {+ const { view, date } = get();+ if (view === "day") set({ date: addDays(date, dir) });+ else if (view === "week") set({ date: addDays(date, 7 * dir) });+ else set({ date: addMonths(date, dir) });+ },+ focusEvent: (id, date) => set({ focusEventId: id, date }),+ clearFocus: () => set({ focusEventId: null }),+}));diff --git a/src/stores/overlay-store.ts b/src/stores/overlay-store.tsnew file mode 100644index 0000000..e0f151d--- /dev/null+++ b/src/stores/overlay-store.ts@@ -0,0 +1,28 @@+"use client";++import { create } from "zustand";++/** App-wide dialogs that can be opened from anywhere (title bar, shortcuts, pages). */+export type Overlay =+ | { kind: "none" }+ | { kind: "request"; requestId?: string; from?: string }+ | { kind: "event"; eventId?: string; date?: string; start?: number; end?: number; allDay?: boolean }+ | { kind: "request-details"; requestId: string }+ | { kind: "holidays" }+ | { kind: "policy" }+ | { kind: "shortcuts" }+ | { kind: "search" };++type OverlayStore = {+ overlay: Overlay;+ open: (overlay: Overlay) => void;+ close: () => void;+};++export const useOverlayStore = create<OverlayStore>()((set) => ({+ overlay: { kind: "none" },+ open: (overlay) => set({ overlay }),+ close: () => set({ overlay: { kind: "none" } }),+}));++export const openOverlay = (overlay: Overlay) => useOverlayStore.getState().open(overlay);