MCPcopy Create free account
hub / github.com/code100x/daily-code / reducer

Function reducer

packages/ui/src/shad/ui/use-toast.tsx:71–122  ·  view source on GitHub ↗
(state: State, action: Action)

Source from the content-addressed store, hash-verified

69};
70
71export const reducer = (state: State, action: Action): State => {
72 switch (action.type) {
73 case "ADD_TOAST":
74 return {
75 ...state,
76 toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT),
77 };
78
79 case "UPDATE_TOAST":
80 return {
81 ...state,
82 toasts: state.toasts.map((t) => (t.id === action.toast.id ? { ...t, ...action.toast } : t)),
83 };
84
85 case "DISMISS_TOAST": {
86 const { toastId } = action;
87
88 // ! Side effects ! - This could be extracted into a dismissToast() action,
89 // but I'll keep it here for simplicity
90 if (toastId) {
91 addToRemoveQueue(toastId);
92 } else {
93 state.toasts.forEach((toast) => {
94 addToRemoveQueue(toast.id);
95 });
96 }
97
98 return {
99 ...state,
100 toasts: state.toasts.map((t) =>
101 t.id === toastId || toastId === undefined
102 ? {
103 ...t,
104 open: false,
105 }
106 : t
107 ),
108 };
109 }
110 case "REMOVE_TOAST":
111 if (action.toastId === undefined) {
112 return {
113 ...state,
114 toasts: [],
115 };
116 }
117 return {
118 ...state,
119 toasts: state.toasts.filter((t) => t.id !== action.toastId),
120 };
121 }
122};
123
124const listeners: Array<(state: State) => void> = [];
125

Callers 1

dispatchFunction · 0.85

Calls 1

addToRemoveQueueFunction · 0.85

Tested by

no test coverage detected