MCPcopy
hub / github.com/shadcn-ui/taxonomy / reducer

Function reducer

components/ui/use-toast.ts:72–125  ·  view source on GitHub ↗
(state: State, action: Action)

Source from the content-addressed store, hash-verified

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

Callers 1

dispatchFunction · 0.85

Calls 1

addToRemoveQueueFunction · 0.85

Tested by

no test coverage detected