@okjt/dashboard 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,11 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import { forwardRef, useId } from "react";
3
+ import { useComponentDefaults } from "./DashboardProvider.js";
3
4
  const baseInputClasses = "w-full px-4 py-2 rounded-lg border border-tone-200 bg-[var(--okjt-field-bg,transparent)] text-[color:var(--okjt-field-text,inherit)] focus:outline-none focus:ring-2 focus:ring-accent/20 focus:border-accent transition-colors";
4
5
  function makeField(Tag, extraClasses, displayName) {
5
- const Field = forwardRef(function Field2({ label, error, bare = false, className = "", id, children, ...props }, ref) {
6
+ const Field = forwardRef(function Field2({ label, error, bare = false, className = "", id, children, baseClassName, ...props }, ref) {
7
+ const { classNames } = useComponentDefaults("Field", {});
8
+ const base = baseClassName ?? classNames?.[Tag] ?? `${baseInputClasses}${extraClasses ? ` ${extraClasses}` : ""}`;
6
9
  const generatedId = useId();
7
10
  const inputId = id || generatedId;
8
11
  const errorId = `${inputId}-error`;
@@ -13,7 +16,7 @@ function makeField(Tag, extraClasses, displayName) {
13
16
  id: inputId,
14
17
  "aria-invalid": error ? true : void 0,
15
18
  "aria-describedby": error && !bare ? errorId : void 0,
16
- className: `${baseInputClasses}${extraClasses ? ` ${extraClasses}` : ""} ${error ? "border-red-400" : ""} ${className}`,
19
+ className: `${base} ${error ? "border-red-400" : ""} ${className}`,
17
20
  ...props,
18
21
  children
19
22
  }
@@ -1,5 +1,6 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import React, { useState, useEffect, useLayoutEffect, useRef } from "react";
3
+ import { createPortal } from "react-dom";
3
4
  import { motion, AnimatePresence } from "framer-motion";
4
5
  import { ChevronRight, ChevronLeft, X, Trophy, Sparkles } from "lucide-react";
5
6
  import { Button } from "@okjt/ui";
@@ -15,7 +16,7 @@ function onScreen(step) {
15
16
  const rect = el.getBoundingClientRect();
16
17
  return rect.width > 0 && rect.height > 0 && rect.right > 0 && rect.left < window.innerWidth;
17
18
  }
18
- function AdminTutorial({ steps: allSteps, onComplete, isRestarted = false, open, skipMissing = false }) {
19
+ function AdminTutorial({ steps: allSteps, onComplete, isRestarted = false, open, skipMissing = false, scrollIntoView = true, render }) {
19
20
  const controlled = open !== void 0;
20
21
  const steps = skipMissing && allSteps ? allSteps.filter(onScreen) : allSteps;
21
22
  const [currentStep, setCurrentStep] = useState(0);
@@ -43,16 +44,18 @@ function AdminTutorial({ steps: allSteps, onComplete, isRestarted = false, open,
43
44
  if (isVisible) {
44
45
  const step2 = steps[currentStep];
45
46
  const el = findTarget(step2);
46
- if (el) {
47
+ if (el && scrollIntoView) {
47
48
  el.scrollIntoView({ behavior: "auto", block: "center" });
48
49
  }
49
- const timer = requestAnimationFrame(() => {
50
+ let timer = null;
51
+ if (scrollIntoView) timer = requestAnimationFrame(() => {
50
52
  updateCoords();
51
53
  });
54
+ else updateCoords();
52
55
  window.addEventListener("resize", updateCoords);
53
56
  return () => {
54
57
  window.removeEventListener("resize", updateCoords);
55
- cancelAnimationFrame(timer);
58
+ if (timer !== null) cancelAnimationFrame(timer);
56
59
  };
57
60
  }
58
61
  }, [isVisible, currentStep]);
@@ -95,7 +98,9 @@ function AdminTutorial({ steps: allSteps, onComplete, isRestarted = false, open,
95
98
  if (onComplete) onComplete();
96
99
  };
97
100
  const keys = useRef(null);
98
- keys.current = { next: handleNext, back: handleBack, done: handleComplete };
101
+ keys.current = { next: () => {
102
+ if (currentStep < steps.length - 1) setCurrentStep(currentStep + 1);
103
+ }, back: handleBack, done: handleComplete };
99
104
  useEffect(() => {
100
105
  if (!isVisible) return void 0;
101
106
  const onKey = (e) => {
@@ -149,6 +154,14 @@ function AdminTutorial({ steps: allSteps, onComplete, isRestarted = false, open,
149
154
  return { style: { top, left, width: boxW }, placement: placement2 };
150
155
  };
151
156
  if (!isVisible || !steps?.length) return null;
157
+ if (render) {
158
+ if (typeof document === "undefined") return null;
159
+ const index = Math.min(currentStep, steps.length - 1);
160
+ return createPortal(
161
+ render({ step: steps[index], index, count: steps.length, rect: coords, next: handleNext, back: handleBack, finish: handleComplete }),
162
+ document.body
163
+ );
164
+ }
152
165
  const step = steps[Math.min(currentStep, steps.length - 1)];
153
166
  const { style: boxStyle, placement } = getBoxStyle();
154
167
  return /* @__PURE__ */ jsxs("div", { className: "fixed inset-0 z-[100] pointer-events-none", children: [
@@ -31,10 +31,8 @@ function NotificationIcon({ icon }) {
31
31
  const IconComp = ICON_MAP[icon] || BellRing;
32
32
  return /* @__PURE__ */ jsx(IconComp, { className: "w-4 h-4 text-accent" });
33
33
  }
34
- function NotificationsBell({ listPath = "/notifications", markAllPath = "/notifications/mark-read", map }) {
35
- const navigate = useNavigate();
34
+ function useNotifications({ listPath = "/notifications", markAllPath = "/notifications/mark-read", markOnePath, map } = {}) {
36
35
  const { api } = useDashboard();
37
- const [showNotifications, setShowNotifications] = useState(false);
38
36
  const [notifications, setNotifications] = useState([]);
39
37
  const [unreadCount, setUnreadCount] = useState(0);
40
38
  const latest = useRef({ listPath, map });
@@ -61,19 +59,63 @@ function NotificationsBell({ listPath = "/notifications", markAllPath = "/notifi
61
59
  clearInterval(interval);
62
60
  };
63
61
  }, []);
62
+ const markAllRead = async () => {
63
+ await api.post(markAllPath);
64
+ setNotifications((list) => list.map((n) => ({ ...n, read_at: n.read_at || (/* @__PURE__ */ new Date()).toISOString() })));
65
+ setUnreadCount(0);
66
+ fetchNotifications();
67
+ };
68
+ const markRead = async (id) => {
69
+ if (!markOnePath) throw new Error("useNotifications: markOnePath is needed to mark one notification read");
70
+ const path = typeof markOnePath === "function" ? markOnePath(id) : markOnePath.replace(":id", encodeURIComponent(id));
71
+ await api.post(path);
72
+ setNotifications((list) => list.map((n) => n.id === id ? { ...n, read_at: n.read_at || (/* @__PURE__ */ new Date()).toISOString() } : n));
73
+ setUnreadCount((c) => Math.max(0, c - 1));
74
+ fetchNotifications();
75
+ };
76
+ return { notifications, unreadCount, refresh: fetchNotifications, markRead, markAllRead };
77
+ }
78
+ function NotificationsBell({ listPath = "/notifications", markAllPath = "/notifications/mark-read", markOnePath, map, renderButton, renderPanel }) {
79
+ const navigate = useNavigate();
80
+ const { notifications, unreadCount, refresh, markRead, markAllRead } = useNotifications({ listPath, markAllPath, markOnePath, map });
81
+ const [showNotifications, setShowNotifications] = useState(false);
82
+ const rootRef = useRef(null);
83
+ const toggle = () => setShowNotifications((v) => !v);
84
+ const close = () => setShowNotifications(false);
85
+ useEffect(() => {
86
+ if (!showNotifications) return void 0;
87
+ const onPointerDown = (e) => {
88
+ if (rootRef.current && !rootRef.current.contains(e.target)) setShowNotifications(false);
89
+ };
90
+ const onKey = (e) => {
91
+ if (e.key === "Escape") setShowNotifications(false);
92
+ };
93
+ document.addEventListener("mousedown", onPointerDown);
94
+ document.addEventListener("keydown", onKey);
95
+ return () => {
96
+ document.removeEventListener("mousedown", onPointerDown);
97
+ document.removeEventListener("keydown", onKey);
98
+ };
99
+ }, [showNotifications]);
64
100
  const handleMarkAllRead = async () => {
65
101
  try {
66
- await api.post(markAllPath);
67
- fetchNotifications();
102
+ await markAllRead();
68
103
  } catch (e) {
69
104
  console.error("Failed to mark read", e);
70
105
  }
71
106
  };
72
- return /* @__PURE__ */ jsxs("div", { className: "relative", children: [
107
+ if (renderButton || renderPanel) {
108
+ const bell = { open: showNotifications, toggle, close, unreadCount, notifications, markRead, markAllRead, refresh };
109
+ return /* @__PURE__ */ jsxs("div", { className: "relative", ref: rootRef, children: [
110
+ renderButton ? renderButton(bell) : null,
111
+ showNotifications && renderPanel ? renderPanel(bell) : null
112
+ ] });
113
+ }
114
+ return /* @__PURE__ */ jsxs("div", { className: "relative", ref: rootRef, children: [
73
115
  /* @__PURE__ */ jsxs(
74
116
  "button",
75
117
  {
76
- onClick: () => setShowNotifications(!showNotifications),
118
+ onClick: toggle,
77
119
  "aria-label": unreadCount > 0 ? `Notifications, ${unreadCount} unread` : "Notifications",
78
120
  "aria-haspopup": "true",
79
121
  "aria-expanded": showNotifications,
@@ -133,5 +175,6 @@ function NotificationsBell({ listPath = "/notifications", markAllPath = "/notifi
133
175
  ] });
134
176
  }
135
177
  export {
136
- NotificationsBell as default
178
+ NotificationsBell as default,
179
+ useNotifications
137
180
  };
package/dist/index.js CHANGED
@@ -49,7 +49,7 @@ import { default as default33 } from "./DashboardSidebar.js";
49
49
  import { default as default34 } from "./DashboardHeader.js";
50
50
  import { AuthProvider, useAuth, default as default35 } from "./AuthContext.js";
51
51
  import { default as default36 } from "./GoogleButton.js";
52
- import { default as default37 } from "./NotificationsBell.js";
52
+ import { default as default37, useNotifications } from "./NotificationsBell.js";
53
53
  import { loginWithPasskey, registerPasskey, passkeysSupported } from "./passkeys.js";
54
54
  import { default as default38, AuthField, AuthSubmit, AuthError } from "./AuthLayout.js";
55
55
  import { default as default39 } from "./SignInPage.js";
@@ -144,6 +144,7 @@ export {
144
144
  useGuardedNavigate,
145
145
  useHasUnsavedChanges,
146
146
  useModules,
147
+ useNotifications,
147
148
  default26 as useSavedViews,
148
149
  useSearch,
149
150
  useUnsavedChanges,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@okjt/dashboard",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "Dashboard pages for OKJTech projects: page layout, hero, summary cards, floating toolbar with search and filters, side panel, lists, bulk actions, one way to save, and working together: presence, version history, sharing; the dashboard shell, sign-in pages and account menu",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -23,7 +23,7 @@
23
23
  "test": "node build.mjs && vitest run"
24
24
  },
25
25
  "dependencies": {
26
- "@okjt/ui": "^0.3.0"
26
+ "@okjt/ui": "^0.4.0"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "@tanstack/react-query": ">=5",