Files
openidea/src/workbench/Notifications.tsx
T
l0ng-ai 658c38a193 fix(app): keep manual update check answers visible (#2385)
A manual update check could end without a visible answer: balloons were hidden whenever a saved layout listed Notifications as open, even when that tool window was not actually shown (welcome screen, hidden panel), and marking the list read dropped unlogged balloons. Balloons now defer only to an effectively visible Notifications tool window, and only listed entries are marked read. Covers update available, up to date and error in project, welcome and light-edit windows. Fixes #2285.
2026-11-10 00:16:38 +08:00

187 lines
5.8 KiB
TypeScript

import { useEffect, useRef, useState, useSyncExternalStore } from "react";
import { useTxTemplate, VerbatimTextContext } from "@/platform/i18n";
import { useProject } from "@/platform/project";
import { EmptyState, IconButton, Toast, T } from "@/ui";
import {
balloonTimeout,
canSuppress,
clearNotifications,
dismissBalloon,
markAllNotificationsRead,
removeNotification,
suppressGroup,
useNotifications,
type IdeNotification,
} from "./notificationStore";
import { useEffectiveVisible, useToolWindows } from "./toolwindows/ToolWindows";
import styles from "./Notifications.module.css";
const MAX_BALLOONS = 3;
function timeLabel(t: number) {
const d = new Date(t);
return `${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`;
}
function subscribeWindowFocus(onChange: () => void) {
window.addEventListener("focus", onChange);
window.addEventListener("blur", onChange);
return () => {
window.removeEventListener("focus", onChange);
window.removeEventListener("blur", onChange);
};
}
/** Whether the app window is active (focused). */
function useWindowActive(): boolean {
return useSyncExternalStore(
subscribeWindowFocus,
() => document.hasFocus(),
() => true,
);
}
const DONT_SHOW_AGAIN = "Don't Show Again";
/** The notification's own actions, then "Don't Show Again" for its group (IDEA). */
export function notificationActions(n: IdeNotification, wrap: (run: () => void) => () => void = (r) => r) {
const own = n.actions ?? [];
if (!canSuppress(n)) return own;
return [...own, { label: DONT_SHOW_AGAIN, run: wrap(() => void suppressGroup(n.group).catch(() => {})) }];
}
/** Title and action labels in the UI language; a plugin's own text stays as written. */
function useShownText() {
const txt = useTxTemplate();
return (n: IdeNotification) => {
const given = (s: string) => (n.verbatim ? s : txt(s));
const own = n.actions?.length ?? 0;
return {
title: given(n.title),
actions: notificationActions(n).map((a, i) => ({ ...a, label: i < own ? given(a.label) : txt(a.label) })),
};
};
}
function Balloon({ n }: { n: IdeNotification }) {
const [hover, setHover] = useState(false);
const active = useWindowActive();
const remaining = useRef<number | null>(null);
const shown = useShownText()(n);
const ms = balloonTimeout(n);
// IDEA's smart fade-out: the clock runs only while the window is active and the balloon not hovered.
useEffect(() => {
if (ms === null || hover || !active) return;
const left = remaining.current ?? ms;
const start = Date.now();
const t = setTimeout(() => dismissBalloon(n.id), left);
return () => {
clearTimeout(t);
remaining.current = Math.max(1, left - (Date.now() - start));
};
}, [n.id, ms, hover, active]);
return (
<VerbatimTextContext.Provider value={!!n.verbatim}>
<Toast
className={styles.balloon}
level={n.level}
title={shown.title}
body={n.body}
actions={shown.actions.map((a) => ({
label: a.label,
run: () => {
dismissBalloon(n.id);
a.run();
},
}))}
onClose={() => dismissBalloon(n.id)}
onPointerEnter={() => setHover(true)}
onPointerLeave={() => setHover(false)}
/>
</VerbatimTextContext.Provider>
);
}
/** Bottom-right balloon stack (newest at the bottom); listed entries defer to the visible tool window. */
export function Balloons() {
const items = useNotifications((s) => s.items);
const listOpen = useListVisible();
const balloons = items
.filter((n) => n.balloon && (!listOpen || !n.logged))
.slice(0, MAX_BALLOONS)
.reverse();
if (!balloons.length) return null;
return (
<div className={styles.stack} aria-live="polite">
{balloons.map((n) => (
<Balloon key={n.id} n={n} />
))}
</div>
);
}
export const NOTIFICATIONS_TOOL_WINDOW = "notifications";
function useListVisible() {
const hasProject = useProject((s) => !!s.project);
const visible = useEffectiveVisible(useToolWindows());
return hasProject && Object.values(visible).includes(NOTIFICATIONS_TOOL_WINDOW);
}
/** The Notifications tool window (right stripe, as in IDEA): history, newest first. */
export function NotificationsToolWindow() {
const shownText = useShownText();
const all = useNotifications((s) => s.items);
const items = all.filter((n) => n.logged);
const visible = useListVisible();
const unread = items.some((n) => !n.read || n.balloon);
useEffect(() => {
if (visible && unread) markAllNotificationsRead();
}, [visible, unread]);
return (
<div className={styles.list} data-autofocus="" tabIndex={-1}>
{items.length === 0 ? (
<EmptyState icon="bell">
<T>No notifications</T>
</EmptyState>
) : (
items.map((n) => {
const shown = shownText(n);
return (
<VerbatimTextContext.Provider key={n.id} value={!!n.verbatim}>
<Toast
className={styles.entry}
level={n.level}
title={shown.title}
body={n.body}
meta={timeLabel(n.time)}
actions={shown.actions}
onClose={() => removeNotification(n.id)}
/>
</VerbatimTextContext.Provider>
);
})
)}
</div>
);
}
export function NotificationsHeaderActions() {
const count = useNotifications((s) => s.items.filter((n) => n.logged).length);
return (
<IconButton
icon="trash"
title="Clear All"
size="small"
iconSize={16}
disabled={!count}
onClick={clearNotifications}
/>
);
}
/** Unread count on the Notifications stripe button. */
export function useUnreadNotifications(): number {
return useNotifications((s) => s.items.filter((n) => !n.read && n.logged).length);
}