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

linkedPrompt.ts

59 lines1.9 KB

1import type { LinkedPrompt } from "@t3tools/shared/promptLinks";
2import { msg, plural, part } 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`${plural(
16 { count: prompt.hiddenCharacters },
17 {
18 one: part`T3 Code removed ${{ count: prompt.hiddenCharacters }} hidden character. Read the prompt before you send it.`,
19 other: part`T3 Code removed ${{ count: prompt.hiddenCharacters }} hidden characters. Read the prompt before you send it.`,
20 },
21 )}`,
22 ),
23 timeout: 0,
24 });
26​
27export function placeLinkedPrompt(
28 target: ComposerThreadTarget,
29 prompt: LinkedPrompt,
30 notes: ReadonlyArray<string> = [],
31): void {
32 warnAboutHiddenCharacters(prompt);
33 const place = () => {
34 useComposerDraftStore.getState().setPrompt(target, prompt.text);
35 toastManager.add({
36 type: "info",
37 title: m(msg("Prompt added from a link")),
38 description: [m(msg("Read it before you send.")), ...notes].join(" "),
39 });
40 };
41 const existing = useComposerDraftStore.getState().getComposerDraft(target)?.prompt ?? "";
42 if (existing.trim().length === 0 || existing === prompt.text) {
43 place();
44 return;
45 }
46 const id = toastManager.add({
47 type: "info",
48 title: m(msg("Linked prompt ready")),
49 description: m(msg("This thread already has a draft. Replace it with the linked prompt?")),
50 timeout: 0,
51 actionProps: {
52 children: m(msg("Replace draft")),
53 onClick: () => {
54 toastManager.close(id);
55 place();
56 },
57 },
58 });