@edenapp/solid-kit 0.15.2 → 0.16.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/README.md +59 -0
- package/package.json +14 -4
- package/src/index.ts +1 -0
- package/src/notices/NoticeProvider.test.tsx +98 -0
- package/src/notices/NoticeProvider.tsx +31 -0
- package/src/notices/NoticeRegion.tsx +125 -0
- package/src/notices/createNotices.test.ts +167 -0
- package/src/notices/createNotices.ts +217 -0
- package/src/notices.ts +22 -0
package/README.md
CHANGED
|
@@ -93,6 +93,65 @@ a stop. `run()` remains pending until cleanup finishes, then rejects with an
|
|
|
93
93
|
`AbortError` when cancelled. Filesystem copies remove incomplete destinations and
|
|
94
94
|
restore overwritten files before reaching the `cancelled` status.
|
|
95
95
|
|
|
96
|
+
## Notices
|
|
97
|
+
|
|
98
|
+
`@edenapp/solid-kit/notices` shows notices inside the app window, at the top
|
|
99
|
+
centre below the app frame. Use them for failures of actions that the user
|
|
100
|
+
started in the app, and for problems that the app observes. Use the toaster
|
|
101
|
+
(`notification` from `@edenapp/tablets`) for failures that are not owned by
|
|
102
|
+
one app.
|
|
103
|
+
|
|
104
|
+
Wrap the app in `NoticeProvider` and pass localized labels:
|
|
105
|
+
|
|
106
|
+
```tsx
|
|
107
|
+
import { NoticeProvider, useNotices } from "@edenapp/solid-kit/notices";
|
|
108
|
+
|
|
109
|
+
<NoticeProvider
|
|
110
|
+
labels={{
|
|
111
|
+
dismiss: t("common.dismiss"),
|
|
112
|
+
showMore: (hidden) => t("notices.showMore", { count: hidden }),
|
|
113
|
+
showLess: t("notices.showLess"),
|
|
114
|
+
}}
|
|
115
|
+
>
|
|
116
|
+
<App />
|
|
117
|
+
</NoticeProvider>
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Report notices with `useNotices()`:
|
|
121
|
+
|
|
122
|
+
```tsx
|
|
123
|
+
const notices = useNotices();
|
|
124
|
+
|
|
125
|
+
// A failure of an action. It stays until the user dismisses it.
|
|
126
|
+
notices.report({
|
|
127
|
+
severity: "danger",
|
|
128
|
+
title: t("offsets.writeFailed"),
|
|
129
|
+
detail: reason,
|
|
130
|
+
key: "offset:G54:X",
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// Run an action. When it throws, the error is logged and reported.
|
|
134
|
+
const file = await notices.attempt(() => openFile(path), {
|
|
135
|
+
title: t("program.openFailed"),
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// A problem that lasts while the condition is true. Call it in a component
|
|
139
|
+
// body, not after an `await` or in an event handler.
|
|
140
|
+
notices.condition(() => !connected(), {
|
|
141
|
+
severity: "warning",
|
|
142
|
+
title: t("connection.lost"),
|
|
143
|
+
});
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Severities match notification types: `danger`, `warning`, `info`, and
|
|
147
|
+
`success`. Danger and warning notices stay until the user dismisses them; info
|
|
148
|
+
and success notices hide after four seconds. A report with the `key` of a shown
|
|
149
|
+
notice replaces it in place and shows a count. Notices are listed most severe first;
|
|
150
|
+
within a severity, condition notices come first. Condition notices have no
|
|
151
|
+
dismiss button and clear when their condition is false or their owner is
|
|
152
|
+
disposed. Three notices are visible at a time; a button shows the others.
|
|
153
|
+
Screen readers announce danger notices immediately and other notices politely.
|
|
154
|
+
|
|
96
155
|
## Dialogs
|
|
97
156
|
|
|
98
157
|
`@edenapp/solid-kit/dialogs` provides local, in-renderer dialogs with a tiny promise API.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edenapp/solid-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "High-level SolidJS UI primitives for Eden apps",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -16,6 +16,11 @@
|
|
|
16
16
|
"import": "./src/dialogs.ts",
|
|
17
17
|
"default": "./src/dialogs.ts"
|
|
18
18
|
},
|
|
19
|
+
"./notices": {
|
|
20
|
+
"types": "./src/notices.ts",
|
|
21
|
+
"import": "./src/notices.ts",
|
|
22
|
+
"default": "./src/notices.ts"
|
|
23
|
+
},
|
|
19
24
|
"./sidebar": {
|
|
20
25
|
"types": "./src/sidebar.tsx",
|
|
21
26
|
"import": "./src/sidebar.tsx",
|
|
@@ -30,11 +35,15 @@
|
|
|
30
35
|
},
|
|
31
36
|
"dependencies": {
|
|
32
37
|
"solid-icons": "^1.2.0",
|
|
33
|
-
"@edenapp/client": "0.
|
|
38
|
+
"@edenapp/client": "0.16.0"
|
|
34
39
|
},
|
|
35
40
|
"devDependencies": {
|
|
41
|
+
"@solidjs/testing-library": "^0.8.10",
|
|
42
|
+
"jsdom": "^30.0.1",
|
|
36
43
|
"typescript": "^6.0.3",
|
|
37
|
-
"
|
|
44
|
+
"vite-plugin-solid": "^2.11.14",
|
|
45
|
+
"vitest": "^4.1.10",
|
|
46
|
+
"@edenapp/types": "0.16.0"
|
|
38
47
|
},
|
|
39
48
|
"keywords": [
|
|
40
49
|
"eden",
|
|
@@ -51,6 +60,7 @@
|
|
|
51
60
|
},
|
|
52
61
|
"scripts": {
|
|
53
62
|
"build": "tsc --noEmit",
|
|
54
|
-
"clean": "rm -rf dist"
|
|
63
|
+
"clean": "rm -rf dist",
|
|
64
|
+
"test": "vitest run"
|
|
55
65
|
}
|
|
56
66
|
}
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cleanup,
|
|
3
|
+
fireEvent,
|
|
4
|
+
render,
|
|
5
|
+
screen,
|
|
6
|
+
within,
|
|
7
|
+
} from "@solidjs/testing-library";
|
|
8
|
+
import { createSignal, onMount } from "solid-js";
|
|
9
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
10
|
+
import type { Notices } from "./createNotices.js";
|
|
11
|
+
import { NoticeProvider, useNotices } from "./NoticeProvider.js";
|
|
12
|
+
|
|
13
|
+
const labels = {
|
|
14
|
+
dismiss: "Dismiss",
|
|
15
|
+
showMore: (hidden: number) => `${hidden} more`,
|
|
16
|
+
showLess: "Show less",
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
describe("NoticeProvider", () => {
|
|
20
|
+
afterEach(cleanup);
|
|
21
|
+
|
|
22
|
+
it("announces danger as an alert and others politely, lets the user dismiss them, and hides extra notices", () => {
|
|
23
|
+
function Reporter() {
|
|
24
|
+
const notices = useNotices();
|
|
25
|
+
onMount(() => {
|
|
26
|
+
for (const title of ["a", "b", "c", "d"]) {
|
|
27
|
+
notices.report({ severity: "warning", title });
|
|
28
|
+
}
|
|
29
|
+
notices.report({ severity: "danger", title: "write failed" });
|
|
30
|
+
});
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
render(() => (
|
|
35
|
+
<NoticeProvider labels={labels}>
|
|
36
|
+
<Reporter />
|
|
37
|
+
</NoticeProvider>
|
|
38
|
+
));
|
|
39
|
+
|
|
40
|
+
expect(screen.getByRole("alert").textContent).toContain("write failed");
|
|
41
|
+
const announced = within(screen.getByRole("status"));
|
|
42
|
+
expect(announced.getByText("d")).toBeTruthy();
|
|
43
|
+
expect(announced.getByText("c")).toBeTruthy();
|
|
44
|
+
expect(announced.queryByText("b")).toBeNull();
|
|
45
|
+
|
|
46
|
+
fireEvent.click(screen.getByRole("button", { name: "2 more" }));
|
|
47
|
+
expect(announced.getByText("b")).toBeTruthy();
|
|
48
|
+
expect(announced.getByText("a")).toBeTruthy();
|
|
49
|
+
|
|
50
|
+
const dismissButtons = screen.getAllByRole("button", { name: "Dismiss" });
|
|
51
|
+
fireEvent.click(dismissButtons[0]);
|
|
52
|
+
expect(screen.queryByRole("alert")).toBeNull();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("keeps focus on the dismiss button when a keyed notice is replaced", () => {
|
|
56
|
+
let notices!: Notices;
|
|
57
|
+
function Reporter() {
|
|
58
|
+
notices = useNotices();
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
render(() => (
|
|
63
|
+
<NoticeProvider labels={labels}>
|
|
64
|
+
<Reporter />
|
|
65
|
+
</NoticeProvider>
|
|
66
|
+
));
|
|
67
|
+
|
|
68
|
+
notices.report({ severity: "warning", title: "limit", key: "feed" });
|
|
69
|
+
notices.report({ severity: "warning", title: "speed" });
|
|
70
|
+
const [, dismiss] = screen.getAllByRole("button", { name: "Dismiss" });
|
|
71
|
+
dismiss.focus();
|
|
72
|
+
notices.report({ severity: "warning", title: "limit again", key: "feed" });
|
|
73
|
+
|
|
74
|
+
expect(screen.getByText("limit again")).toBeTruthy();
|
|
75
|
+
expect(document.activeElement).toBe(dismiss);
|
|
76
|
+
expect(screen.getAllByRole("button", { name: "Dismiss" })[1]).toBe(dismiss);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("gives condition notices no dismiss button", () => {
|
|
80
|
+
function Condition() {
|
|
81
|
+
const [offline] = createSignal(true);
|
|
82
|
+
useNotices().condition(offline, {
|
|
83
|
+
severity: "warning",
|
|
84
|
+
title: "offline",
|
|
85
|
+
});
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
render(() => (
|
|
90
|
+
<NoticeProvider labels={labels}>
|
|
91
|
+
<Condition />
|
|
92
|
+
</NoticeProvider>
|
|
93
|
+
));
|
|
94
|
+
|
|
95
|
+
expect(screen.getByRole("status").textContent).toContain("offline");
|
|
96
|
+
expect(screen.queryByRole("button", { name: "Dismiss" })).toBeNull();
|
|
97
|
+
});
|
|
98
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createContext, type JSX, useContext } from "solid-js";
|
|
2
|
+
import { createNotices, type Notices } from "./createNotices.js";
|
|
3
|
+
import { type NoticeLabels, NoticeRegion } from "./NoticeRegion.js";
|
|
4
|
+
|
|
5
|
+
const NoticesContext = createContext<Notices>();
|
|
6
|
+
|
|
7
|
+
export interface NoticeProviderProps {
|
|
8
|
+
/** Localized labels for the notice controls. */
|
|
9
|
+
labels: NoticeLabels;
|
|
10
|
+
autoHideMs?: number;
|
|
11
|
+
children: JSX.Element;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Provides notice state to the app and shows its notices at the top centre. */
|
|
15
|
+
export function NoticeProvider(props: NoticeProviderProps) {
|
|
16
|
+
const notices = createNotices({ autoHideMs: props.autoHideMs });
|
|
17
|
+
return (
|
|
18
|
+
<NoticesContext.Provider value={notices}>
|
|
19
|
+
{props.children}
|
|
20
|
+
<NoticeRegion notices={notices} labels={props.labels} />
|
|
21
|
+
</NoticesContext.Provider>
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function useNotices(): Notices {
|
|
26
|
+
const notices = useContext(NoticesContext);
|
|
27
|
+
if (!notices) {
|
|
28
|
+
throw new Error("useNotices must be used inside NoticeProvider");
|
|
29
|
+
}
|
|
30
|
+
return notices;
|
|
31
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { IconTypes } from "solid-icons";
|
|
2
|
+
import {
|
|
3
|
+
FiAlertOctagon,
|
|
4
|
+
FiAlertTriangle,
|
|
5
|
+
FiCheckCircle,
|
|
6
|
+
FiInfo,
|
|
7
|
+
FiX,
|
|
8
|
+
} from "solid-icons/fi";
|
|
9
|
+
import { createEffect, createMemo, createSignal, For, Show } from "solid-js";
|
|
10
|
+
import { Dynamic } from "solid-js/web";
|
|
11
|
+
import type { Notice, NoticeSeverity, Notices } from "./createNotices.js";
|
|
12
|
+
|
|
13
|
+
export interface NoticeLabels {
|
|
14
|
+
/** Accessible name of the dismiss button. */
|
|
15
|
+
dismiss: string;
|
|
16
|
+
/** Text of the button that shows the hidden notices. */
|
|
17
|
+
showMore: (hidden: number) => string;
|
|
18
|
+
/** Text of the button that collapses the list again. */
|
|
19
|
+
showLess: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const NOTICE_MAX_VISIBLE = 3;
|
|
23
|
+
|
|
24
|
+
const severityIcons: Record<NoticeSeverity, IconTypes> = {
|
|
25
|
+
danger: FiAlertOctagon,
|
|
26
|
+
warning: FiAlertTriangle,
|
|
27
|
+
info: FiInfo,
|
|
28
|
+
success: FiCheckCircle,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function NoticeItem(props: {
|
|
32
|
+
notice: Notice;
|
|
33
|
+
labels: NoticeLabels;
|
|
34
|
+
onDismiss: (id: string) => void;
|
|
35
|
+
}) {
|
|
36
|
+
// Most screen readers announce an alert inserted into the page. Other
|
|
37
|
+
// notices are announced by the polite live region that contains them.
|
|
38
|
+
return (
|
|
39
|
+
<div
|
|
40
|
+
class={`eden-notice eden-notice-${props.notice.severity}`}
|
|
41
|
+
role={props.notice.severity === "danger" ? "alert" : undefined}
|
|
42
|
+
>
|
|
43
|
+
<span class="eden-notice-icon" aria-hidden="true">
|
|
44
|
+
<Dynamic component={severityIcons[props.notice.severity]} size={18} />
|
|
45
|
+
</span>
|
|
46
|
+
<div class="eden-notice-content">
|
|
47
|
+
<div class="eden-notice-title">
|
|
48
|
+
<span>{props.notice.title}</span>
|
|
49
|
+
<Show when={props.notice.count > 1}>
|
|
50
|
+
<span class="eden-badge eden-notice-count">
|
|
51
|
+
×{props.notice.count}
|
|
52
|
+
</span>
|
|
53
|
+
</Show>
|
|
54
|
+
</div>
|
|
55
|
+
<Show when={props.notice.detail}>
|
|
56
|
+
<div class="eden-notice-detail">{props.notice.detail}</div>
|
|
57
|
+
</Show>
|
|
58
|
+
</div>
|
|
59
|
+
<Show when={props.notice.kind === "event"}>
|
|
60
|
+
<button
|
|
61
|
+
type="button"
|
|
62
|
+
class="eden-btn eden-btn-ghost eden-btn-sm eden-btn-square eden-notice-dismiss"
|
|
63
|
+
aria-label={props.labels.dismiss}
|
|
64
|
+
title={props.labels.dismiss}
|
|
65
|
+
onClick={() => props.onDismiss(props.notice.id)}
|
|
66
|
+
>
|
|
67
|
+
<FiX size={16} aria-hidden="true" />
|
|
68
|
+
</button>
|
|
69
|
+
</Show>
|
|
70
|
+
</div>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function NoticeRegion(props: {
|
|
75
|
+
notices: Notices;
|
|
76
|
+
labels: NoticeLabels;
|
|
77
|
+
}) {
|
|
78
|
+
const [expanded, setExpanded] = createSignal(false);
|
|
79
|
+
const list = () => props.notices.list();
|
|
80
|
+
const visible = () =>
|
|
81
|
+
expanded() ? list() : list().slice(0, NOTICE_MAX_VISIBLE);
|
|
82
|
+
const hidden = () => list().length - visible().length;
|
|
83
|
+
const byId = createMemo(
|
|
84
|
+
() => new Map(list().map((notice) => [notice.id, notice])),
|
|
85
|
+
);
|
|
86
|
+
// Keyed by id, so a keyed replacement updates the shown element in place and
|
|
87
|
+
// keeps focus on its dismiss button.
|
|
88
|
+
const visibleIds = () => visible().map((notice) => notice.id);
|
|
89
|
+
|
|
90
|
+
createEffect(() => {
|
|
91
|
+
if (list().length <= NOTICE_MAX_VISIBLE) setExpanded(false);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<div class="eden-notice-region">
|
|
96
|
+
{/* The live region stays in the page, so screen readers announce the
|
|
97
|
+
notices added to it. aria-atomic="false" announces only the change. */}
|
|
98
|
+
<div class="eden-notice-list" role="status" aria-atomic="false">
|
|
99
|
+
<For each={visibleIds()}>
|
|
100
|
+
{(id) => (
|
|
101
|
+
<Show when={byId().get(id)}>
|
|
102
|
+
{(notice) => (
|
|
103
|
+
<NoticeItem
|
|
104
|
+
notice={notice()}
|
|
105
|
+
labels={props.labels}
|
|
106
|
+
onDismiss={props.notices.dismiss}
|
|
107
|
+
/>
|
|
108
|
+
)}
|
|
109
|
+
</Show>
|
|
110
|
+
)}
|
|
111
|
+
</For>
|
|
112
|
+
</div>
|
|
113
|
+
<Show when={hidden() > 0 || expanded()}>
|
|
114
|
+
<button
|
|
115
|
+
type="button"
|
|
116
|
+
class="eden-btn eden-btn-ghost eden-btn-sm eden-notice-more"
|
|
117
|
+
aria-expanded={expanded()}
|
|
118
|
+
onClick={() => setExpanded(!expanded())}
|
|
119
|
+
>
|
|
120
|
+
{expanded() ? props.labels.showLess : props.labels.showMore(hidden())}
|
|
121
|
+
</button>
|
|
122
|
+
</Show>
|
|
123
|
+
</div>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { createEffect, createRoot, createSignal } from "solid-js";
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
import { createNotices, type Notices } from "./createNotices.js";
|
|
4
|
+
|
|
5
|
+
const titles = (notices: Notices) => notices.list().map((n) => n.title);
|
|
6
|
+
|
|
7
|
+
describe("createNotices", () => {
|
|
8
|
+
let dispose: (() => void) | undefined;
|
|
9
|
+
const create = () =>
|
|
10
|
+
createRoot((disposeRoot) => {
|
|
11
|
+
dispose = disposeRoot;
|
|
12
|
+
return createNotices({ autoHideMs: 1_000 });
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
beforeEach(() => vi.useFakeTimers());
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
dispose?.();
|
|
18
|
+
vi.useRealTimers();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("keeps danger and warning notices until dismissed and hides info and success", () => {
|
|
22
|
+
const notices = create();
|
|
23
|
+
const dangerId = notices.report({ severity: "danger", title: "write" });
|
|
24
|
+
notices.report({ severity: "warning", title: "limit" });
|
|
25
|
+
notices.report({ severity: "info", title: "saved draft" });
|
|
26
|
+
notices.report({ severity: "success", title: "saved" });
|
|
27
|
+
|
|
28
|
+
vi.advanceTimersByTime(1_000);
|
|
29
|
+
expect(titles(notices)).toEqual(["write", "limit"]);
|
|
30
|
+
|
|
31
|
+
notices.dismiss(dangerId);
|
|
32
|
+
expect(titles(notices)).toEqual(["limit"]);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("replaces a notice with the same key in place and counts the repeats", () => {
|
|
36
|
+
const notices = create();
|
|
37
|
+
notices.report({ severity: "danger", title: "first", key: "offset" });
|
|
38
|
+
notices.report({ severity: "danger", title: "other" });
|
|
39
|
+
notices.report({ severity: "danger", title: "second", key: "offset" });
|
|
40
|
+
|
|
41
|
+
expect(notices.list()).toMatchObject([
|
|
42
|
+
{ title: "other", count: 1 },
|
|
43
|
+
{ title: "second", count: 2 },
|
|
44
|
+
]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("reports a keyed notice from an effect once per change of its source", () => {
|
|
48
|
+
const notices = create();
|
|
49
|
+
const [failure, setFailure] = createSignal("timeout");
|
|
50
|
+
let runs = 0;
|
|
51
|
+
createRoot(() =>
|
|
52
|
+
createEffect(() => {
|
|
53
|
+
runs++;
|
|
54
|
+
notices.report({
|
|
55
|
+
severity: "danger",
|
|
56
|
+
title: failure(),
|
|
57
|
+
key: "link",
|
|
58
|
+
});
|
|
59
|
+
}),
|
|
60
|
+
);
|
|
61
|
+
setFailure("refused");
|
|
62
|
+
|
|
63
|
+
expect(runs).toBe(2);
|
|
64
|
+
expect(notices.list()).toMatchObject([{ title: "refused", count: 2 }]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("restarts the hide timer when a keyed info notice repeats", () => {
|
|
68
|
+
const notices = create();
|
|
69
|
+
notices.report({ severity: "info", title: "synced", key: "sync" });
|
|
70
|
+
vi.advanceTimersByTime(800);
|
|
71
|
+
notices.report({ severity: "info", title: "synced", key: "sync" });
|
|
72
|
+
vi.advanceTimersByTime(800);
|
|
73
|
+
expect(titles(notices)).toEqual(["synced"]);
|
|
74
|
+
vi.advanceTimersByTime(200);
|
|
75
|
+
expect(titles(notices)).toEqual([]);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("shows a condition while it is true, before events of its severity, and removes it with its owner", () => {
|
|
79
|
+
const notices = create();
|
|
80
|
+
const [offline, setOffline] = createSignal(false);
|
|
81
|
+
const disposeOwner = createRoot((disposeCondition) => {
|
|
82
|
+
notices.condition(offline, { severity: "warning", title: "offline" });
|
|
83
|
+
return disposeCondition;
|
|
84
|
+
});
|
|
85
|
+
notices.report({ severity: "warning", title: "write" });
|
|
86
|
+
|
|
87
|
+
expect(titles(notices)).toEqual(["write"]);
|
|
88
|
+
setOffline(true);
|
|
89
|
+
expect(notices.list()).toMatchObject([
|
|
90
|
+
{ title: "offline", kind: "condition" },
|
|
91
|
+
{ title: "write", kind: "event" },
|
|
92
|
+
]);
|
|
93
|
+
setOffline(false);
|
|
94
|
+
expect(titles(notices)).toEqual(["write"]);
|
|
95
|
+
|
|
96
|
+
setOffline(true);
|
|
97
|
+
disposeOwner();
|
|
98
|
+
expect(titles(notices)).toEqual(["write"]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("refuses a condition without a reactive owner, which could never remove it", () => {
|
|
102
|
+
const notices = create();
|
|
103
|
+
expect(() =>
|
|
104
|
+
notices.condition(() => true, { severity: "warning", title: "offline" }),
|
|
105
|
+
).toThrow(/reactive owner/);
|
|
106
|
+
expect(notices.list()).toEqual([]);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("leaves no hide timer pending for a report after the notices are disposed", () => {
|
|
110
|
+
const notices = create();
|
|
111
|
+
dispose?.();
|
|
112
|
+
notices.report({ severity: "info", title: "saved" });
|
|
113
|
+
expect(vi.getTimerCount()).toBe(0);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("shows more severe notices first, so a failure is not hidden behind warnings", () => {
|
|
117
|
+
const notices = create();
|
|
118
|
+
createRoot(() => {
|
|
119
|
+
notices.condition(() => true, { severity: "warning", title: "offline" });
|
|
120
|
+
notices.condition(() => true, { severity: "info", title: "syncing" });
|
|
121
|
+
});
|
|
122
|
+
notices.report({ severity: "danger", title: "stop failed" });
|
|
123
|
+
notices.report({ severity: "warning", title: "invalid value" });
|
|
124
|
+
|
|
125
|
+
expect(titles(notices)).toEqual([
|
|
126
|
+
"stop failed",
|
|
127
|
+
"offline",
|
|
128
|
+
"invalid value",
|
|
129
|
+
"syncing",
|
|
130
|
+
]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("follows reactive condition content", () => {
|
|
134
|
+
const notices = create();
|
|
135
|
+
const [title, setTitle] = createSignal("offline");
|
|
136
|
+
createRoot(() =>
|
|
137
|
+
notices.condition(
|
|
138
|
+
() => true,
|
|
139
|
+
() => ({ severity: "warning", title: title() }),
|
|
140
|
+
),
|
|
141
|
+
);
|
|
142
|
+
setTitle("rozłączono");
|
|
143
|
+
expect(titles(notices)).toEqual(["rozłączono"]);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("reports and logs a thrown action failure and resolves to undefined", async () => {
|
|
147
|
+
const notices = create();
|
|
148
|
+
const log = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
149
|
+
const failure = new Error("controller rejected the write");
|
|
150
|
+
|
|
151
|
+
await expect(
|
|
152
|
+
notices.attempt(() => Promise.reject(failure), { title: "write" }),
|
|
153
|
+
).resolves.toBeUndefined();
|
|
154
|
+
await expect(
|
|
155
|
+
notices.attempt(() => Promise.resolve(7), { title: "read" }),
|
|
156
|
+
).resolves.toBe(7);
|
|
157
|
+
|
|
158
|
+
expect(notices.list()).toMatchObject([
|
|
159
|
+
{
|
|
160
|
+
severity: "danger",
|
|
161
|
+
title: "write",
|
|
162
|
+
detail: "controller rejected the write",
|
|
163
|
+
},
|
|
164
|
+
]);
|
|
165
|
+
expect(log).toHaveBeenCalledWith("write", failure);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import type { NotificationType } from "@edenapp/types";
|
|
2
|
+
import {
|
|
3
|
+
type Accessor,
|
|
4
|
+
createEffect,
|
|
5
|
+
createMemo,
|
|
6
|
+
createSignal,
|
|
7
|
+
getOwner,
|
|
8
|
+
onCleanup,
|
|
9
|
+
untrack,
|
|
10
|
+
} from "solid-js";
|
|
11
|
+
|
|
12
|
+
export type NoticeSeverity = NotificationType;
|
|
13
|
+
|
|
14
|
+
export interface NoticeContent {
|
|
15
|
+
severity: NoticeSeverity;
|
|
16
|
+
title: string;
|
|
17
|
+
detail?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface NoticeReport extends NoticeContent {
|
|
21
|
+
/** A report with the key of a shown notice replaces it and increments its count. */
|
|
22
|
+
key?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface Notice extends NoticeContent {
|
|
26
|
+
id: string;
|
|
27
|
+
kind: "event" | "condition";
|
|
28
|
+
key?: string;
|
|
29
|
+
count: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface NoticeAttemptOptions {
|
|
33
|
+
title: string;
|
|
34
|
+
key?: string;
|
|
35
|
+
severity?: "danger" | "warning";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface Notices {
|
|
39
|
+
/**
|
|
40
|
+
* Shown notices, most severe first. Within a severity, conditions come
|
|
41
|
+
* first, then events, most recently added first. A keyed repeat keeps its
|
|
42
|
+
* notice's place.
|
|
43
|
+
*/
|
|
44
|
+
readonly list: Accessor<readonly Notice[]>;
|
|
45
|
+
report(notice: NoticeReport): string;
|
|
46
|
+
dismiss(id: string): void;
|
|
47
|
+
/**
|
|
48
|
+
* Shows a notice while `when` is true. The operator cannot dismiss it. Call
|
|
49
|
+
* inside a reactive owner, such as a component body; the notice is removed
|
|
50
|
+
* when the owner is disposed. Throws when called without an owner, for
|
|
51
|
+
* example after an `await` or in an event handler.
|
|
52
|
+
*/
|
|
53
|
+
condition(
|
|
54
|
+
when: Accessor<boolean>,
|
|
55
|
+
content: NoticeContent | Accessor<NoticeContent>,
|
|
56
|
+
): void;
|
|
57
|
+
/**
|
|
58
|
+
* Runs an action. When it throws, logs the error, reports a notice with the
|
|
59
|
+
* error message as detail, and resolves to `undefined`.
|
|
60
|
+
*/
|
|
61
|
+
attempt<T>(
|
|
62
|
+
action: () => Promise<T>,
|
|
63
|
+
options: NoticeAttemptOptions,
|
|
64
|
+
): Promise<T | undefined>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface CreateNoticesOptions {
|
|
68
|
+
/** Time after which info and success notices hide. */
|
|
69
|
+
autoHideMs?: number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export const DEFAULT_NOTICE_AUTO_HIDE_MS = 4_000;
|
|
73
|
+
|
|
74
|
+
/** The more urgent a severity, the earlier its notices are shown. */
|
|
75
|
+
const SEVERITY_RANK: Record<NoticeSeverity, number> = {
|
|
76
|
+
danger: 0,
|
|
77
|
+
warning: 1,
|
|
78
|
+
info: 2,
|
|
79
|
+
success: 3,
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const hidesBySelf = (severity: NoticeSeverity) =>
|
|
83
|
+
severity === "info" || severity === "success";
|
|
84
|
+
|
|
85
|
+
const contentOf = (content: NoticeContent): NoticeContent => ({
|
|
86
|
+
severity: content.severity,
|
|
87
|
+
title: content.title,
|
|
88
|
+
detail: content.detail,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
export function errorDetail(error: unknown): string {
|
|
92
|
+
return error instanceof Error ? error.message : String(error);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Creates notice state owned by the current reactive owner. */
|
|
96
|
+
export function createNotices(options: CreateNoticesOptions = {}): Notices {
|
|
97
|
+
const autoHideMs = options.autoHideMs ?? DEFAULT_NOTICE_AUTO_HIDE_MS;
|
|
98
|
+
const [events, setEvents] = createSignal<readonly Notice[]>([]);
|
|
99
|
+
const [conditions, setConditions] = createSignal<readonly Notice[]>([]);
|
|
100
|
+
const timers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
101
|
+
let disposed = false;
|
|
102
|
+
let nextId = 0;
|
|
103
|
+
const createId = () => `eden-notice-${nextId++}`;
|
|
104
|
+
|
|
105
|
+
const clearTimer = (id: string) => {
|
|
106
|
+
const timer = timers.get(id);
|
|
107
|
+
if (timer === undefined) return;
|
|
108
|
+
clearTimeout(timer);
|
|
109
|
+
timers.delete(id);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const dismiss = (id: string) => {
|
|
113
|
+
clearTimer(id);
|
|
114
|
+
setEvents((list) => list.filter((notice) => notice.id !== id));
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const report = (input: NoticeReport) => {
|
|
118
|
+
// A report after the owner is disposed has nowhere to show.
|
|
119
|
+
if (disposed) return createId();
|
|
120
|
+
// Untracked, so that a caller inside a reactive computation does not
|
|
121
|
+
// depend on the notices it reports and re-run on its own report.
|
|
122
|
+
const existing =
|
|
123
|
+
input.key === undefined
|
|
124
|
+
? undefined
|
|
125
|
+
: untrack(events).find((notice) => notice.key === input.key);
|
|
126
|
+
const notice: Notice = {
|
|
127
|
+
...contentOf(input),
|
|
128
|
+
id: existing?.id ?? createId(),
|
|
129
|
+
kind: "event",
|
|
130
|
+
key: input.key,
|
|
131
|
+
count: (existing?.count ?? 0) + 1,
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// A repeat replaces its notice in place, so the notices around it, and
|
|
135
|
+
// the one under the pointer or keyboard focus, do not move.
|
|
136
|
+
setEvents((list) =>
|
|
137
|
+
existing
|
|
138
|
+
? list.map((shown) => (shown.id === notice.id ? notice : shown))
|
|
139
|
+
: [notice, ...list],
|
|
140
|
+
);
|
|
141
|
+
clearTimer(notice.id);
|
|
142
|
+
if (hidesBySelf(notice.severity)) {
|
|
143
|
+
timers.set(
|
|
144
|
+
notice.id,
|
|
145
|
+
setTimeout(() => dismiss(notice.id), autoHideMs),
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
return notice.id;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const removeCondition = (id: string) =>
|
|
152
|
+
setConditions((list) => list.filter((notice) => notice.id !== id));
|
|
153
|
+
|
|
154
|
+
const condition: Notices["condition"] = (when, content) => {
|
|
155
|
+
if (!getOwner()) {
|
|
156
|
+
throw new Error(
|
|
157
|
+
"notices.condition must be called inside a reactive owner, such as a component body",
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
const id = createId();
|
|
161
|
+
const read = typeof content === "function" ? content : () => content;
|
|
162
|
+
|
|
163
|
+
createEffect(() => {
|
|
164
|
+
if (!when()) {
|
|
165
|
+
removeCondition(id);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const notice: Notice = {
|
|
169
|
+
...contentOf(read()),
|
|
170
|
+
id,
|
|
171
|
+
kind: "condition",
|
|
172
|
+
count: 1,
|
|
173
|
+
};
|
|
174
|
+
setConditions((list) =>
|
|
175
|
+
list.some((shown) => shown.id === id)
|
|
176
|
+
? list.map((shown) => (shown.id === id ? notice : shown))
|
|
177
|
+
: [...list, notice],
|
|
178
|
+
);
|
|
179
|
+
});
|
|
180
|
+
onCleanup(() => removeCondition(id));
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const attempt = async <T>(
|
|
184
|
+
action: () => Promise<T>,
|
|
185
|
+
attemptOptions: NoticeAttemptOptions,
|
|
186
|
+
): Promise<T | undefined> => {
|
|
187
|
+
try {
|
|
188
|
+
return await action();
|
|
189
|
+
} catch (error) {
|
|
190
|
+
console.error(attemptOptions.title, error);
|
|
191
|
+
report({
|
|
192
|
+
severity: attemptOptions.severity ?? "danger",
|
|
193
|
+
title: attemptOptions.title,
|
|
194
|
+
detail: errorDetail(error),
|
|
195
|
+
key: attemptOptions.key,
|
|
196
|
+
});
|
|
197
|
+
return undefined;
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
onCleanup(() => {
|
|
202
|
+
disposed = true;
|
|
203
|
+
for (const timer of timers.values()) clearTimeout(timer);
|
|
204
|
+
timers.clear();
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// Array.prototype.sort is stable, so each severity keeps conditions first
|
|
208
|
+
// and events most recently added first.
|
|
209
|
+
const list = createMemo(() =>
|
|
210
|
+
[...conditions(), ...events()].sort(
|
|
211
|
+
(left, right) =>
|
|
212
|
+
SEVERITY_RANK[left.severity] - SEVERITY_RANK[right.severity],
|
|
213
|
+
),
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
return { list, report, dismiss, condition, attempt };
|
|
217
|
+
}
|
package/src/notices.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export {
|
|
2
|
+
type CreateNoticesOptions,
|
|
3
|
+
createNotices,
|
|
4
|
+
DEFAULT_NOTICE_AUTO_HIDE_MS,
|
|
5
|
+
errorDetail,
|
|
6
|
+
type Notice,
|
|
7
|
+
type NoticeAttemptOptions,
|
|
8
|
+
type NoticeContent,
|
|
9
|
+
type NoticeReport,
|
|
10
|
+
type NoticeSeverity,
|
|
11
|
+
type Notices,
|
|
12
|
+
} from "./notices/createNotices.js";
|
|
13
|
+
export {
|
|
14
|
+
NoticeProvider,
|
|
15
|
+
type NoticeProviderProps,
|
|
16
|
+
useNotices,
|
|
17
|
+
} from "./notices/NoticeProvider.js";
|
|
18
|
+
export {
|
|
19
|
+
NOTICE_MAX_VISIBLE,
|
|
20
|
+
type NoticeLabels,
|
|
21
|
+
NoticeRegion,
|
|
22
|
+
} from "./notices/NoticeRegion.js";
|