@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 115 lines 2.93 KB
Raw
1 import type { ScopedThreadRef, ThreadId } from "@t3tools/contracts";
2
3 /** Base UI updates omit undefined; clear actions with defined actionProps and empty children, treated as
4 * invisible. */
5 export function hasVisibleToastAction(actionProps: unknown): boolean {
6 if (actionProps == null || typeof actionProps !== "object") {
7 return false;
8 }
9 if (!("children" in actionProps)) {
10 return false;
11 }
12 const children = actionProps.children;
13 return children != null && children !== false && children !== "";
14 }
15
16 export function shouldHideCollapsedToastContent(
17 visibleToastIndex: number,
18 visibleToastCount: number,
19 ): boolean {
20 if (visibleToastCount <= 1) return false;
21 return visibleToastIndex > 0;
22 }
23
24 type ToastWithHeight = {
25 height?: number | null | undefined;
26 };
27
28 type ToastWithTransitionStatus = {
29 transitionStatus?: "starting" | "ending" | undefined;
30 };
31
32 type ToastWithLayoutProps = ToastWithHeight & ToastWithTransitionStatus;
33
34 type VisibleToastLayoutItem<TToast extends object> = {
35 toast: TToast;
36 visibleIndex: number;
37 offsetY: number;
38 };
39
40 export function buildVisibleToastLayout<TToast extends object>(
41 visibleToasts: readonly (TToast & ToastWithLayoutProps)[],
42 ): {
43 frontmostHeight: number;
44 items: VisibleToastLayoutItem<TToast & ToastWithLayoutProps>[];
45 } {
46 let fullIndex = 0;
47 let fullOffsetY = 0;
48 let liveIndex = 0;
49 let liveOffsetY = 0;
50
51 const items: VisibleToastLayoutItem<TToast & ToastWithLayoutProps>[] = visibleToasts.map(
52 (toast) => {
53 const height = normalizeToastHeight(toast.height);
54
55 if (toast.transitionStatus === "ending") {
56 const item = {
57 toast,
58 visibleIndex: fullIndex,
59 offsetY: fullOffsetY,
60 };
61 fullOffsetY += height;
62 fullIndex += 1;
63 return item;
64 }
65
66 const item = {
67 toast,
68 visibleIndex: liveIndex,
69 offsetY: liveOffsetY,
70 };
71
72 fullOffsetY += height;
73 fullIndex += 1;
74 liveOffsetY += height;
75 liveIndex += 1;
76 return item;
77 },
78 );
79
80 const frontmostLiveToast = visibleToasts.find((toast) => toast.transitionStatus !== "ending");
81
82 return {
83 frontmostHeight: normalizeToastHeight(frontmostLiveToast?.height),
84 items,
85 };
86 }
87
88 function normalizeToastHeight(height: number | null | undefined): number {
89 return typeof height === "number" && Number.isFinite(height) && height > 0 ? height : 0;
90 }
91
92 export function shouldRenderThreadScopedToast(
93 data:
94 | {
95 threadRef?: ScopedThreadRef | null;
96 threadId?: ThreadId | null;
97 }
98 | undefined,
99 activeThreadRef: ScopedThreadRef | null,
100 ): boolean {
101 if (data?.threadRef) {
102 return (
103 activeThreadRef !== null &&
104 data.threadRef.environmentId === activeThreadRef.environmentId &&
105 data.threadRef.threadId === activeThreadRef.threadId
106 );
107 }
108
109 const toastThreadId = data?.threadId;
110 if (!toastThreadId) {
111 return true;
112 }
113
114 return activeThreadRef?.threadId === toastThreadId;
115 }