@umituz/web-dashboard 6.9.0 → 6.11.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.
- package/package.json +15 -11
- package/src/domains/layouts/components/DashboardHeader.tsx +1 -1
- package/src/domains/layouts/components/DashboardLayout.test.tsx +1 -1
- package/src/domains/layouts/components/DashboardLayout.tsx +41 -5
- package/src/domains/layouts/components/DashboardSidebar.tsx +1 -1
- package/src/domains/layouts/components/PageHeader.test.tsx +15 -0
- package/src/domains/layouts/components/PageHeader.tsx +2 -1
- package/src/domains/layouts/types/layout.ts +5 -0
- package/src/domains/search/index.ts +2 -0
- package/src/domains/search/searchItems.test.ts +42 -0
- package/src/domains/search/searchItems.ts +36 -0
- package/src/domains/settings/components/SettingsLayout.tsx +1 -1
- package/src/domains/settings/components/SettingsSection.tsx +1 -1
- package/src/domains/timing/hooks/index.ts +1 -0
- package/src/domains/timing/hooks/useCountdown.test.ts +107 -0
- package/src/domains/timing/hooks/useCountdown.ts +75 -0
- package/src/domains/timing/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-dashboard",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.11.0",
|
|
4
4
|
"description": "Installable app skeleton for the fleet — layouts, analytics (dependency-free SVG charts), calendar, onboarding flow, i18n via @umituz/web-localization, permissions/module registry; udb-* stylesheet on design-system tokens",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -39,6 +39,10 @@
|
|
|
39
39
|
"./settings/hooks": "./src/domains/settings/hooks/index.ts",
|
|
40
40
|
"./settings/types": "./src/domains/settings/types/index.ts",
|
|
41
41
|
"./settings/utils": "./src/domains/settings/utils/index.ts",
|
|
42
|
+
"./timing": "./src/domains/timing/index.ts",
|
|
43
|
+
"./timing/hooks": "./src/domains/timing/hooks/index.ts",
|
|
44
|
+
"./search": "./src/domains/search/index.ts",
|
|
45
|
+
"./search/utils": "./src/domains/search/searchItems.ts",
|
|
42
46
|
"./styles.css": "./src/styles/dashboard.css"
|
|
43
47
|
},
|
|
44
48
|
"files": [
|
|
@@ -55,10 +59,10 @@
|
|
|
55
59
|
"version:major": "npm version major -m 'chore: release v%s'"
|
|
56
60
|
},
|
|
57
61
|
"peerDependencies": {
|
|
62
|
+
"framer-motion": "^12.0.0",
|
|
58
63
|
"react": "^18.0.0 || ^19.0.0",
|
|
59
64
|
"react-dom": "^18.0.0 || ^19.0.0",
|
|
60
|
-
"react-router
|
|
61
|
-
"framer-motion": "^12.0.0"
|
|
65
|
+
"react-router": "^6.0.0 || ^7.0.0"
|
|
62
66
|
},
|
|
63
67
|
"peerDependenciesMeta": {
|
|
64
68
|
"framer-motion": {
|
|
@@ -66,24 +70,24 @@
|
|
|
66
70
|
}
|
|
67
71
|
},
|
|
68
72
|
"dependencies": {
|
|
69
|
-
"@umituz/web-
|
|
70
|
-
"lucide-react": "^0.577.0"
|
|
71
|
-
"@umituz/web-localization": "^1.2.1"
|
|
73
|
+
"@umituz/web-localization": "^1.2.1",
|
|
74
|
+
"lucide-react": "^0.577.0"
|
|
72
75
|
},
|
|
73
76
|
"devDependencies": {
|
|
77
|
+
"@testing-library/dom": "^10.4.2",
|
|
78
|
+
"@testing-library/react": "^16.3.3",
|
|
74
79
|
"@types/node": "^22.20.5",
|
|
75
80
|
"@types/react": "^19.2.14",
|
|
76
81
|
"@types/react-dom": "^19.2.3",
|
|
77
82
|
"@typescript-eslint/eslint-plugin": "^8.57.2",
|
|
78
83
|
"@typescript-eslint/parser": "^8.57.2",
|
|
84
|
+
"@umituz/web-design-system": "^4.21.0",
|
|
79
85
|
"eslint": "^10.0.2",
|
|
80
86
|
"framer-motion": "^12.23.12",
|
|
81
|
-
"
|
|
87
|
+
"jsdom": "^29.1.1",
|
|
88
|
+
"react-router": "^7.13.1",
|
|
82
89
|
"typescript": "^5.9.3",
|
|
83
|
-
"vitest": "^5.0.1"
|
|
84
|
-
"@testing-library/dom": "^10.4.2",
|
|
85
|
-
"@testing-library/react": "^16.3.3",
|
|
86
|
-
"jsdom": "^29.1.1"
|
|
90
|
+
"vitest": "^5.0.1"
|
|
87
91
|
},
|
|
88
92
|
"keywords": [
|
|
89
93
|
"dashboard",
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
DropdownMenu, DropdownMenuTrigger, DropdownMenuContent,
|
|
9
9
|
DropdownMenuItem, DropdownMenuSeparator
|
|
10
10
|
} from "@umituz/web-design-system/organisms";
|
|
11
|
-
import { useNavigate } from "react-router
|
|
11
|
+
import { useNavigate } from "react-router";
|
|
12
12
|
import { useT } from "@umituz/web-localization/react";
|
|
13
13
|
import type { ReactNode } from "react";
|
|
14
14
|
import type {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, expect, it, vi } from "vitest";
|
|
2
2
|
import { render, screen, fireEvent } from "@testing-library/react";
|
|
3
|
-
import { MemoryRouter, Routes, Route } from "react-router
|
|
3
|
+
import { MemoryRouter, Routes, Route } from "react-router";
|
|
4
4
|
import { Home } from "lucide-react";
|
|
5
5
|
import { DashboardLayout } from "./DashboardLayout";
|
|
6
6
|
import type { DashboardLayoutConfig } from "../types/layout";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { useState, useEffect, type ReactNode } from "react";
|
|
2
|
-
import { useLocation, useNavigate, Outlet, Navigate } from "react-router
|
|
1
|
+
import { useState, useEffect, useRef, type ReactNode } from "react";
|
|
2
|
+
import { useLocation, useNavigate, Outlet, Navigate } from "react-router";
|
|
3
3
|
import { useT } from "@umituz/web-localization/react";
|
|
4
4
|
import { Skeleton } from "@umituz/web-design-system/atoms";
|
|
5
|
-
import { CommandPalette } from "@umituz/web-design-system/organisms";
|
|
5
|
+
import { CommandPalette, type CommandGroup, type CommandItem } from "@umituz/web-design-system/organisms";
|
|
6
6
|
import { DashboardSidebar } from "./DashboardSidebar";
|
|
7
7
|
import { DashboardHeader } from "./DashboardHeader";
|
|
8
8
|
import { getActiveSidebarItem } from "../utils/dashboard";
|
|
@@ -67,7 +67,11 @@ export const DashboardLayout = ({
|
|
|
67
67
|
const [mobileOpen, setMobileOpen] = useState(false);
|
|
68
68
|
const [loading, setLoading] = useState(routeTransitionMs > 0);
|
|
69
69
|
const [paletteOpen, setPaletteOpen] = useState(false);
|
|
70
|
+
const [searchItems, setSearchItems] = useState<CommandItem[]>([]);
|
|
71
|
+
const searchTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
72
|
+
const searchSeq = useRef(0);
|
|
70
73
|
const palette = config.commandPalette;
|
|
74
|
+
const paletteSearch = palette?.search;
|
|
71
75
|
|
|
72
76
|
useEffect(() => {
|
|
73
77
|
if (!collapsedStorageKey || typeof window === "undefined") return;
|
|
@@ -113,6 +117,31 @@ export const DashboardLayout = ({
|
|
|
113
117
|
return () => window.removeEventListener("keydown", onKeyDown);
|
|
114
118
|
}, [mobileOpen]);
|
|
115
119
|
|
|
120
|
+
useEffect(
|
|
121
|
+
() => () => {
|
|
122
|
+
if (searchTimer.current) clearTimeout(searchTimer.current);
|
|
123
|
+
},
|
|
124
|
+
[]
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const handlePaletteQuery = (q: string) => {
|
|
128
|
+
if (searchTimer.current) clearTimeout(searchTimer.current);
|
|
129
|
+
const seq = ++searchSeq.current;
|
|
130
|
+
if (!paletteSearch || q.trim() === "") {
|
|
131
|
+
setSearchItems([]);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
searchTimer.current = setTimeout(() => {
|
|
135
|
+
paletteSearch(q)
|
|
136
|
+
.then((items) => {
|
|
137
|
+
if (seq === searchSeq.current) setSearchItems(items);
|
|
138
|
+
})
|
|
139
|
+
.catch(() => {
|
|
140
|
+
if (seq === searchSeq.current) setSearchItems([]);
|
|
141
|
+
});
|
|
142
|
+
}, 180);
|
|
143
|
+
};
|
|
144
|
+
|
|
116
145
|
useEffect(() => {
|
|
117
146
|
if (typeof document === "undefined") return;
|
|
118
147
|
document.documentElement.classList.toggle("udb-scroll-lock", mobileOpen);
|
|
@@ -133,10 +162,16 @@ export const DashboardLayout = ({
|
|
|
133
162
|
|
|
134
163
|
const currentTitle = getTitle();
|
|
135
164
|
|
|
165
|
+
const searchGroup: CommandGroup | null =
|
|
166
|
+
paletteSearch && searchItems.length > 0
|
|
167
|
+
? { heading: palette?.searchResultsHeading ?? t("Search results"), items: searchItems }
|
|
168
|
+
: null;
|
|
169
|
+
const basePaletteGroups = searchGroup ? [searchGroup, ...(palette?.groups ?? [])] : (palette?.groups ?? []);
|
|
170
|
+
|
|
136
171
|
// Sidebar-derived navigation groups — one nav definition feeds both chrome surfaces.
|
|
137
172
|
const paletteGroups = palette?.includeSidebar
|
|
138
173
|
? [
|
|
139
|
-
...
|
|
174
|
+
...basePaletteGroups,
|
|
140
175
|
...config.sidebarGroups.map((group) => ({
|
|
141
176
|
heading: group.title,
|
|
142
177
|
items: group.items.map((item) => ({
|
|
@@ -243,10 +278,11 @@ export const DashboardLayout = ({
|
|
|
243
278
|
<CommandPalette
|
|
244
279
|
open={paletteOpen}
|
|
245
280
|
onOpenChange={setPaletteOpen}
|
|
246
|
-
groups={paletteGroups ??
|
|
281
|
+
groups={paletteGroups ?? basePaletteGroups}
|
|
247
282
|
placeholder={palette.placeholder}
|
|
248
283
|
emptyLabel={palette.emptyLabel}
|
|
249
284
|
label={palette.label}
|
|
285
|
+
onQueryChange={paletteSearch ? handlePaletteQuery : undefined}
|
|
250
286
|
/>
|
|
251
287
|
)}
|
|
252
288
|
</div>
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useState, type ReactNode } from "react";
|
|
2
|
-
import { Link, useLocation } from "react-router
|
|
2
|
+
import { Link, useLocation } from "react-router";
|
|
3
3
|
import { useT } from "@umituz/web-localization/react";
|
|
4
4
|
|
|
5
5
|
import { Button } from "@umituz/web-design-system/atoms";
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest';
|
|
2
2
|
import { renderToString } from 'react-dom/server';
|
|
3
|
+
import { render } from '@testing-library/react';
|
|
4
|
+
import { MemoryRouter } from 'react-router';
|
|
3
5
|
import { PageHeader } from './PageHeader';
|
|
4
6
|
|
|
5
7
|
describe('PageHeader markup smoke', () => {
|
|
@@ -30,6 +32,19 @@ describe('PageHeader markup smoke', () => {
|
|
|
30
32
|
expect(html).toContain('New claim');
|
|
31
33
|
});
|
|
32
34
|
|
|
35
|
+
it('renders crumb lists as in-app links', () => {
|
|
36
|
+
const { container } = render(
|
|
37
|
+
<MemoryRouter>
|
|
38
|
+
<PageHeader
|
|
39
|
+
title="Claim 92"
|
|
40
|
+
breadcrumbs={[{ label: 'Policies', to: '/policies' }, { label: 'Claim 92' }]}
|
|
41
|
+
/>
|
|
42
|
+
</MemoryRouter>,
|
|
43
|
+
);
|
|
44
|
+
expect(container.querySelectorAll("a[href='/policies']")).toHaveLength(1);
|
|
45
|
+
expect(container.querySelector('.udb-pageheader__crumb a')?.textContent).toBe('Policies');
|
|
46
|
+
});
|
|
47
|
+
|
|
33
48
|
it('renders meta under the title block and omits it when undefined', () => {
|
|
34
49
|
const html = renderToString(
|
|
35
50
|
<PageHeader title="Policy AC-123" meta={<span>Renews 2027-01-15</span>} />,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ReactNode } from "react";
|
|
2
|
+
import { Link } from "react-router";
|
|
2
3
|
|
|
3
4
|
export interface PageHeaderCrumb {
|
|
4
5
|
label: string;
|
|
@@ -36,7 +37,7 @@ export const PageHeader = ({
|
|
|
36
37
|
: isCrumbList(breadcrumbs)
|
|
37
38
|
? breadcrumbs.map((crumb, index) => (
|
|
38
39
|
<span key={`${crumb.label}-${index}`} className="udb-pageheader__crumb">
|
|
39
|
-
{crumb.to !== undefined ? <
|
|
40
|
+
{crumb.to !== undefined ? <Link to={crumb.to}>{crumb.label}</Link> : crumb.label}
|
|
40
41
|
</span>
|
|
41
42
|
))
|
|
42
43
|
: breadcrumbs;
|
|
@@ -45,6 +45,11 @@ export interface CommandPaletteConfig {
|
|
|
45
45
|
/** Append the sidebar groups as navigation groups — derived from
|
|
46
46
|
* sidebarGroups and routed internally, so apps never map their nav twice. */
|
|
47
47
|
includeSidebar?: boolean;
|
|
48
|
+
/** Async search — typed queries are debounced and the results render as a
|
|
49
|
+
* dedicated group above the static ones (items carry their own onSelect). */
|
|
50
|
+
search?: (q: string) => Promise<import("@umituz/web-design-system/organisms").CommandItem[]>;
|
|
51
|
+
/** Heading for the async search result group. */
|
|
52
|
+
searchResultsHeading?: string;
|
|
48
53
|
}
|
|
49
54
|
|
|
50
55
|
export interface DashboardSidebarProps {
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { searchItems } from './searchItems';
|
|
3
|
+
|
|
4
|
+
interface Course {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
blurb: string;
|
|
8
|
+
level: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const COURSES: Course[] = [
|
|
12
|
+
{ id: 'c1', name: 'Present perfect workshop', blurb: 'Have you ever…', level: 'B1' },
|
|
13
|
+
{ id: 'c2', name: 'IELTS reading sprint', blurb: 'Skimming strategies', level: 'C1' },
|
|
14
|
+
{ id: 'c3', name: 'Everyday vocabulary', blurb: 'Present simple review', level: 'A1' },
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
const toHit = (c: Course) => ({ id: c.id, title: c.name, subtitle: c.blurb, category: c.level, to: `/courses/${c.id}` });
|
|
18
|
+
|
|
19
|
+
describe('searchItems', () => {
|
|
20
|
+
it('returns nothing for empty or blank queries', () => {
|
|
21
|
+
expect(searchItems(COURSES, '', toHit)).toEqual([]);
|
|
22
|
+
expect(searchItems(COURSES, ' ', toHit)).toEqual([]);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('matches titles case-insensitively and returns route hits', () => {
|
|
26
|
+
const hits = searchItems(COURSES, 'ielts', toHit);
|
|
27
|
+
expect(hits).toHaveLength(1);
|
|
28
|
+
expect(hits[0]).toMatchObject({ id: 'c2', title: 'IELTS reading sprint', to: '/courses/c2' });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('ranks title matches above body-only matches', () => {
|
|
32
|
+
// "present" is in c1's title and c3's blurb — title wins.
|
|
33
|
+
const hits = searchItems(COURSES, 'present', toHit);
|
|
34
|
+
expect(hits[0]?.id).toBe('c1');
|
|
35
|
+
expect(hits).toHaveLength(2);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('scores multi-term queries across fields', () => {
|
|
39
|
+
const hits = searchItems(COURSES, 'reading strategies', toHit);
|
|
40
|
+
expect(hits[0]?.id).toBe('c2');
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export interface SearchHit {
|
|
2
|
+
id: string;
|
|
3
|
+
title: string;
|
|
4
|
+
subtitle: string;
|
|
5
|
+
category: string;
|
|
6
|
+
to: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Case-insensitive substring scoring across a haystack of fields.
|
|
10
|
+
* Title matches weigh 3×; every additional field hit adds 1. Empty
|
|
11
|
+
* queries return no hits. Results are sorted by descending score. */
|
|
12
|
+
export function searchItems<T extends object>(
|
|
13
|
+
items: readonly T[],
|
|
14
|
+
query: string,
|
|
15
|
+
toHit: (item: T) => Omit<SearchHit, 'id'> & { id?: string } & Record<string, unknown>,
|
|
16
|
+
): SearchHit[] {
|
|
17
|
+
const q = query.trim().toLowerCase();
|
|
18
|
+
if (!q) return [];
|
|
19
|
+
const terms = q.split(/\s+/);
|
|
20
|
+
const hits: { hit: SearchHit; score: number }[] = [];
|
|
21
|
+
for (const item of items) {
|
|
22
|
+
const built = toHit(item);
|
|
23
|
+
const haystack =
|
|
24
|
+
`${built.title} ${built.subtitle} ${built.category} ${Object.values(item).filter((v) => typeof v === 'string').join(' ')}`.toLowerCase();
|
|
25
|
+
let score = 0;
|
|
26
|
+
for (const term of terms) {
|
|
27
|
+
if (built.title.toLowerCase().includes(term)) score += 3;
|
|
28
|
+
if (haystack.includes(term)) score += 1;
|
|
29
|
+
}
|
|
30
|
+
if (score > 0) {
|
|
31
|
+
const { title, subtitle, category, to, ...rest } = built;
|
|
32
|
+
hits.push({ hit: { id: (rest.id as string) ?? title, title, subtitle, category, to }, score });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return hits.sort((a, b) => b.score - a.score).map((h) => h.hit);
|
|
36
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEffect, useState } from "react";
|
|
2
|
-
import { Outlet, useLocation, useNavigate } from "react-router
|
|
2
|
+
import { Outlet, useLocation, useNavigate } from "react-router";
|
|
3
3
|
import { Skeleton, Button } from "@umituz/web-design-system/atoms";
|
|
4
4
|
import { ChevronLeft, Menu } from "lucide-react";
|
|
5
5
|
import { SettingsSection } from "./SettingsSection";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useMemo } from "react";
|
|
2
|
-
import { Link } from "react-router
|
|
2
|
+
import { Link } from "react-router";
|
|
3
3
|
import { ChevronRight } from "lucide-react";
|
|
4
4
|
import { cn } from "@umituz/web-design-system/utils/cn";
|
|
5
5
|
import type { SettingsSection as SettingsSectionType } from "../types/settings";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useCountdown, useStopwatch } from './useCountdown';
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { act, renderHook } from '@testing-library/react';
|
|
3
|
+
import { useCountdown, useStopwatch } from './useCountdown';
|
|
4
|
+
|
|
5
|
+
describe('useCountdown', () => {
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
vi.useFakeTimers();
|
|
8
|
+
});
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
vi.useRealTimers();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('ticks down once per second while running', () => {
|
|
14
|
+
const { result } = renderHook(() => useCountdown(3));
|
|
15
|
+
expect(result.current.remaining).toBe(3);
|
|
16
|
+
act(() => {
|
|
17
|
+
vi.advanceTimersByTime(2000);
|
|
18
|
+
});
|
|
19
|
+
expect(result.current.remaining).toBe(1);
|
|
20
|
+
expect(result.current.running).toBe(true);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('fires onFinish exactly once at zero and stops', () => {
|
|
24
|
+
const onFinish = vi.fn();
|
|
25
|
+
const { result } = renderHook(() => useCountdown(2, onFinish));
|
|
26
|
+
act(() => {
|
|
27
|
+
vi.advanceTimersByTime(2000);
|
|
28
|
+
});
|
|
29
|
+
expect(result.current.remaining).toBe(0);
|
|
30
|
+
expect(result.current.running).toBe(false);
|
|
31
|
+
expect(onFinish).toHaveBeenCalledTimes(1);
|
|
32
|
+
act(() => {
|
|
33
|
+
vi.advanceTimersByTime(5000);
|
|
34
|
+
});
|
|
35
|
+
expect(onFinish).toHaveBeenCalledTimes(1);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('restart rewinds the clock and re-arms completion', () => {
|
|
39
|
+
const onFinish = vi.fn();
|
|
40
|
+
const { result } = renderHook(() => useCountdown(1, onFinish));
|
|
41
|
+
act(() => {
|
|
42
|
+
vi.advanceTimersByTime(1000);
|
|
43
|
+
});
|
|
44
|
+
expect(onFinish).toHaveBeenCalledTimes(1);
|
|
45
|
+
act(() => {
|
|
46
|
+
result.current.restart();
|
|
47
|
+
});
|
|
48
|
+
expect(result.current.remaining).toBe(1);
|
|
49
|
+
expect(result.current.running).toBe(true);
|
|
50
|
+
act(() => {
|
|
51
|
+
vi.advanceTimersByTime(1000);
|
|
52
|
+
});
|
|
53
|
+
expect(onFinish).toHaveBeenCalledTimes(2);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('pause halts the tick and start resumes it', () => {
|
|
57
|
+
const { result } = renderHook(() => useCountdown(5));
|
|
58
|
+
// state change and time travel must be separate acts — the interval
|
|
59
|
+
// effect only re-registers after the pause() commit
|
|
60
|
+
act(() => {
|
|
61
|
+
result.current.pause();
|
|
62
|
+
});
|
|
63
|
+
act(() => {
|
|
64
|
+
vi.advanceTimersByTime(3000);
|
|
65
|
+
});
|
|
66
|
+
expect(result.current.remaining).toBe(5);
|
|
67
|
+
act(() => {
|
|
68
|
+
result.current.start();
|
|
69
|
+
});
|
|
70
|
+
act(() => {
|
|
71
|
+
vi.advanceTimersByTime(1000);
|
|
72
|
+
});
|
|
73
|
+
expect(result.current.remaining).toBe(4);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('useStopwatch', () => {
|
|
78
|
+
beforeEach(() => {
|
|
79
|
+
vi.useFakeTimers();
|
|
80
|
+
});
|
|
81
|
+
afterEach(() => {
|
|
82
|
+
vi.useRealTimers();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('counts up after start and freezes on stop', () => {
|
|
86
|
+
const { result } = renderHook(() => useStopwatch());
|
|
87
|
+
expect(result.current.elapsed).toBe(0);
|
|
88
|
+
act(() => {
|
|
89
|
+
result.current.start();
|
|
90
|
+
});
|
|
91
|
+
act(() => {
|
|
92
|
+
vi.advanceTimersByTime(3000);
|
|
93
|
+
});
|
|
94
|
+
expect(result.current.elapsed).toBe(3);
|
|
95
|
+
act(() => {
|
|
96
|
+
result.current.stop();
|
|
97
|
+
});
|
|
98
|
+
act(() => {
|
|
99
|
+
vi.advanceTimersByTime(3000);
|
|
100
|
+
});
|
|
101
|
+
expect(result.current.elapsed).toBe(3);
|
|
102
|
+
act(() => {
|
|
103
|
+
result.current.reset();
|
|
104
|
+
});
|
|
105
|
+
expect(result.current.elapsed).toBe(0);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
/** Ticking countdown in seconds; fires onFinish once at zero. */
|
|
4
|
+
export function useCountdown(seconds: number | null, onFinish?: () => void) {
|
|
5
|
+
const [remaining, setRemaining] = useState(seconds ?? 0);
|
|
6
|
+
const [running, setRunning] = useState(seconds != null);
|
|
7
|
+
const finishedRef = useRef(false);
|
|
8
|
+
const onFinishRef = useRef(onFinish);
|
|
9
|
+
onFinishRef.current = onFinish;
|
|
10
|
+
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
setRemaining(seconds ?? 0);
|
|
13
|
+
setRunning(seconds != null);
|
|
14
|
+
finishedRef.current = false;
|
|
15
|
+
}, [seconds]);
|
|
16
|
+
|
|
17
|
+
const restart = () => {
|
|
18
|
+
finishedRef.current = false;
|
|
19
|
+
setRemaining(seconds ?? 0);
|
|
20
|
+
setRunning(seconds != null);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
if (!running) return;
|
|
25
|
+
const id = window.setInterval(() => {
|
|
26
|
+
setRemaining((r) => (r <= 1 ? 0 : r - 1));
|
|
27
|
+
}, 1000);
|
|
28
|
+
return () => window.clearInterval(id);
|
|
29
|
+
}, [running]);
|
|
30
|
+
|
|
31
|
+
// Completion fires in an effect, not inside the setRemaining updater —
|
|
32
|
+
// updaters must stay pure (StrictMode double-invokes them).
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
if (running && remaining === 0 && !finishedRef.current) {
|
|
35
|
+
finishedRef.current = true;
|
|
36
|
+
setRunning(false);
|
|
37
|
+
onFinishRef.current?.();
|
|
38
|
+
}
|
|
39
|
+
}, [running, remaining]);
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
remaining,
|
|
43
|
+
running,
|
|
44
|
+
start: () => setRunning(true),
|
|
45
|
+
pause: () => setRunning(false),
|
|
46
|
+
stop: () => {
|
|
47
|
+
setRunning(false);
|
|
48
|
+
finishedRef.current = true;
|
|
49
|
+
},
|
|
50
|
+
restart,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Stopwatch for open practice (writing time, speaking response windows). */
|
|
55
|
+
export function useStopwatch() {
|
|
56
|
+
const [elapsed, setElapsed] = useState(0);
|
|
57
|
+
const [running, setRunning] = useState(false);
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
if (!running) return;
|
|
61
|
+
const id = window.setInterval(() => setElapsed((e) => e + 1), 1000);
|
|
62
|
+
return () => window.clearInterval(id);
|
|
63
|
+
}, [running]);
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
elapsed,
|
|
67
|
+
running,
|
|
68
|
+
start: () => setRunning(true),
|
|
69
|
+
stop: () => setRunning(false),
|
|
70
|
+
reset: () => {
|
|
71
|
+
setRunning(false);
|
|
72
|
+
setElapsed(0);
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './hooks';
|