Skip to content

Open-source, local-first AI agent for real work.

2.0 KB
Raw Blame
import type { BrowserCapture } from "../../../shared/browser-capture";
import { useAtom } from "jotai";
import { useRef, useState } from "react";
import { BrowserCaptureDialog } from "./BrowserCaptureDialog";
import { AttachmentThumbnail } from "./AttachmentThumbnail";
import { useTranslation } from "react-i18next";
import { toVettaFileUrl } from "@shared/lib/utils";
import { browserCapturesAtom } from "@shared/store/atoms";

export function BrowserCaptureDraftCards() {
	const [captures, setCaptures] = useAtom(browserCapturesAtom);
	const { t } = useTranslation("chat");
	return (
		<>
			{captures.map((capture) => (
				<DraftThumbnail
					key={capture.id}
					capture={capture}
					removeLabel={t("browserCapture.remove")}
					onRemove={() => setCaptures((items) => items.filter((item) => item.id !== capture.id))}
					onSave={(note) =>
						setCaptures((items) => items.map((item) => (item.id === capture.id ? { ...item, note } : item)))
					}
				/>
			))}
		</>
	);
}

function DraftThumbnail({
	capture,
	removeLabel,
	onRemove,
	onSave,
}: {
	capture: BrowserCapture;
	removeLabel: string;
	onRemove: () => void;
	onSave: (note: string) => void;
}) {
	const [editing, setEditing] = useState(false);
	const thumbnailRef = useRef<HTMLButtonElement>(null);
	return (
		<>
			<AttachmentThumbnail
				name={capture.title || capture.url}
				src={toVettaFileUrl(capture.imagePath)}
				badge={<span className="icon-[mdi--web] size-3" />}
				removeLabel={removeLabel}
				openRef={thumbnailRef}
				onRemove={onRemove}
				onOpen={() => setEditing(true)}
			/>
			{editing && (
				<BrowserCaptureDialog
					editing
					capture={capture}
					// 浮层不是由 Dialog.Trigger 打开的,Radix 不知道该把焦点还给谁。
					onCloseAutoFocus={(event) => {
						event.preventDefault();
						thumbnailRef.current?.focus();
					}}
					onCancel={() => setEditing(false)}
					onSave={(note) => {
						onSave(note);
						setEditing(false);
					}}
				/>
			)}
		</>
	);
}