@celestia-island/hikari 0.55.351 → 0.55.352
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 +1 -1
- package/src/composables/messaging/index.ts +1 -0
- package/src/composables/messaging/messaging.test.ts +21 -0
- package/src/composables/messaging/toastTransport.ts +1 -0
- package/src/composables/messaging/types.ts +10 -0
- package/src/runtime/useToast.test.ts +169 -0
- package/src/runtime/useToast.ts +24 -4
package/package.json
CHANGED
|
@@ -146,6 +146,7 @@ export function useMessaging() {
|
|
|
146
146
|
title: options.title,
|
|
147
147
|
body: options.body,
|
|
148
148
|
tag: options.tag,
|
|
149
|
+
key: options.key,
|
|
149
150
|
requireInteraction: options.requireInteraction,
|
|
150
151
|
copyable: options.copyable ?? defaultCopyable(severity),
|
|
151
152
|
duration: options.duration ?? defaultDurationFor(severity),
|
|
@@ -67,6 +67,27 @@ describe("useMessaging", () => {
|
|
|
67
67
|
notify("manual", { severity: "error", duration: 1_000 });
|
|
68
68
|
expect(toasts[0].duration).toBe(1_000);
|
|
69
69
|
});
|
|
70
|
+
|
|
71
|
+
it("key routes the message into a dedicated keyed slot", () => {
|
|
72
|
+
const { toasts } = useToast();
|
|
73
|
+
const { notify, warning } = useMessaging();
|
|
74
|
+
// A sticky keyed warning: it must not merge with the shared warning
|
|
75
|
+
// lane a plain warning() push uses.
|
|
76
|
+
notify("machine incomplete", {
|
|
77
|
+
severity: "warning",
|
|
78
|
+
key: "shell.machine-status",
|
|
79
|
+
duration: 0,
|
|
80
|
+
});
|
|
81
|
+
warning("careful");
|
|
82
|
+
|
|
83
|
+
expect(toasts).toHaveLength(2);
|
|
84
|
+
const keyed = toasts.find((t) => t.key === "shell.machine-status")!;
|
|
85
|
+
expect(keyed.type).toBe("warning");
|
|
86
|
+
expect(keyed.duration).toBe(0);
|
|
87
|
+
expect(keyed.messages.map((m) => m.text)).toEqual(["machine incomplete"]);
|
|
88
|
+
expect(toasts.find((t) => t.key === undefined)!.messages.map((m) => m.text))
|
|
89
|
+
.toEqual(["careful"]);
|
|
90
|
+
});
|
|
70
91
|
});
|
|
71
92
|
|
|
72
93
|
describe("transport allow-list", () => {
|
|
@@ -44,6 +44,14 @@ export interface NotifyOptions {
|
|
|
44
44
|
copyable?: boolean;
|
|
45
45
|
/** Browser: dedup tag — same-tag notifications replace each other. */
|
|
46
46
|
tag?: string;
|
|
47
|
+
/**
|
|
48
|
+
* Toast: address a dedicated keyed slot instead of the shared severity
|
|
49
|
+
* slot — see ToastItem.key. The canonical use is a sticky status surface
|
|
50
|
+
* (duration `0` + a stable key) that must not merge with — or inherit the
|
|
51
|
+
* lifetime, the inline action, or the dismissal of — same-severity
|
|
52
|
+
* transient messages. Browser/native transports ignore it.
|
|
53
|
+
*/
|
|
54
|
+
key?: string;
|
|
47
55
|
/** Browser: stay open until user dismisses. Defaults to severity-driven. */
|
|
48
56
|
requireInteraction?: boolean;
|
|
49
57
|
/**
|
|
@@ -68,6 +76,8 @@ export interface MessagePayload {
|
|
|
68
76
|
title?: string;
|
|
69
77
|
body?: string;
|
|
70
78
|
tag?: string;
|
|
79
|
+
/** Toast keyed-slot identity — see NotifyOptions.key. */
|
|
80
|
+
key?: string;
|
|
71
81
|
requireInteraction?: boolean;
|
|
72
82
|
copyable?: boolean;
|
|
73
83
|
duration?: number;
|
|
@@ -132,3 +132,172 @@ describe("useToast inline actions", () => {
|
|
|
132
132
|
expect(toast.toasts.find((t) => t.type === "success")!.action).toEqual(second);
|
|
133
133
|
});
|
|
134
134
|
});
|
|
135
|
+
|
|
136
|
+
describe("useToast keyed slots", () => {
|
|
137
|
+
beforeEach(() => {
|
|
138
|
+
vi.useFakeTimers();
|
|
139
|
+
clearAll();
|
|
140
|
+
});
|
|
141
|
+
afterEach(() => {
|
|
142
|
+
clearAll();
|
|
143
|
+
vi.useRealTimers();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("a keyed push owns its own slot, not the shared type slot", () => {
|
|
147
|
+
const toast = useToast();
|
|
148
|
+
toast.error("transient boom"); // the shared error lane
|
|
149
|
+
toast.show("machine incomplete", {
|
|
150
|
+
type: "error",
|
|
151
|
+
key: "shell.machine-status",
|
|
152
|
+
duration: 0,
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
expect(toast.toasts).toHaveLength(2);
|
|
156
|
+
const keyed = toast.toasts.find((t) => t.key === "shell.machine-status")!;
|
|
157
|
+
expect(keyed.type).toBe("error");
|
|
158
|
+
expect(keyed.messages.map((m) => m.text)).toEqual(["machine incomplete"]);
|
|
159
|
+
expect(toast.toasts.find((t) => t.key === undefined)!.messages.map((m) => m.text))
|
|
160
|
+
.toEqual(["transient boom"]);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("an unkeyed push never lands in the keyed slot", () => {
|
|
164
|
+
const toast = useToast();
|
|
165
|
+
toast.show("machine incomplete", {
|
|
166
|
+
type: "warning",
|
|
167
|
+
key: "shell.machine-status",
|
|
168
|
+
duration: 0,
|
|
169
|
+
});
|
|
170
|
+
toast.warning("careful"); // transient warning — the shared lane
|
|
171
|
+
|
|
172
|
+
const keyed = toast.toasts.find((t) => t.key === "shell.machine-status")!;
|
|
173
|
+
expect(keyed.messages).toHaveLength(1);
|
|
174
|
+
expect(toast.toasts).toHaveLength(2);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("re-pushes with the same key update the same keyed slot", () => {
|
|
178
|
+
const toast = useToast();
|
|
179
|
+
toast.show("machine incomplete", { type: "warning", key: "k", duration: 0 });
|
|
180
|
+
const firstId = toast.show("machine incomplete", { type: "warning", key: "k", duration: 0 });
|
|
181
|
+
const slot = toast.toasts.find((t) => t.key === "k")!;
|
|
182
|
+
expect(slot.messages).toHaveLength(1); // identical text dedupes
|
|
183
|
+
expect(firstId).toBe(slot.messages[0]!.id);
|
|
184
|
+
|
|
185
|
+
toast.show("machine incomplete: wsl missing", { type: "warning", key: "k", duration: 0 });
|
|
186
|
+
expect(toast.toasts.find((t) => t.key === "k")!.messages.map((m) => m.text)).toEqual([
|
|
187
|
+
"machine incomplete",
|
|
188
|
+
"machine incomplete: wsl missing",
|
|
189
|
+
]);
|
|
190
|
+
expect(toast.toasts).toHaveLength(1);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("a sticky keyed slot never expires and does not poison later unkeyed slots", () => {
|
|
194
|
+
const toast = useToast();
|
|
195
|
+
// The sticky status surface: duration 0 — no timer, ever.
|
|
196
|
+
toast.show("machine incomplete", {
|
|
197
|
+
type: "warning",
|
|
198
|
+
key: "shell.machine-status",
|
|
199
|
+
duration: 0,
|
|
200
|
+
});
|
|
201
|
+
// A LATER transient warning in the shared lane keeps its own lifetime.
|
|
202
|
+
toast.warning("careful");
|
|
203
|
+
vi.advanceTimersByTime(31_000);
|
|
204
|
+
|
|
205
|
+
expect(toast.toasts).toHaveLength(1);
|
|
206
|
+
const keyed = toast.toasts[0]!;
|
|
207
|
+
expect(keyed.key).toBe("shell.machine-status");
|
|
208
|
+
expect(keyed.duration).toBe(0);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("an unkeyed slot created first does not retire the keyed sticky slot", () => {
|
|
212
|
+
const toast = useToast();
|
|
213
|
+
toast.warning("careful"); // creates the shared warning lane (30s)
|
|
214
|
+
toast.show("machine incomplete", {
|
|
215
|
+
type: "warning",
|
|
216
|
+
key: "shell.machine-status",
|
|
217
|
+
duration: 0,
|
|
218
|
+
});
|
|
219
|
+
vi.advanceTimersByTime(31_000);
|
|
220
|
+
|
|
221
|
+
// The transient lane expired; the keyed sticky slot (own timer = none)
|
|
222
|
+
// is untouched by the shared lane's clock.
|
|
223
|
+
expect(toast.toasts).toHaveLength(1);
|
|
224
|
+
expect(toast.toasts[0]!.key).toBe("shell.machine-status");
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("the keyed slot keeps its action across same-type transient pushes", () => {
|
|
228
|
+
const toast = useToast();
|
|
229
|
+
const run = vi.fn();
|
|
230
|
+
toast.show("machine incomplete", {
|
|
231
|
+
type: "warning",
|
|
232
|
+
key: "shell.machine-status",
|
|
233
|
+
duration: 0,
|
|
234
|
+
action: { label: "Open settings", onClick: run },
|
|
235
|
+
});
|
|
236
|
+
toast.warning("careful"); // a plain push clears the SHARED slot's action
|
|
237
|
+
toast.warning("careful again");
|
|
238
|
+
|
|
239
|
+
const keyed = toast.toasts.find((t) => t.key === "shell.machine-status")!;
|
|
240
|
+
expect(keyed.action?.label).toBe("Open settings");
|
|
241
|
+
expect(toast.toasts.find((t) => t.key === undefined)!.action).toBeUndefined();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("remove(message id) retires only the keyed slot", () => {
|
|
245
|
+
const toast = useToast();
|
|
246
|
+
toast.error("transient boom");
|
|
247
|
+
const msgId = toast.show("machine incomplete", {
|
|
248
|
+
type: "error",
|
|
249
|
+
key: "shell.machine-status",
|
|
250
|
+
duration: 0,
|
|
251
|
+
});
|
|
252
|
+
toast.remove(msgId);
|
|
253
|
+
|
|
254
|
+
expect(toast.toasts).toHaveLength(1);
|
|
255
|
+
expect(toast.toasts[0]!.messages[0]!.text).toBe("transient boom");
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("remove(slot id) retires the keyed slot too", () => {
|
|
259
|
+
const toast = useToast();
|
|
260
|
+
toast.show("machine incomplete", { type: "warning", key: "shell.machine-status", duration: 0 });
|
|
261
|
+
const slot = toast.toasts.find((t) => t.key === "shell.machine-status")!;
|
|
262
|
+
toast.remove(slot.id);
|
|
263
|
+
|
|
264
|
+
expect(toast.toasts.find((t) => t.key === "shell.machine-status")).toBeUndefined();
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it("the same key under a different type opens its own (key, type) slot", () => {
|
|
268
|
+
const toast = useToast();
|
|
269
|
+
toast.show("machine incomplete", { type: "warning", key: "shell.machine-status", duration: 0 });
|
|
270
|
+
toast.show("machine broken", { type: "error", key: "shell.machine-status", duration: 0 });
|
|
271
|
+
|
|
272
|
+
const slots = toast.toasts.filter((t) => t.key === "shell.machine-status");
|
|
273
|
+
expect(slots.map((t) => t.type).sort()).toEqual(["error", "warning"]);
|
|
274
|
+
// Each keeps its own tone and its own message list — no cross-tone merge.
|
|
275
|
+
expect(slots.find((t) => t.type === "warning")!.messages.map((m) => m.text))
|
|
276
|
+
.toEqual(["machine incomplete"]);
|
|
277
|
+
expect(slots.find((t) => t.type === "error")!.messages.map((m) => m.text))
|
|
278
|
+
.toEqual(["machine broken"]);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("the per-slot cap applies to a keyed slot as well", () => {
|
|
282
|
+
const toast = useToast();
|
|
283
|
+
for (let i = 0; i < MAX_TOASTS_PER_SLOT + 2; i++) {
|
|
284
|
+
toast.show(`status-${i}`, { type: "info", key: "k", duration: 0 });
|
|
285
|
+
}
|
|
286
|
+
const slot = toast.toasts.find((t) => t.key === "k")!;
|
|
287
|
+
expect(slot.messages).toHaveLength(MAX_TOASTS_PER_SLOT);
|
|
288
|
+
expect(slot.messages[0]!.text).toBe("status-2");
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it("a later same-key push cannot un-stick a sticky keyed slot", () => {
|
|
292
|
+
const toast = useToast();
|
|
293
|
+
toast.show("machine incomplete", { type: "warning", key: "k", duration: 0 });
|
|
294
|
+
// Even a push that PASSES a duration cannot rewrite the slot's clock:
|
|
295
|
+
// duration is fixed at creation (the caller owns the slot's lifetime).
|
|
296
|
+
toast.show("machine incomplete: wsl missing", { type: "warning", key: "k", duration: 30_000 });
|
|
297
|
+
vi.advanceTimersByTime(31_000);
|
|
298
|
+
|
|
299
|
+
const slot = toast.toasts.find((t) => t.key === "k")!;
|
|
300
|
+
expect(slot, "still sticky — the 30s push did not arm a timer").toBeTruthy();
|
|
301
|
+
expect(slot.duration).toBe(0);
|
|
302
|
+
});
|
|
303
|
+
});
|
package/src/runtime/useToast.ts
CHANGED
|
@@ -47,6 +47,18 @@ export interface ToastItem {
|
|
|
47
47
|
copyable?: boolean;
|
|
48
48
|
/** Inline action button — see ToastAction. */
|
|
49
49
|
action?: ToastAction;
|
|
50
|
+
/**
|
|
51
|
+
* Stable slot identity. Unkeyed pushes (the default) share ONE slot per
|
|
52
|
+
* {@link ToastType} — the dedupe/stack lane every transient call site
|
|
53
|
+
* uses. A keyed push addresses its own (key, type) slot instead, so a
|
|
54
|
+
* long-lived surface (a sticky "machine incomplete" status, an
|
|
55
|
+
* in-progress task) can sit in the same corner stack for hours WITHOUT
|
|
56
|
+
* merging with — or inheriting the lifetime, the action, or the ✕ of —
|
|
57
|
+
* same-type transient messages. The key is caller-chosen (namespaced
|
|
58
|
+
* strings recommended); the item renders with its {@link ToastType}
|
|
59
|
+
* icon/tone, which stays fixed for the slot's lifetime.
|
|
60
|
+
*/
|
|
61
|
+
key?: string;
|
|
50
62
|
}
|
|
51
63
|
|
|
52
64
|
/** Hard cap on stacked messages per toast slot. Beyond it the oldest
|
|
@@ -104,16 +116,22 @@ function scheduleAutoDismiss(slot: ToastItem) {
|
|
|
104
116
|
);
|
|
105
117
|
}
|
|
106
118
|
|
|
107
|
-
function findSlot(type: ToastType): ToastItem | undefined {
|
|
108
|
-
|
|
119
|
+
function findSlot(type: ToastType, key?: string): ToastItem | undefined {
|
|
120
|
+
if (key !== undefined) {
|
|
121
|
+
// Keyed pushes address their own (key, type) slot only — never a
|
|
122
|
+
// same-key slot of another tone, and never a transient (unkeyed)
|
|
123
|
+
// slot; the unkeyed branch below is likewise blind to keyed slots.
|
|
124
|
+
return state.toasts.find((t) => t.key === key && t.type === type);
|
|
125
|
+
}
|
|
126
|
+
return state.toasts.find((t) => t.type === type && t.key === undefined);
|
|
109
127
|
}
|
|
110
128
|
|
|
111
129
|
function push(
|
|
112
130
|
type: ToastType,
|
|
113
131
|
text: string,
|
|
114
|
-
options: { duration?: number; copyable?: boolean; action?: ToastAction } = {},
|
|
132
|
+
options: { key?: string; duration?: number; copyable?: boolean; action?: ToastAction } = {},
|
|
115
133
|
): number {
|
|
116
|
-
let slot = findSlot(type);
|
|
134
|
+
let slot = findSlot(type, options.key);
|
|
117
135
|
if (!slot) {
|
|
118
136
|
slot = {
|
|
119
137
|
id: ++nextSlotId,
|
|
@@ -122,6 +140,7 @@ function push(
|
|
|
122
140
|
duration: options.duration ?? TOAST_DURATION[type],
|
|
123
141
|
copyable: options.copyable ?? defaultCopyable(type),
|
|
124
142
|
action: options.action,
|
|
143
|
+
...(options.key !== undefined ? { key: options.key } : {}),
|
|
125
144
|
};
|
|
126
145
|
state.toasts.push(slot);
|
|
127
146
|
} else {
|
|
@@ -160,6 +179,7 @@ export function useToast() {
|
|
|
160
179
|
) {
|
|
161
180
|
const type = options.type || "info";
|
|
162
181
|
return push(type, message, {
|
|
182
|
+
key: options.key,
|
|
163
183
|
duration: options.duration,
|
|
164
184
|
copyable: options.copyable,
|
|
165
185
|
action: options.action,
|