fix(webui): use consistent chat names for scheduled tasks
This commit is contained in:
1 parent
61ab136e58
commit
9a5542bc15
17 files changed
+138
-27
No files matched your search
@@ -12,8 +12,9 @@ import { readLocalPreferences } from "@/lib/local-preferences";
|
||||
import type { AutomationChat, AutomationChatsPayload, AutomationChatUpdate, NanobotFeatureInfo, SessionAutomationJob } from "@/lib/types";
|
||||
|
||||
export type ChangeAutomationChat = (job: SessionAutomationJob, values: AutomationChatUpdate) => Promise<void>;
|
||||
export type AutomationChatNames = ReadonlyMap<string, { title: string; handle: string }>;
|
||||
|
||||
function ChatIdentity({ chat }: { chat: AutomationChat }) {
|
||||
function ChatIdentity({ chat, title }: { chat: AutomationChat; title: string }) {
|
||||
const { t } = useTranslation();
|
||||
const owner = channelUiOwner(chat.channel);
|
||||
const presentation = channelUiPresentation(chat.channel);
|
||||
@@ -29,13 +30,14 @@ function ChatIdentity({ chat }: { chat: AutomationChat }) {
|
||||
: <span className="absolute left-0 top-0 origin-top-left scale-75"><ChannelLogo feature={feature} showBrandLogos={readLocalPreferences().brandLogos} /></span>}
|
||||
</span>
|
||||
<span className="flex min-w-0 flex-wrap items-baseline gap-x-2 text-[13px] leading-5">
|
||||
<span className="min-w-0 break-words">{chat.title}</span><span className="text-[12px] text-muted-foreground">{platform}</span>
|
||||
<span className="min-w-0 break-words">{title}</span><span className="text-[12px] text-muted-foreground">{platform}</span>
|
||||
</span>
|
||||
</span>;
|
||||
}
|
||||
|
||||
export function AutomationChatBinding({ job, token, onSave, children }: {
|
||||
export function AutomationChatBinding({ job, token, chatNames, onSave, children }: {
|
||||
job: SessionAutomationJob; token: string; onSave: ChangeAutomationChat;
|
||||
chatNames?: AutomationChatNames;
|
||||
children: (picker: ReactNode) => ReactNode;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
@@ -72,6 +74,18 @@ export function AutomationChatBinding({ job, token, onSave, children }: {
|
||||
const back = () => { returning.current = true; setDraft(null); };
|
||||
const current = data?.current;
|
||||
const fallback: AutomationChat = { id: "current", title: job.origin?.title || tx("current"), channel: job.origin?.channel || "websocket" };
|
||||
const baseTitle = (chat: AutomationChat) => chatNames?.get(chat.id)?.title ?? chat.title;
|
||||
const counts = new Map<string, number>();
|
||||
for (const chat of data?.chats ?? []) {
|
||||
const title = baseTitle(chat);
|
||||
counts.set(title, (counts.get(title) ?? 0) + 1);
|
||||
}
|
||||
const chatTitle = (chat: AutomationChat) => {
|
||||
const title = baseTitle(chat);
|
||||
const name = chatNames?.get(chat.id);
|
||||
return name && (counts.get(title) ?? 0) > 1 ? `${title} · @${name.handle}` : title;
|
||||
};
|
||||
const identity = (chat: AutomationChat) => <ChatIdentity chat={chat} title={chatTitle(chat)} />;
|
||||
const targetUnavailable = Boolean(draft && data && !data.chats.some(chat => chat.id === draft.target.id && !chat.unavailable));
|
||||
const picker = (review: boolean) => <Select value={review ? draft?.target.id : current?.id ?? "current"}
|
||||
disabled={pickerDisabled} onValueChange={id => {
|
||||
@@ -85,13 +99,13 @@ export function AutomationChatBinding({ job, token, onSave, children }: {
|
||||
}}>
|
||||
<SelectTrigger ref={review ? undefined : trigger} aria-label={tx(review ? "new" : "label")}
|
||||
className="h-auto min-h-11 w-full py-2 [&>span:first-child]:min-w-0 [&>span:first-child]:flex-1 [&>span:first-child]:text-start">
|
||||
<SelectValue><ChatIdentity chat={(review ? draft?.target : current) ?? fallback} /></SelectValue>
|
||||
<SelectValue>{identity((review ? draft?.target : current) ?? fallback)}</SelectValue>
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{!current ? <SelectItem value="current" disabled><ChatIdentity chat={fallback} /></SelectItem> : null}
|
||||
{data?.chats.map(chat => <SelectItem key={chat.id} value={chat.id} textValue={`${chat.title} ${chat.channel}`}
|
||||
{!current ? <SelectItem value="current" disabled>{identity(fallback)}</SelectItem> : null}
|
||||
{data?.chats.map(chat => <SelectItem key={chat.id} value={chat.id} textValue={`${chatTitle(chat)} ${chat.channel}`}
|
||||
disabled={chat.unavailable || (review && chat.id === current?.id)}
|
||||
className="h-auto min-h-11 py-2 [&>span:first-child]:min-w-0 [&>span:first-child]:max-w-[calc(100vw-5rem)] [&>span:first-child]:w-full"><ChatIdentity chat={chat} /></SelectItem>)}
|
||||
className="h-auto min-h-11 py-2 [&>span:first-child]:min-w-0 [&>span:first-child]:max-w-[calc(100vw-5rem)] [&>span:first-child]:w-full">{identity(chat)}</SelectItem>)}
|
||||
</SelectContent>
|
||||
</Select>;
|
||||
const save = async () => {
|
||||
@@ -113,14 +127,14 @@ export function AutomationChatBinding({ job, token, onSave, children }: {
|
||||
<p className="text-[13px] font-medium">{tx("label")}</p>
|
||||
{picker(false)}
|
||||
<p role={saved ? "status" : undefined} className="text-[12px] leading-5 text-muted-foreground">
|
||||
{job.state.pending ? tx("busy") : saved ? tx("saved", { chat: saved.target.title }) : tx("hint")}
|
||||
{job.state.pending ? tx("busy") : saved ? tx("saved", { chat: chatTitle(saved.target) }) : tx("hint")}
|
||||
</p>
|
||||
{saved && current?.id === saved.target.id && data?.chats.some(chat => chat.id === saved.previous.id && !chat.unavailable) ? (
|
||||
<Button variant="link" size="sm" className="h-11 justify-start whitespace-normal p-0 text-start text-[12px] sm:h-auto" disabled={pickerDisabled}
|
||||
onClick={() => {
|
||||
setError("");
|
||||
setDraft({ target: saved.previous, previous: current, revision: data.revision, message: job.payload.message });
|
||||
}}>{tx("changeBack", { chat: saved.previous.title })}</Button>
|
||||
}}>{tx("changeBack", { chat: chatTitle(saved.previous) })}</Button>
|
||||
) : null}
|
||||
{loadError ? <Button variant="link" size="sm" onClick={() => setReload(value => value + 1)}>{tx("retry")}</Button> : null}
|
||||
{data && data.chats.length <= (current ? 1 : 0) ? <p className="text-[12px] leading-5 text-muted-foreground">{tx("available")}</p> : null}
|
||||
@@ -133,7 +147,7 @@ export function AutomationChatBinding({ job, token, onSave, children }: {
|
||||
<div className="space-y-2">
|
||||
<div role="group" aria-label={tx("previous")} className="flex min-w-0 items-start gap-3 pb-1 text-muted-foreground">
|
||||
<span className="shrink-0 text-[12px] leading-6">{tx("previous")}</span>
|
||||
<ChatIdentity chat={draft.previous} />
|
||||
{identity(draft.previous)}
|
||||
</div>
|
||||
<p className="text-[13px] font-medium">{tx("new")}</p>
|
||||
{picker(true)}
|
||||
|
||||
@@ -15,7 +15,7 @@ import { useTranslation } from "react-i18next";
|
||||
import { channelTranslator } from "@/channel-plugins/i18n";
|
||||
import { channelUiOwner, channelUiPresentation } from "@/channel-plugins/registry";
|
||||
import { AutomationCalendar } from "@/components/settings/system/AutomationCalendar";
|
||||
import { AutomationChatBinding, type ChangeAutomationChat } from "@/components/settings/system/AutomationChatBinding";
|
||||
import { AutomationChatBinding, type AutomationChatNames, type ChangeAutomationChat } from "@/components/settings/system/AutomationChatBinding";
|
||||
import { AutomationRunAtPicker, parseAutomationRunAt } from "@/components/settings/system/AutomationRunAtPicker";
|
||||
import {
|
||||
modelPresetOptionsFromSettings,
|
||||
@@ -46,6 +46,7 @@ import type { SendAttachment, SendOptions } from "@/hooks/useNanobotStream";
|
||||
import type {
|
||||
AutomationsPayload,
|
||||
AutomationUpdatePayload,
|
||||
ChatSummary,
|
||||
SessionAutomationJob,
|
||||
SettingsPayload,
|
||||
} from "@/lib/types";
|
||||
@@ -55,11 +56,13 @@ export type AutomationFilter = "all" | "active" | "paused" | "failed" | "system"
|
||||
export type AutomationAction = "enable" | "disable" | "delete" | "run";
|
||||
|
||||
const EMPTY_TITLE_OVERRIDES: Record<string, string> = {};
|
||||
const EMPTY_SESSIONS: ChatSummary[] = [];
|
||||
|
||||
export function AutomationsSettings({
|
||||
token = "",
|
||||
payload, loading, filter, actionKey, error,
|
||||
titleOverrides = EMPTY_TITLE_OVERRIDES,
|
||||
sessions = EMPTY_SESSIONS,
|
||||
settingsSnapshot = null,
|
||||
onFilterChange, onAction,
|
||||
onRequestEdit, onRequestDelete, onStartChat, onManageModels,
|
||||
@@ -69,6 +72,7 @@ export function AutomationsSettings({
|
||||
token?: string;
|
||||
payload: AutomationsPayload | null;
|
||||
titleOverrides?: Record<string, string>;
|
||||
sessions?: ChatSummary[];
|
||||
settingsSnapshot?: SettingsPayload | null;
|
||||
loading: boolean;
|
||||
filter: AutomationFilter;
|
||||
@@ -92,6 +96,13 @@ export function AutomationsSettings({
|
||||
const { t, i18n } = useTranslation();
|
||||
const tx = (key: string, fallback: string, values?: Record<string, unknown>) =>
|
||||
t(key, { defaultValue: fallback, ...(values ?? {}) });
|
||||
// Join display names by the server-owned handle ID, never by a mutable title.
|
||||
const chatNames: AutomationChatNames = useMemo(() => new Map(sessions.flatMap(session => (
|
||||
session.handle ? [[session.handle.id, {
|
||||
title: displayTitle(session, titleOverrides, t("chat.newChat")),
|
||||
handle: session.handle.name,
|
||||
}] as const] : []
|
||||
))), [sessions, titleOverrides, t]);
|
||||
const jobs = useMemo(() => (payload?.jobs ?? []).map((job) => {
|
||||
const origin = job.origin;
|
||||
if (origin?.channel !== "websocket") return job;
|
||||
@@ -367,6 +378,7 @@ export function AutomationsSettings({
|
||||
|
||||
<AutomationDetailDialog
|
||||
token={token}
|
||||
chatNames={chatNames}
|
||||
onChangeChat={onChangeChat}
|
||||
job={selectedJob}
|
||||
open={detailOpen}
|
||||
@@ -395,6 +407,7 @@ export function AutomationsSettings({
|
||||
|
||||
export function AutomationDetailDialog({
|
||||
token = "",
|
||||
chatNames,
|
||||
onChangeChat,
|
||||
job,
|
||||
open,
|
||||
@@ -408,6 +421,7 @@ export function AutomationDetailDialog({
|
||||
onCloseAutoFocus,
|
||||
}: {
|
||||
token?: string;
|
||||
chatNames?: AutomationChatNames;
|
||||
onChangeChat?: ChangeAutomationChat;
|
||||
job: SessionAutomationJob | null;
|
||||
open: boolean;
|
||||
@@ -438,7 +452,7 @@ export function AutomationDetailDialog({
|
||||
onCloseAutoFocus={onCloseAutoFocus}
|
||||
>
|
||||
{job.chat_binding_revision && onChangeChat ? (
|
||||
<AutomationChatBinding key={job.id} job={job} token={token} onSave={onChangeChat}>{panel}</AutomationChatBinding>
|
||||
<AutomationChatBinding key={job.id} job={job} token={token} chatNames={chatNames} onSave={onChangeChat}>{panel}</AutomationChatBinding>
|
||||
) : panel()}
|
||||
</DialogContent>
|
||||
) : null}
|
||||
|
||||
Reference in new issue
Block a user