Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

linkedPrompt.ts

56 lines1.8 KB

1import type { LinkedPrompt } from "@t3tools/shared/promptLinks";
2import { msg } from "gt-react";
3​
4import { type ComposerThreadTarget, useComposerDraftStore } from "~/composerDraftStore";
5import { m } from "~/i18n/messages";
6​
7import { toastManager } from "../ui/toast";
8​
9export function warnAboutHiddenCharacters(prompt: LinkedPrompt): void {
10 if (prompt.hiddenCharacters === 0) return;
11 toastManager.add({
12 type: "warning",
13 title: m(msg("This prompt had hidden characters, it may be malicious")),
14 description: m(
15 msg(
16 "{count, plural, one {T3 Code removed {count} hidden character. Read the prompt before you send it.} other {T3 Code removed {count} hidden characters. Read the prompt before you send it.}}",
17 { count: prompt.hiddenCharacters },
18 ),
19 ),
20 timeout: 0,
21 });
23​
24export function placeLinkedPrompt(
25 target: ComposerThreadTarget,
26 prompt: LinkedPrompt,
27 notes: ReadonlyArray<string> = [],
28): void {
29 warnAboutHiddenCharacters(prompt);
30 const place = () => {
31 useComposerDraftStore.getState().setPrompt(target, prompt.text);
32 toastManager.add({
33 type: "info",
34 title: m(msg("Prompt added from a link")),
35 description: [m(msg("Read it before you send.")), ...notes].join(" "),
36 });
37 };
38 const existing = useComposerDraftStore.getState().getComposerDraft(target)?.prompt ?? "";
39 if (existing.trim().length === 0 || existing === prompt.text) {
40 place();
41 return;
42 }
43 const id = toastManager.add({
44 type: "info",
45 title: m(msg("Linked prompt ready")),
46 description: m(msg("This thread already has a draft. Replace it with the linked prompt?")),
47 timeout: 0,
48 actionProps: {
49 children: m(msg("Replace draft")),
50 onClick: () => {
51 toastManager.close(id);
52 place();
53 },
54 },
55 });