@okjt/dashboard 0.3.0 → 0.5.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.
@@ -0,0 +1,61 @@
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
+ import { useState } from "react";
3
+ import { useParams, useSearchParams } from "react-router-dom";
4
+ import { useAuth } from "./AuthContext.js";
5
+ import AuthLayout, { AuthError, AuthField, AuthSubmit } from "./AuthLayout.js";
6
+ function ResetPasswordPage({
7
+ layout: Layout = AuthLayout,
8
+ token: tokenProp,
9
+ email: emailProp,
10
+ onReset,
11
+ title = "Choose a new password",
12
+ subtitle = "Make it at least 8 characters. You\u2019ll sign in with it from now on.",
13
+ footer,
14
+ submitLabel = "Reset password",
15
+ idPrefix = "reset"
16
+ }) {
17
+ const params = useParams();
18
+ const [query] = useSearchParams();
19
+ const token = tokenProp ?? params.token ?? "";
20
+ const { resetPassword } = useAuth();
21
+ const [email, setEmail] = useState(emailProp ?? query.get("email") ?? "");
22
+ const [password, setPassword] = useState("");
23
+ const [confirmation, setConfirmation] = useState("");
24
+ const [errors, setErrors] = useState({});
25
+ const [error, setError] = useState(null);
26
+ const [loading, setLoading] = useState(false);
27
+ const onSubmit = async (e) => {
28
+ e.preventDefault();
29
+ if (password !== confirmation) {
30
+ setErrors({ password_confirmation: "The passwords don\u2019t match." });
31
+ return;
32
+ }
33
+ setLoading(true);
34
+ setError(null);
35
+ setErrors({});
36
+ const result = await resetPassword({ token, email, password, password_confirmation: confirmation });
37
+ setLoading(false);
38
+ if (result.success) {
39
+ onReset?.(result);
40
+ return;
41
+ }
42
+ const fieldErrors = Object.fromEntries(Object.entries(result.errors ?? {}).map(([k, v]) => [k, Array.isArray(v) ? v[0] : v]));
43
+ setErrors(fieldErrors);
44
+ if (!Object.keys(fieldErrors).length) setError(result.message);
45
+ };
46
+ if (!token) {
47
+ return /* @__PURE__ */ jsx(Layout, { title: "This link is incomplete", subtitle: "Open the link from the email again, or ask for a new one.", footer });
48
+ }
49
+ return /* @__PURE__ */ jsxs(Layout, { title, subtitle, footer, children: [
50
+ /* @__PURE__ */ jsx(AuthError, { children: error }),
51
+ /* @__PURE__ */ jsxs("form", { onSubmit, className: "space-y-5", children: [
52
+ /* @__PURE__ */ jsx(AuthField, { id: `${idPrefix}-email`, label: "Email", type: "email", autoComplete: "email", value: email, onChange: (e) => setEmail(e.target.value), error: errors.email, required: true }),
53
+ /* @__PURE__ */ jsx(AuthField, { id: `${idPrefix}-password`, label: "New password", type: "password", autoComplete: "new-password", value: password, onChange: (e) => setPassword(e.target.value), error: errors.password, required: true, autoFocus: true }),
54
+ /* @__PURE__ */ jsx(AuthField, { id: `${idPrefix}-password-confirmation`, label: "Confirm password", type: "password", autoComplete: "new-password", value: confirmation, onChange: (e) => setConfirmation(e.target.value), error: errors.password_confirmation, required: true }),
55
+ /* @__PURE__ */ jsx("div", { className: "pt-2", children: /* @__PURE__ */ jsx(AuthSubmit, { loading, children: loading ? "Saving\u2026" : submitLabel }) })
56
+ ] })
57
+ ] });
58
+ }
59
+ export {
60
+ ResetPasswordPage as default
61
+ };
@@ -1,10 +1,21 @@
1
- import { jsx, jsxs } from "react/jsx-runtime";
1
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
2
  import { useState } from "react";
3
3
  import { useForm } from "react-hook-form";
4
4
  import { useAuth } from "./AuthContext.js";
5
5
  import AuthLayout, { AuthError, AuthField, AuthSubmit } from "./AuthLayout.js";
6
6
  import TwoFactorForm from "./TwoFactorForm.js";
7
7
  import { loginWithPasskey, passkeysSupported } from "./passkeys.js";
8
+ const LABELS = {
9
+ submit: "Sign in",
10
+ submitting: "Signing in\u2026",
11
+ sendCode: "Email me a code",
12
+ verifyCode: "Verify & sign in",
13
+ emailPlaceholder: "you@example.com",
14
+ passwordPlaceholder: "Type your password",
15
+ remember: "Remember me",
16
+ useCode: "Email me a sign-in code instead",
17
+ usePassword: "Use password instead"
18
+ };
8
19
  function SignInPage({
9
20
  layout: Layout = AuthLayout,
10
21
  onSignedIn,
@@ -12,41 +23,78 @@ function SignInPage({
12
23
  subtitle,
13
24
  footer,
14
25
  forgotHint,
26
+ remember: offerRemember = false,
27
+ emailCode = false,
28
+ status,
29
+ passkey,
30
+ labels: labelsIn,
31
+ emailAutoComplete = "email",
15
32
  google,
16
33
  idPrefix = "sign-in"
17
34
  }) {
18
- const { login, verify2fa, resendVerification, signedIn } = useAuth();
35
+ const labels = { ...LABELS, ...labelsIn };
36
+ const { login, verify2fa, resendVerification, signedIn, requestLoginCode, verifyLoginCode } = useAuth();
19
37
  const [error, setError] = useState(null);
38
+ const [notice, setNotice] = useState(status ?? null);
20
39
  const [loading, setLoading] = useState(false);
21
- const [is2faRequired, setIs2faRequired] = useState(false);
22
- const [emailFor2fa, setEmailFor2fa] = useState("");
40
+ const [challenge, setChallenge] = useState(null);
23
41
  const [twoFactorCode, setTwoFactorCode] = useState("");
42
+ const [useRecovery, setUseRecovery] = useState(false);
24
43
  const [needsVerification, setNeedsVerification] = useState(null);
25
44
  const [resendState, setResendState] = useState(null);
26
- const { register, handleSubmit, formState: { errors } } = useForm();
45
+ const [method, setMethod] = useState("password");
46
+ const [codeFor, setCodeFor] = useState(null);
47
+ const [rememberMe, setRememberMe] = useState(false);
48
+ const [emailedCode, setEmailedCode] = useState("");
49
+ const { register, handleSubmit, resetField, formState: { errors } } = useForm();
27
50
  const done = (result) => {
28
51
  const problem = onSignedIn?.(result);
29
52
  if (typeof problem === "string") setError(problem);
30
53
  };
31
- const onSubmit = async (data) => {
32
- setLoading(true);
33
- setError(null);
34
- const result = await login(data.email, data.password);
54
+ const settle = (result) => {
35
55
  if (result.requires_verification) {
36
56
  setNeedsVerification(result.email);
37
57
  setError(null);
38
58
  } else if (result.success) {
39
59
  if (result.requires_2fa) {
40
- setIs2faRequired(true);
41
- setEmailFor2fa(result.email);
60
+ setChallenge(result.challenge ?? "");
42
61
  } else {
43
62
  done(result);
44
63
  }
45
64
  } else {
46
- setError(result.message);
65
+ setError(result.errors?.code?.[0] || result.message);
66
+ }
67
+ };
68
+ const onSubmit = async (data) => {
69
+ setLoading(true);
70
+ setError(null);
71
+ if (method === "code") {
72
+ if (!codeFor) {
73
+ const result = await requestLoginCode(data.email);
74
+ if (result.success) {
75
+ setCodeFor(data.email);
76
+ setNotice(result.message || "If that address exists, a sign-in code is on its way.");
77
+ } else {
78
+ setError(result.message);
79
+ }
80
+ } else {
81
+ setNotice(null);
82
+ settle(await verifyLoginCode(codeFor, emailedCode));
83
+ }
84
+ setLoading(false);
85
+ return;
47
86
  }
87
+ settle(await login(data.email, data.password, offerRemember && rememberMe ? { remember: true } : {}));
48
88
  setLoading(false);
49
89
  };
90
+ const switchMethod = () => {
91
+ setMethod((m) => m === "password" ? "code" : "password");
92
+ setCodeFor(null);
93
+ setError(null);
94
+ setNotice(null);
95
+ resetField("password");
96
+ setEmailedCode("");
97
+ };
50
98
  const handleResend = async () => {
51
99
  setResendState("sending");
52
100
  const result = await resendVerification(needsVerification);
@@ -62,8 +110,7 @@ function SignInPage({
62
110
  return;
63
111
  }
64
112
  if (result.requires_2fa) {
65
- setIs2faRequired(true);
66
- setEmailFor2fa(result.email);
113
+ setChallenge(result.challenge ?? "");
67
114
  return;
68
115
  }
69
116
  done(signedIn(result));
@@ -77,7 +124,7 @@ function SignInPage({
77
124
  e.preventDefault();
78
125
  setLoading(true);
79
126
  setError(null);
80
- const result = await verify2fa(emailFor2fa, twoFactorCode);
127
+ const result = await verify2fa(challenge, twoFactorCode);
81
128
  if (result.success) {
82
129
  done(result);
83
130
  } else {
@@ -117,24 +164,46 @@ function SignInPage({
117
164
  }
118
165
  );
119
166
  }
120
- if (is2faRequired) {
167
+ if (challenge !== null) {
121
168
  return /* @__PURE__ */ jsxs(
122
169
  Layout,
123
170
  {
124
171
  title: "Two-step verification",
125
- subtitle: "Enter the 6-digit code from your authenticator app to finish signing in.",
172
+ subtitle: useRecovery ? "Enter one of the recovery codes you saved when you turned two-step sign-in on." : "Enter the 6-digit code from your authenticator app to finish signing in.",
126
173
  footer: /* @__PURE__ */ jsx("button", { type: "button", onClick: () => {
127
- setIs2faRequired(false);
174
+ setChallenge(null);
175
+ setTwoFactorCode("");
176
+ setUseRecovery(false);
128
177
  setError(null);
129
178
  }, className: "hover:text-body", children: "\u2190 Use a different account" }),
130
179
  children: [
131
180
  /* @__PURE__ */ jsx(AuthError, { children: error }),
132
- /* @__PURE__ */ jsx(TwoFactorForm, { id: `${idPrefix}-2fa`, code: twoFactorCode, onCode: setTwoFactorCode, loading, onSubmit: handle2faVerify })
181
+ /* @__PURE__ */ jsx(TwoFactorForm, { id: `${idPrefix}-2fa`, code: twoFactorCode, onCode: setTwoFactorCode, loading, onSubmit: handle2faVerify, recovery: useRecovery }),
182
+ /* @__PURE__ */ jsx("div", { className: "mt-4 text-center", children: /* @__PURE__ */ jsx("button", { type: "button", onClick: () => {
183
+ setUseRecovery((r) => !r);
184
+ setTwoFactorCode("");
185
+ setError(null);
186
+ }, className: "text-sm text-muted hover:text-body", children: useRecovery ? "Use your authenticator app instead" : "Use a recovery code instead" }) })
133
187
  ]
134
188
  }
135
189
  );
136
190
  }
191
+ const byCode = method === "code";
192
+ const passkeyButton = passkey !== void 0 ? passkey : passkeysSupported() && /* @__PURE__ */ jsxs(
193
+ "button",
194
+ {
195
+ type: "button",
196
+ onClick: handlePasskey,
197
+ disabled: loading,
198
+ className: "mb-3 flex w-full items-center justify-center gap-2 rounded-full border border-tone-200 bg-surface px-4 py-3 text-sm font-semibold text-ink shadow-sm transition-colors hover:bg-tone-50 disabled:opacity-50",
199
+ children: [
200
+ /* @__PURE__ */ jsx("svg", { className: "h-5 w-5", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor", strokeWidth: "1.8", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M15.75 5.25a3 3 0 0 1 3 3m3 0a6 6 0 0 1-7.029 5.912c-.563-.097-1.159.026-1.563.43L10.5 17.25H8.25v2.25H6v2.25H2.25v-2.818c0-.597.237-1.17.659-1.591l6.499-6.499c.404-.404.527-1 .43-1.563A6 6 0 1 1 21.75 8.25Z" }) }),
201
+ "Sign in with a passkey"
202
+ ]
203
+ }
204
+ );
137
205
  return /* @__PURE__ */ jsxs(Layout, { title, subtitle, footer, children: [
206
+ notice && /* @__PURE__ */ jsx("div", { role: "status", className: "mb-5 rounded-2xl border border-emerald-200 bg-emerald-50 px-4 py-3 text-center text-sm text-emerald-700", children: notice }),
138
207
  /* @__PURE__ */ jsx(AuthError, { children: error }),
139
208
  /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit(onSubmit), className: "space-y-5", noValidate: true, children: [
140
209
  /* @__PURE__ */ jsx(
@@ -143,8 +212,9 @@ function SignInPage({
143
212
  id: `${idPrefix}-email`,
144
213
  label: "Email",
145
214
  type: "email",
146
- autoComplete: "email",
147
- placeholder: "you@example.com",
215
+ autoComplete: emailAutoComplete,
216
+ placeholder: labels.emailPlaceholder,
217
+ readOnly: byCode && !!codeFor,
148
218
  error: errors.email?.message,
149
219
  ...register("email", {
150
220
  required: "Email is required",
@@ -155,14 +225,14 @@ function SignInPage({
155
225
  })
156
226
  }
157
227
  ),
158
- /* @__PURE__ */ jsx(
228
+ !byCode && /* @__PURE__ */ jsx(
159
229
  AuthField,
160
230
  {
161
231
  id: `${idPrefix}-password`,
162
232
  label: "Password",
163
233
  type: "password",
164
234
  autoComplete: "current-password",
165
- placeholder: "Type your password",
235
+ placeholder: labels.passwordPlaceholder,
166
236
  error: errors.password?.message,
167
237
  ...register("password", {
168
238
  required: "Password is required",
@@ -170,27 +240,40 @@ function SignInPage({
170
240
  })
171
241
  }
172
242
  ),
173
- forgotHint && /* @__PURE__ */ jsx("div", { className: "text-xs text-faint text-right -mt-1", children: forgotHint }),
174
- /* @__PURE__ */ jsx("div", { className: "pt-2", children: /* @__PURE__ */ jsx(AuthSubmit, { loading, children: loading ? "Signing in\u2026" : "Sign in" }) })
243
+ byCode && codeFor && /* @__PURE__ */ jsx(
244
+ AuthField,
245
+ {
246
+ id: `${idPrefix}-code`,
247
+ label: "Sign-in code",
248
+ inputMode: "numeric",
249
+ autoComplete: "one-time-code",
250
+ placeholder: "6-digit code",
251
+ maxLength: 6,
252
+ autoFocus: true,
253
+ required: true,
254
+ value: emailedCode,
255
+ onChange: (e) => setEmailedCode(e.target.value.replace(/\D/g, ""))
256
+ }
257
+ ),
258
+ !byCode && offerRemember && /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-3 -mt-1", children: [
259
+ /* @__PURE__ */ jsxs("label", { className: "flex cursor-pointer items-center gap-2 text-sm text-muted", children: [
260
+ /* @__PURE__ */ jsx("input", { type: "checkbox", checked: rememberMe, onChange: (e) => setRememberMe(e.target.checked), className: "h-4 w-4 rounded border-line accent-[var(--okjt-auth-focus,var(--color-accent))]" }),
261
+ labels.remember
262
+ ] }),
263
+ forgotHint && /* @__PURE__ */ jsx("div", { className: "text-sm text-muted", children: forgotHint })
264
+ ] }),
265
+ !byCode && !offerRemember && forgotHint && /* @__PURE__ */ jsx("div", { className: "text-xs text-faint text-right -mt-1", children: forgotHint }),
266
+ /* @__PURE__ */ jsx("div", { className: "pt-2", children: /* @__PURE__ */ jsx(AuthSubmit, { loading, children: loading ? labels.submitting : !byCode ? labels.submit : codeFor ? labels.verifyCode : labels.sendCode }) })
175
267
  ] }),
176
- /* @__PURE__ */ jsxs("div", { className: "relative my-5", "aria-hidden": "true", children: [
177
- /* @__PURE__ */ jsx("div", { className: "absolute inset-0 flex items-center", children: /* @__PURE__ */ jsx("div", { className: "w-full border-t border-tone-200" }) }),
178
- /* @__PURE__ */ jsx("div", { className: "relative flex justify-center text-xs", children: /* @__PURE__ */ jsx("span", { className: "bg-white px-3 text-faint", children: "or" }) })
268
+ (passkeyButton || google) && /* @__PURE__ */ jsxs(Fragment, { children: [
269
+ /* @__PURE__ */ jsxs("div", { className: "relative my-5", "aria-hidden": "true", children: [
270
+ /* @__PURE__ */ jsx("div", { className: "absolute inset-0 flex items-center", children: /* @__PURE__ */ jsx("div", { className: "w-full border-t border-tone-200" }) }),
271
+ /* @__PURE__ */ jsx("div", { className: "relative flex justify-center text-xs", children: /* @__PURE__ */ jsx("span", { className: "bg-[var(--okjt-auth-card,#fff)] px-3 text-faint", children: "or" }) })
272
+ ] }),
273
+ passkeyButton,
274
+ google
179
275
  ] }),
180
- passkeysSupported() && /* @__PURE__ */ jsxs(
181
- "button",
182
- {
183
- type: "button",
184
- onClick: handlePasskey,
185
- disabled: loading,
186
- className: "mb-3 flex w-full items-center justify-center gap-2 rounded-full border border-tone-200 bg-white px-4 py-3 text-sm font-semibold text-ink shadow-sm transition-colors hover:bg-tone-50 disabled:opacity-50",
187
- children: [
188
- /* @__PURE__ */ jsx("svg", { className: "h-5 w-5", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor", strokeWidth: "1.8", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M15.75 5.25a3 3 0 0 1 3 3m3 0a6 6 0 0 1-7.029 5.912c-.563-.097-1.159.026-1.563.43L10.5 17.25H8.25v2.25H6v2.25H2.25v-2.818c0-.597.237-1.17.659-1.591l6.499-6.499c.404-.404.527-1 .43-1.563A6 6 0 1 1 21.75 8.25Z" }) }),
189
- "Sign in with a passkey"
190
- ]
191
- }
192
- ),
193
- google
276
+ emailCode && /* @__PURE__ */ jsx("div", { className: "mt-4 text-center", children: /* @__PURE__ */ jsx("button", { type: "button", onClick: switchMethod, className: "text-sm text-muted hover:text-body", children: byCode ? labels.usePassword : labels.useCode }) })
194
277
  ] });
195
278
  }
196
279
  export {
@@ -1,8 +1,22 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import { AuthField, AuthSubmit } from "./AuthLayout.js";
3
- function TwoFactorForm({ id, code, onCode, loading, onSubmit }) {
3
+ function TwoFactorForm({ id, code, onCode, loading, onSubmit, recovery = false }) {
4
4
  return /* @__PURE__ */ jsxs("form", { onSubmit, className: "space-y-6", children: [
5
- /* @__PURE__ */ jsx(
5
+ recovery ? /* @__PURE__ */ jsx(
6
+ AuthField,
7
+ {
8
+ id,
9
+ label: "Recovery code",
10
+ value: code,
11
+ onChange: (e) => onCode(e.target.value.trim()),
12
+ autoComplete: "off",
13
+ placeholder: "xxxxxxxxxx-xxxxxxxxxx",
14
+ required: true,
15
+ autoFocus: true,
16
+ inputClassName: "text-center font-mono"
17
+ },
18
+ "recovery"
19
+ ) : /* @__PURE__ */ jsx(
6
20
  AuthField,
7
21
  {
8
22
  id,
@@ -16,7 +30,8 @@ function TwoFactorForm({ id, code, onCode, loading, onSubmit }) {
16
30
  required: true,
17
31
  autoFocus: true,
18
32
  inputClassName: "text-center text-xl tracking-[0.6em] font-semibold"
19
- }
33
+ },
34
+ "code"
20
35
  ),
21
36
  /* @__PURE__ */ jsx(AuthSubmit, { loading, children: loading ? "Verifying\u2026" : "Verify & sign in" })
22
37
  ] });
@@ -1,8 +1,10 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import { AlertCircle } from "lucide-react";
3
3
  import { AdminHeaderStatus } from "./AdminChromeContext.js";
4
+ import { useHasUnsavedChanges } from "./useUnsavedChangesGuard.js";
4
5
  function UnsavedChangesStatus({ dirty }) {
5
- if (!dirty) return null;
6
+ const anyUnsaved = useHasUnsavedChanges();
7
+ if (!(dirty ?? anyUnsaved)) return null;
6
8
  return /* @__PURE__ */ jsx(AdminHeaderStatus, { children: /* @__PURE__ */ jsxs("span", { role: "status", className: "px-3 py-1.5 rounded-full text-xs font-semibold flex items-center gap-2 bg-amber-50 text-amber-800 whitespace-nowrap", children: [
7
9
  /* @__PURE__ */ jsx(AlertCircle, { className: "w-3.5 h-3.5" }),
8
10
  " Unsaved changes"
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { DashboardProvider, useDashboard } from "./DashboardProvider.js";
1
+ import { DashboardProvider, useDashboard, useComponentDefaults } from "./DashboardProvider.js";
2
2
  import { AdminChromeProvider, useAdminChrome, AdminHeaderStatus } from "./AdminChromeContext.js";
3
3
  import { SearchProvider, useSearch } from "./SearchContext.js";
4
4
  import { default as default2 } from "./AdminPageLayout.js";
@@ -8,6 +8,7 @@ import { default as default5 } from "./StatCard.js";
8
8
  import { default as default6 } from "./AdminFloatingToolbar.js";
9
9
  import { default as default7 } from "./AdminToolbarSearch.js";
10
10
  import { default as default8, countActiveFilters } from "./AdminToolbarFilters.js";
11
+ import { countActiveFilterLists } from "./BoldToolbar.js";
11
12
  import { SidePanelNav, SidePanelAside, SidePanelMobileBar } from "./AdminSidePanel.js";
12
13
  import { default as default9 } from "./AdminListPanel.js";
13
14
  import { default as default10, pageWindow } from "./AdminPagination.js";
@@ -19,38 +20,51 @@ import { default as default14 } from "./EditableFieldset.js";
19
20
  import { default as default15 } from "./DeleteButton.js";
20
21
  import { default as default16 } from "./ToggleButton.js";
21
22
  import { default as default17 } from "./EmptyState.js";
22
- import { default as default18 } from "./QueryError.js";
23
- import { default as default19 } from "./UnsavedChangesStatus.js";
24
- import { default as default20 } from "./AdminTutorial.js";
25
- import { default as default21, useBulkAdminAction } from "./useAdminAction.js";
26
- import { default as default22 } from "./useCrudHandlers.js";
27
- import { default as default23, useUnsavedChanges } from "./useUnsavedChangesGuard.js";
28
- import { default as default24 } from "./useFilteredItems.js";
29
- import { default as default25 } from "./useSavedViews.js";
23
+ import { default as default18 } from "./DashboardIcon.js";
24
+ import { default as default19 } from "./QueryError.js";
25
+ import { default as default20 } from "./UnsavedChangesStatus.js";
26
+ import { default as default21 } from "./AdminTutorial.js";
27
+ import { default as default22, useBulkAdminAction } from "./useAdminAction.js";
28
+ import { default as default23 } from "./useCrudHandlers.js";
29
+ import {
30
+ default as default24,
31
+ useUnsavedChanges,
32
+ useHasUnsavedChanges,
33
+ hasUnsavedChanges,
34
+ confirmLeave,
35
+ useGuardedNavigate,
36
+ allowNextNavigation
37
+ } from "./useUnsavedChangesGuard.js";
38
+ import { default as default25 } from "./useFilteredItems.js";
39
+ import { default as default26 } from "./useSavedViews.js";
30
40
  import { useModules, moduleForPathIn, initials, collaboratorColor } from "./modules.js";
31
41
  import { CollaborationProvider, useCollaboration, HEARTBEAT_MS } from "./CollaborationContext.js";
32
- import { default as default26, CollaboratorAvatar } from "./CollaboratorAvatars.js";
33
- import { default as default27 } from "./ActivityPanel.js";
34
- import { default as default28 } from "./useActivityPanel.js";
35
- import { default as default29 } from "./VersionHistoryDrawer.js";
36
- import { default as default30 } from "./ShareModal.js";
37
- import { default as default31 } from "./DashboardShell.js";
38
- import { default as default32 } from "./DashboardSidebar.js";
39
- import { default as default33 } from "./DashboardHeader.js";
40
- import { AuthProvider, useAuth, default as default34 } from "./AuthContext.js";
41
- import { default as default35 } from "./GoogleButton.js";
42
- import { default as default36 } from "./NotificationsBell.js";
42
+ import { default as default27, CollaboratorAvatar } from "./CollaboratorAvatars.js";
43
+ import { default as default28 } from "./ActivityPanel.js";
44
+ import { default as default29 } from "./useActivityPanel.js";
45
+ import { default as default30 } from "./VersionHistoryDrawer.js";
46
+ import { default as default31 } from "./ShareModal.js";
47
+ import { default as default32 } from "./DashboardShell.js";
48
+ import { default as default33 } from "./DashboardSidebar.js";
49
+ import { default as default34 } from "./DashboardHeader.js";
50
+ import { AuthProvider, useAuth, default as default35 } from "./AuthContext.js";
51
+ import { default as default36 } from "./GoogleButton.js";
52
+ import { default as default37 } from "./NotificationsBell.js";
43
53
  import { loginWithPasskey, registerPasskey, passkeysSupported } from "./passkeys.js";
44
- import { default as default37, AuthField, AuthSubmit, AuthError } from "./AuthLayout.js";
45
- import { default as default38 } from "./SignInPage.js";
46
- import { default as default39 } from "./GoogleCallbackPage.js";
47
- import { default as default40 } from "./VerifyEmailPage.js";
48
- import { default as default41 } from "./AcceptInvitePage.js";
49
- import { default as default42 } from "./AccountMenu.js";
54
+ import { default as default38, AuthField, AuthSubmit, AuthError } from "./AuthLayout.js";
55
+ import { default as default39 } from "./SignInPage.js";
56
+ import { default as default40 } from "./GoogleCallbackPage.js";
57
+ import { default as default41 } from "./VerifyEmailPage.js";
58
+ import { default as default42 } from "./ForgotPasswordPage.js";
59
+ import { default as default43 } from "./ResetPasswordPage.js";
60
+ import { default as default44 } from "./ConfirmPasswordPage.js";
61
+ import { default as default45 } from "./TwoFactorForm.js";
62
+ import { default as default46 } from "./AcceptInvitePage.js";
63
+ import { default as default47 } from "./AccountMenu.js";
50
64
  export {
51
- default41 as AcceptInvitePage,
52
- default42 as AccountMenu,
53
- default27 as ActivityPanel,
65
+ default46 as AcceptInvitePage,
66
+ default47 as AccountMenu,
67
+ default28 as ActivityPanel,
54
68
  default11 as AdminBulkActions,
55
69
  default12 as AdminCard,
56
70
  AdminChromeProvider,
@@ -67,59 +81,71 @@ export {
67
81
  AdminTextarea,
68
82
  default8 as AdminToolbarFilters,
69
83
  default7 as AdminToolbarSearch,
70
- default20 as AdminTutorial,
71
- default34 as AuthContext,
84
+ default21 as AdminTutorial,
85
+ default35 as AuthContext,
72
86
  AuthError,
73
87
  AuthField,
74
- default37 as AuthLayout,
88
+ default38 as AuthLayout,
75
89
  AuthProvider,
76
90
  AuthSubmit,
77
91
  CollaborationProvider,
78
92
  CollaboratorAvatar,
79
- default26 as CollaboratorAvatars,
80
- default33 as DashboardHeader,
93
+ default27 as CollaboratorAvatars,
94
+ default44 as ConfirmPasswordPage,
95
+ default34 as DashboardHeader,
96
+ default18 as DashboardIcon,
81
97
  DashboardProvider,
82
- default31 as DashboardShell,
83
- default32 as DashboardSidebar,
98
+ default32 as DashboardShell,
99
+ default33 as DashboardSidebar,
84
100
  default15 as DeleteButton,
85
101
  default14 as EditableFieldset,
86
102
  default17 as EmptyState,
87
- default35 as GoogleButton,
88
- default39 as GoogleCallbackPage,
103
+ default42 as ForgotPasswordPage,
104
+ default36 as GoogleButton,
105
+ default40 as GoogleCallbackPage,
89
106
  HEARTBEAT_MS,
90
- default36 as NotificationsBell,
91
- default18 as QueryError,
107
+ default37 as NotificationsBell,
108
+ default19 as QueryError,
109
+ default43 as ResetPasswordPage,
92
110
  SearchProvider,
93
- default30 as ShareModal,
111
+ default31 as ShareModal,
94
112
  SidePanelAside,
95
113
  SidePanelMobileBar,
96
114
  SidePanelNav,
97
- default38 as SignInPage,
115
+ default39 as SignInPage,
98
116
  default5 as StatCard,
99
117
  default16 as ToggleButton,
100
- default19 as UnsavedChangesStatus,
101
- default40 as VerifyEmailPage,
102
- default29 as VersionHistoryDrawer,
118
+ default45 as TwoFactorForm,
119
+ default20 as UnsavedChangesStatus,
120
+ default41 as VerifyEmailPage,
121
+ default30 as VersionHistoryDrawer,
122
+ allowNextNavigation,
103
123
  collaboratorColor,
124
+ confirmLeave,
125
+ countActiveFilterLists,
104
126
  countActiveFilters,
127
+ hasUnsavedChanges,
105
128
  initials,
106
129
  loginWithPasskey,
107
130
  moduleForPathIn,
108
131
  pageWindow,
109
132
  passkeysSupported,
110
133
  registerPasskey,
111
- default28 as useActivityPanel,
112
- default21 as useAdminAction,
134
+ default29 as useActivityPanel,
135
+ default22 as useAdminAction,
113
136
  useAdminChrome,
114
137
  useAuth,
115
138
  useBulkAdminAction,
116
139
  useCollaboration,
117
- default22 as useCrudHandlers,
140
+ useComponentDefaults,
141
+ default23 as useCrudHandlers,
118
142
  useDashboard,
119
- default24 as useFilteredItems,
143
+ default25 as useFilteredItems,
144
+ useGuardedNavigate,
145
+ useHasUnsavedChanges,
120
146
  useModules,
121
- default25 as useSavedViews,
147
+ default26 as useSavedViews,
122
148
  useSearch,
123
149
  useUnsavedChanges,
124
- default23 as useUnsavedChangesGuard
150
+ default24 as useUnsavedChangesGuard
125
151
  };
@@ -1,8 +1,63 @@
1
- import { useEffect, useState } from "react";
1
+ import { useCallback, useEffect, useId, useState, useSyncExternalStore } from "react";
2
2
  import { useNavigate } from "react-router-dom";
3
3
  import { confirmAction } from "@okjt/ui";
4
- function useUnsavedChangesGuard(dirty, message = "You have unsaved changes. Leave without saving?") {
4
+ const DEFAULT_MESSAGE = "You have unsaved changes. Leave without saving?";
5
+ const dirtyForms = /* @__PURE__ */ new Map();
6
+ const listeners = /* @__PURE__ */ new Set();
7
+ const notify = () => listeners.forEach((listener) => listener());
8
+ function reportUnsaved(key, dirty, message) {
9
+ const had = dirtyForms.has(key);
10
+ if (dirty) dirtyForms.set(key, message);
11
+ else dirtyForms.delete(key);
12
+ if (had !== dirty) notify();
13
+ }
14
+ const subscribe = (listener) => {
15
+ listeners.add(listener);
16
+ return () => listeners.delete(listener);
17
+ };
18
+ function hasUnsavedChanges() {
19
+ return dirtyForms.size > 0;
20
+ }
21
+ function useHasUnsavedChanges() {
22
+ return useSyncExternalStore(subscribe, hasUnsavedChanges, () => false);
23
+ }
24
+ let bypassUntil = 0;
25
+ function allowNextNavigation(ms = 1e3) {
26
+ bypassUntil = Date.now() + ms;
27
+ }
28
+ function takeBypass() {
29
+ if (Date.now() <= bypassUntil) {
30
+ bypassUntil = 0;
31
+ return true;
32
+ }
33
+ return false;
34
+ }
35
+ let asking = null;
36
+ function confirmLeave(message) {
37
+ if (!hasUnsavedChanges() || takeBypass()) return Promise.resolve(true);
38
+ if (asking) return asking;
39
+ const text = message || [...dirtyForms.values()][0] || DEFAULT_MESSAGE;
40
+ asking = confirmAction({ title: "Leave without saving?", message: text, confirmLabel: "Leave", tone: "danger" }).then((leave) => {
41
+ if (leave) allowNextNavigation();
42
+ return !!leave;
43
+ }).finally(() => {
44
+ asking = null;
45
+ });
46
+ return asking;
47
+ }
48
+ function useGuardedNavigate() {
5
49
  const navigate = useNavigate();
50
+ return useCallback(async (to, options) => {
51
+ if (typeof to === "number" || await confirmLeave()) navigate(to, options);
52
+ }, [navigate]);
53
+ }
54
+ function useUnsavedChangesGuard(dirty, message = DEFAULT_MESSAGE) {
55
+ const navigate = useNavigate();
56
+ const key = useId();
57
+ useEffect(() => {
58
+ reportUnsaved(key, !!dirty, message);
59
+ return () => reportUnsaved(key, false);
60
+ }, [key, dirty, message]);
6
61
  useEffect(() => {
7
62
  if (!dirty) return void 0;
8
63
  const onBeforeUnload = (e) => {
@@ -15,9 +70,11 @@ function useUnsavedChangesGuard(dirty, message = "You have unsaved changes. Leav
15
70
  if (!link || link.target === "_blank" || link.hasAttribute("download")) return;
16
71
  const url = new URL(link.href, window.location.href);
17
72
  if (url.origin !== window.location.origin || url.pathname === window.location.pathname) return;
73
+ if (takeBypass()) return;
18
74
  e.preventDefault();
19
75
  e.stopPropagation();
20
- if (await confirmAction({ title: "Leave without saving?", message, confirmLabel: "Leave", tone: "danger" })) {
76
+ if (await confirmLeave(message)) {
77
+ takeBypass();
21
78
  navigate(url.pathname + url.search + url.hash);
22
79
  }
23
80
  };
@@ -36,6 +93,11 @@ function useUnsavedChanges(value) {
36
93
  return { dirty, markSaved: setBaseline, baseline };
37
94
  }
38
95
  export {
96
+ allowNextNavigation,
97
+ confirmLeave,
39
98
  useUnsavedChangesGuard as default,
99
+ hasUnsavedChanges,
100
+ useGuardedNavigate,
101
+ useHasUnsavedChanges,
40
102
  useUnsavedChanges
41
103
  };