Files
kandev/apps/web/components/task/session-context-summary.ts
T
João Salavisa 5257dfbaab fix(web): close the i18n coverage gaps in the non-JSX scanner and locale catalogs (#2727)
Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-16 23:03:29 +00:00

58 lines
1.4 KiB
TypeScript

import type { RefObject } from "react";
import type { SummarizeSessionResult } from "@/hooks/use-summarize-session";
import { t } from "@/lib/i18n";
type PromptValueTarget = { value: string } | { setValue: (value: string) => void };
type PromptValueRef = RefObject<PromptValueTarget | null>;
export type SummaryToastFn = (opts: {
title: string;
description?: string;
variant?: "error" | "default";
}) => void;
export function sanitizePromptText(value: string): string {
return value.replace(/\r/g, "").replace(/[<>]/g, " ");
}
export function applySummarizeSessionResult({
result,
promptRef,
setContextValue,
setHasPrompt,
toast,
}: {
result: SummarizeSessionResult;
promptRef: PromptValueRef;
setContextValue: (v: string) => void;
setHasPrompt: (v: boolean) => void;
toast: SummaryToastFn;
}) {
const setPromptValue = (value: string): boolean => {
const target = promptRef.current;
if (!target) return false;
if ("setValue" in target) {
target.setValue(value);
} else {
target.value = value;
}
return true;
};
if (result.summary === null) {
setContextValue("blank");
setPromptValue("");
setHasPrompt(false);
toast({
title: t("task:summarizeFailed"),
description: result.error ?? t("task:summarizeCouldNotGenerate"),
variant: "error",
});
return;
}
if (setPromptValue(sanitizePromptText(result.summary))) {
setHasPrompt(true);
}
}