DDBS UI
dashboard

Dashboard Store

createDashboardStore builds a Provider and hook with reducer state persisted to localStorage, a toast queue and a one-modal host. The dashboard counterpart of createCartProvider.

npx shadcn@latest add https://ui.develop-better-solutions.com/r/dash-store.json
Fullscreen

Appointments today: 3 (saved in your browser)

    Usage

    dash-store-example.tsx
    "use client";
    
    import { createDashboardStore } from "@dbs/ui";
    import { DashScope } from "./_store";
    
    interface State {
      appointments: number;
    }
    type Action = { type: "add" } | { type: "clear" };
    
    const { Provider, useDashboard } = createDashboardStore<State, Action>({
      storageKey: "dbs-lab-dash-store",
      initial: { appointments: 3 },
      reducer: (s, a) => (a.type === "add" ? { appointments: s.appointments + 1 } : { appointments: 0 }),
    });
    
    function Panel() {
      const { state, dispatch, toast, toasts, dismissToast, modal, openModal, closeModal } = useDashboard();
      const btn = "h-10 rounded-full border border-[var(--dash-line)] px-4 text-sm hover:bg-[var(--dash-hover)]";
      return (
        <div className="flex flex-col gap-4">
          <p className="text-sm">Appointments today: <strong className="tabular-nums">{state.appointments}</strong> (saved in your browser)</p>
          <div className="flex flex-wrap gap-2">
            <button type="button" className={btn} onClick={() => { dispatch({ type: "add" }); toast("Appointment added"); }}>Add appointment</button>
            <button type="button" className={btn} onClick={() => dispatch({ type: "clear" })}>Clear</button>
            <button type="button" className={btn} onClick={() => openModal("help")}>Open modal</button>
          </div>
          {modal === "help" && (
            <div role="dialog" aria-label="Help" className="rounded-xl border border-[var(--dash-line)] p-4 text-sm">
              One modal id is open at a time. <button type="button" className="underline" onClick={closeModal}>Close</button>
            </div>
          )}
          <ul aria-live="polite" className="flex flex-col gap-2">
            {toasts.map((t) => (
              <li key={t.id} className="flex items-center justify-between rounded-xl bg-[var(--dash-green-soft)] px-4 py-2 text-sm">
                {t.message}
                <button type="button" aria-label="Dismiss" onClick={() => dismissToast(t.id)}>x</button>
              </li>
            ))}
          </ul>
        </div>
      );
    }
    
    export default function DashStoreExample() {
      return (
        <div className="w-full max-w-lg">
          <DashScope minHeight={0}>
            <Provider>
              <Panel />
            </Provider>
          </DashScope>
        </div>
      );
    }