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

threadTitle.ts

32 lines918 Bytes

1import { truncate } from "@t3tools/shared/String";
2​
3export interface ThreadTitleSeedInput {
4 readonly text: string;
5 readonly attachments: ReadonlyArray<{ readonly name: string }>;
6 readonly fallbackLabels?: ReadonlyArray<string | null | undefined>;
7}
8​
9function normalizeTitleSeed(value: string): string {
10 return value.trim().replace(/\s+/gu, " ");
12​
13export function deriveThreadTitleSeed(input: ThreadTitleSeedInput): string {
14 const text = normalizeTitleSeed(input.text);
15 if (text.length > 0) {
16 return truncate(text);
17 }
18​
19 const attachmentName = normalizeTitleSeed(input.attachments[0]?.name ?? "");
20 if (attachmentName.length > 0) {
21 return truncate(`Image: ${attachmentName}`);
22 }
23​
24 for (const label of input.fallbackLabels ?? []) {
25 const normalized = normalizeTitleSeed(label ?? "");
26 if (normalized.length > 0) {
27 return truncate(normalized);
28 }
29 }
30​
31 return "New thread";