MCPcopy
hub / github.com/Nutlope/self.so / reducer

Function reducer

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

Source from the content-addressed store, hash-verified

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

Callers 1

dispatchFunction · 0.70

Calls 1

addToRemoveQueueFunction · 0.70

Tested by

no test coverage detected