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

toast.logic.ts

118 lines3.0 KB

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