Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
import { useEffect, useLayoutEffect, useRef, useState, useSyncExternalStore } from 'react';
import { useTranslation } from 'react-i18next';
import { cn } from '@/lib/utils';
import { type ToastItem, getToastSnapshot, subscribeToastStore } from '@/lib/toast';
import { getFileTransferToasts, subscribeFileTransferToasts } from '@/lib/fileTransferToasts';
import { Toast } from './Toast';
import { DownloadToast } from './DownloadToast';
// ────────────────────────────────────────────────────────────
// ToastFadeWrapper — 纯 opacity 淡入淡出(整块,无裁切)
// ────────────────────────────────────────────────────────────
function ToastFadeWrapper({
item,
toastId,
children,
}: {
item: ToastItem;
toastId: string;
children: React.ReactNode;
}) {
const [mounted, setMounted] = useState(false);
useEffect(() => {
const id = requestAnimationFrame(() => setMounted(true));
return () => cancelAnimationFrame(id);
}, []);
const isVisible = mounted && !item.exiting;
return (
<div className="max-w-[calc(100%_-_32px)]" data-toast-id={toastId} data-exiting={item.exiting ? 'true' : undefined}>
<div
className={cn(
'transition-opacity duration-300 ease-out',
isVisible ? 'opacity-100' : 'opacity-0',
)}
>
{children}
</div>
</div>
);
}
// ────────────────────────────────────────────────────────────
// ToastContainer — 全局容器 + FLIP 位置动画
// ────────────────────────────────────────────────────────────
export function ToastContainer() {
const { t } = useTranslation();
const items = useSyncExternalStore(subscribeToastStore, getToastSnapshot, getToastSnapshot);
const transfers = useSyncExternalStore(subscribeFileTransferToasts, getFileTransferToasts, getFileTransferToasts);
const containerRef = useRef<HTMLDivElement>(null);
const prevRectsRef = useRef<Map<string, number>>(new Map());
useLayoutEffect(() => {
const container = containerRef.current;
if (!container) return;
const wrappers = container.querySelectorAll<HTMLElement>('[data-toast-id]');
const prevTops = prevRectsRef.current;
// Step 1: 清除所有残留的 FLIP transform,确保测量的是自然布局位置
wrappers.forEach((el) => {
el.style.transform = '';
el.style.transition = 'none';
});
// Step 2: 测量每个元素的自然布局位置(无 transform 偏移)
const naturalTops = new Map<string, number>();
wrappers.forEach((el) => {
const id = el.dataset.toastId;
if (id) naturalTops.set(id, el.getBoundingClientRect().top);
});
// Step 3: 对非退场的元素做 FLIP(退场的 toast 原地淡出,不参与位移动画)
wrappers.forEach((el) => {
const id = el.dataset.toastId;
if (!id) return;
if (el.dataset.exiting === 'true') return; // ← 退场中的 toast 跳过 FLIP
const prevTop = prevTops.get(id);
if (prevTop === undefined) return; // 新元素,无旧位置可比
const newTop = naturalTops.get(id)!;
const deltaY = prevTop - newTop;
if (Math.abs(deltaY) < 1) return;
// Invert: 瞬间跳回旧位置
el.style.transform = `translateY(${deltaY}px)`;
// transition 已经在 Step 1 设为 none
// Play: 下一帧平滑滑到新位置
requestAnimationFrame(() => {
el.style.transition = 'transform 300ms ease-out';
el.style.transform = '';
});
});
// Step 4: 存储自然布局位置(不含 transform),供下次 FLIP 比较
prevRectsRef.current = naturalTops;
});
return (
<div
ref={containerRef}
aria-label={t('commonUi.toastContainer.ariaLabel')}
// z-[10100]: 高于所有 Radix Dialog (z-[10000]),避免 toast 被打开的对话框遮挡
className="pointer-events-none fixed inset-x-0 top-5 z-[10100] flex flex-col items-center gap-[10px]"
>
<DownloadToast items={transfers} />
{items.map((item) => (
<ToastFadeWrapper key={item.id} item={item} toastId={item.id}>
<Toast item={item} />
</ToastFadeWrapper>
))}
</div>
);
}