The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import type { ThreadTitleRegenerationOutcome } from "@t3tools/contracts"; |
| 3 | import { |
| 4 | type AtomCommandResult, |
| 5 | isAtomCommandInterrupted, |
| 6 | squashAtomCommandFailure, |
| 7 | } from "@t3tools/client-runtime/state/runtime"; |
| 8 | import { msg } from "gt-react"; |
| 9 | |
| 10 | import { m } from "../i18n/messages"; |
| 11 | import { stackedThreadToast } from "./ui/toast"; |
| 12 | |
| 13 | type TitleRegenerationResult = AtomCommandResult<ThreadTitleRegenerationOutcome | null, unknown>; |
| 14 | |
| 15 | function failureMessage(result: TitleRegenerationResult): string | null { |
| 16 | if (result._tag === "Failure") { |
| 17 | if (isAtomCommandInterrupted(result)) return null; |
| 18 | const error = squashAtomCommandFailure(result); |
| 19 | return thrownErrorMessage(error, m) ?? m(msg("An error occurred.")); |
| 20 | } |
| 21 | return result.value?.status === "failed" |
| 22 | ? (result.value.message ?? m(msg("Title generation failed."))) |
| 23 | : null; |
| 24 | } |
| 25 | |
| 26 | function isKept(result: TitleRegenerationResult): boolean { |
| 27 | return result._tag === "Success" && result.value?.status === "unchanged"; |
| 28 | } |
| 29 | |
| 30 | export function titleRegenerationToast(results: ReadonlyArray<TitleRegenerationResult>) { |
| 31 | const failures = results.flatMap((result) => { |
| 32 | const message = failureMessage(result); |
| 33 | return message === null ? [] : [message]; |
| 34 | }); |
| 35 | if (failures.length > 0) { |
| 36 | return stackedThreadToast({ |
| 37 | type: "error", |
| 38 | title: |
| 39 | results.length === 1 |
| 40 | ? m(msg("Failed to regenerate thread title")) |
| 41 | : m( |
| 42 | msg( |
| 43 | "{total, plural, one {Failed to regenerate {failed} of {total} thread titles} other {Failed to regenerate {failed} of {total} thread titles}}", |
| 44 | { |
| 45 | failed: failures.length, |
| 46 | total: results.length, |
| 47 | }, |
| 48 | ), |
| 49 | ), |
| 50 | description: failures[0], |
| 51 | }); |
| 52 | } |
| 53 | const kept = results.filter(isKept).length; |
| 54 | if (kept === 0) return null; |
| 55 | if (results.length === 1) { |
| 56 | return stackedThreadToast({ |
| 57 | type: "info", |
| 58 | title: m(msg("Title kept")), |
| 59 | description: m(msg("The current title already fits this thread.")), |
| 60 | }); |
| 61 | } |
| 62 | return stackedThreadToast({ |
| 63 | type: "info", |
| 64 | title: |
| 65 | kept === results.length |
| 66 | ? m(msg("Titles kept")) |
| 67 | : m( |
| 68 | msg( |
| 69 | "{total, plural, one {Kept {kept} of {total} titles} other {Kept {kept} of {total} titles}}", |
| 70 | { kept, total: results.length }, |
| 71 | ), |
| 72 | ), |
| 73 | description: |
| 74 | kept === results.length |
| 75 | ? m(msg("The current titles already fit these threads.")) |
| 76 | : m(msg("Those titles already fit their threads.")), |
| 77 | }); |
| 78 | } |