Skip to content

Commit 5774323

Browse files
feat(ui): add updateDescription flag to toast API
If false, when updating a toast, the description is left alone. The count will still tick up.
1 parent 4a394c6 commit 5774323

File tree

1 file changed

+29
-5
lines changed
  • invokeai/frontend/web/src/features/toast

1 file changed

+29
-5
lines changed

invokeai/frontend/web/src/features/toast/toast.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ type ToastArg = ToastConfig & {
2222
* // second toast: 'Hello (2)'
2323
*/
2424
withCount?: boolean;
25+
/**
26+
* Whether to update the description when updating the toast. Defaults to true.
27+
* @example
28+
* // updateDescription: true
29+
* toast({ title: 'Hello', description: 'Foo' }); // Foo
30+
* toast({ title: 'Hello', description: 'Bar' }); // Bar
31+
* @example
32+
* // updateDescription: false
33+
* toast({ title: 'Hello', description: 'Foo' }); // Foo
34+
* toast({ title: 'Hello', description: 'Bar' }); // Foo
35+
*/
36+
updateDescription?: boolean;
2537
};
2638

2739
type ToastInternalState = {
@@ -48,6 +60,7 @@ const getGetState = (id: string) => () => $toastMap.get()[id] ?? null;
4860
/**
4961
* Creates a toast with the given config. If the toast with the same id already exists, it will be updated.
5062
* When a toast is updated, its title, description, status and duration will be overwritten by the new config.
63+
* Use `updateDescription: false` to keep the description when updating.
5164
* Set duration to `null` to make the toast persistent.
5265
* @param arg The toast config.
5366
* @returns An object with methods to get the toast state, close the toast and check if the toast is active
@@ -61,17 +74,20 @@ export const toast = (arg: ToastArg): ToastApi => {
6174
if (arg.withCount === undefined) {
6275
arg.withCount = true;
6376
}
77+
if (arg.updateDescription === undefined) {
78+
arg.updateDescription = true;
79+
}
6480
let state = $toastMap.get()[arg.id];
6581
if (!state) {
6682
// First time caller, create and set the state
67-
state = { id, config: parseConfig(id, arg, 1), count: 1 };
83+
state = { id, config: parseConfig(null, id, arg, 1), count: 1 };
6884
$toastMap.setKey(id, state);
6985
// Create the toast
7086
toastApi(state.config);
7187
} else {
7288
// This toast is already active, update its state
7389
state.count += 1;
74-
state.config = parseConfig(id, arg, state.count);
90+
state.config = parseConfig(state, id, arg, state.count);
7591
$toastMap.setKey(id, state);
7692
// Update the toast itself
7793
toastApi.update(id, state.config);
@@ -81,18 +97,26 @@ export const toast = (arg: ToastArg): ToastApi => {
8197

8298
/**
8399
* Give a toast id, arg and current count, returns the parsed toast config (including dynamic title and description)
100+
* @param state The current state of the toast or null if it doesn't exist
84101
* @param id The id of the toast
85102
* @param arg The arg passed to the toast function
86103
* @param count The current call count of the toast
87104
* @returns The parsed toast config
88105
*/
89-
const parseConfig = (id: string, arg: ToastArg, count: number): ToastConfig => {
90-
const title = arg.withCount && count > 1 ? `${arg.title} (${count})` : arg.title;
106+
const parseConfig = (state: ToastInternalState | null, id: string, arg: ToastArg, count: number): ToastConfig => {
91107
const onCloseComplete = () => {
92108
$toastMap.setKey(id, undefined);
93109
if (arg.onCloseComplete) {
94110
arg.onCloseComplete();
95111
}
96112
};
97-
return { ...arg, title, onCloseComplete };
113+
const title = arg.withCount && count > 1 ? `${arg.title} (${count})` : arg.title;
114+
const description = !arg.updateDescription && state ? state.config.description : arg.description;
115+
const config: ToastConfig = {
116+
...arg,
117+
title,
118+
description,
119+
onCloseComplete,
120+
};
121+
return config;
98122
};

0 commit comments

Comments
 (0)