Skip to content

Commit 1fa577e

Browse files
fix(ui): auto-dismiss error toasts after 10 seconds (#1411)
1 parent 0262ac8 commit 1fa577e

3 files changed

Lines changed: 32 additions & 7 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@truefoundry/trueforge-ui': patch
3+
---
4+
5+
Auto-dismiss error toasts after 10 seconds so stacked failures like request timeouts clear without a manual close.

‎packages/trueforge-ui/src/containers/ToasterContainer.tsx‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getErrorMessage } from '../utils/getErrorMessage.js';
88

99
const MAX_TOAST_DESCRIPTION_CHARS = 480;
1010
const MAX_VISIBLE_TOASTS = 5;
11+
const ERROR_TOAST_TTL_MS = 10_000;
1112
const SUCCESS_TOAST_TTL_MS = 3000;
1213
const SDK_TIMEOUT_TOAST_TITLE = 'Request timed out';
1314
const SDK_TIMEOUT_TOAST_DESCRIPTION = 'The request took too long and was cancelled. Try again.';
@@ -67,19 +68,24 @@ export function ToasterProvider({ children }: { children: ReactNode }) {
6768
setToasts(prev => prev.filter(t => t.id !== id));
6869
}, []);
6970

70-
const showError = useCallback((error: unknown) => {
71-
console.error('[trueforge-ui]', error);
72-
const item: ToastItem = { id: nextToastId(), variant: 'error', ...normalizeError(error) };
73-
setToasts(prev => [...prev, item].slice(-MAX_VISIBLE_TOASTS));
74-
}, []);
71+
const showError = useCallback(
72+
(error: unknown) => {
73+
console.error('[trueforge-ui]', error);
74+
const id = nextToastId();
75+
const item: ToastItem = { id, variant: 'error', ...normalizeError(error) };
76+
setToasts(prev => [...prev, item].slice(-MAX_VISIBLE_TOASTS));
77+
if (typeof window !== 'undefined') {
78+
window.setTimeout(() => dismiss(id), ERROR_TOAST_TTL_MS);
79+
}
80+
},
81+
[dismiss],
82+
);
7583

7684
const showSuccess = useCallback(
7785
(content: ToastContent) => {
7886
const id = nextToastId();
7987
const item: ToastItem = { id, variant: 'success', ...content };
8088
setToasts(prev => [...prev, item].slice(-MAX_VISIBLE_TOASTS));
81-
// Success is a transient confirmation — auto-dismiss so it doesn't linger. Errors persist
82-
// (a dev may need to read or copy them).
8389
if (typeof window !== 'undefined') {
8490
window.setTimeout(() => dismiss(id), SUCCESS_TOAST_TTL_MS);
8591
}

‎packages/trueforge-ui/test/containers/ToasterContainer.test.tsx‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe('ToasterProvider', () => {
3737
});
3838

3939
afterEach(() => {
40+
vi.useRealTimers();
4041
vi.restoreAllMocks();
4142
});
4243

@@ -77,6 +78,19 @@ describe('ToasterProvider', () => {
7778
expect(screen.getByText('keep-me')).toBeTruthy();
7879
});
7980

81+
it('auto-dismisses error toasts after 10 seconds', async () => {
82+
vi.useFakeTimers();
83+
renderToaster(<Trigger errors={[new Error('stale-error')]} />);
84+
85+
fireEvent.click(screen.getByRole('button', { name: 'boom' }));
86+
expect(screen.getByText('stale-error')).toBeTruthy();
87+
88+
await act(async () => {
89+
vi.advanceTimersByTime(10_000);
90+
});
91+
expect(screen.queryByText('stale-error')).toBeNull();
92+
});
93+
8094
it('formats HTTP-like status and response body details', async () => {
8195
const httpError = Object.assign(new Error('request failed'), {
8296
statusCode: 422,

0 commit comments

Comments
 (0)