дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 17:21:13 +02:00
Notifications now has a Timeline heading and a Clear All button that stays visible while the history scrolls. Entries use the workbench UI text size and align their titles, timestamps, and actions. In a narrow pane the timestamp and dismiss button move below the title so longer titles can wrap at normal word boundaries. Multiline messages scroll with the history.
187 рядки
6.1 KiB
TypeScript
187 рядки
6.1 KiB
TypeScript
import { useEffect, useRef, useState, useSyncExternalStore } from "react";
|
|
import { useTxTemplate, VerbatimTextContext } from "@/platform/i18n";
|
|
import { useProject } from "@/platform/project";
|
|
import { EmptyState, 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>
|
|
) : (
|
|
<>
|
|
<div className={styles.timelineHeader}>
|
|
<h2 id="notification-timeline">
|
|
<T>Timeline</T>
|
|
</h2>
|
|
<button type="button" className={styles.clear} onClick={clearNotifications}>
|
|
<T>Clear All</T>
|
|
</button>
|
|
</div>
|
|
<ol className={styles.timeline} role="list" aria-labelledby="notification-timeline">
|
|
{items.map((n) => {
|
|
const shown = shownText(n);
|
|
return (
|
|
<li key={n.id}>
|
|
<VerbatimTextContext.Provider value={!!n.verbatim}>
|
|
<Toast
|
|
variant="history"
|
|
level={n.level}
|
|
title={shown.title}
|
|
body={n.body}
|
|
meta={<time dateTime={new Date(n.time).toISOString()}>{timeLabel(n.time)}</time>}
|
|
actions={shown.actions}
|
|
onClose={() => removeNotification(n.id)}
|
|
/>
|
|
</VerbatimTextContext.Provider>
|
|
</li>
|
|
);
|
|
})}
|
|
</ol>
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** Unread count on the Notifications stripe button. */
|
|
export function useUnreadNotifications(): number {
|
|
return useNotifications((s) => s.items.filter((n) => !n.read && n.logged).length);
|
|
}
|