MCPcopy Create free account
hub / github.com/sourcebot-dev/sourcebot / reducer

Function reducer

packages/web/src/components/hooks/use-toast.ts:77–130  ·  view source on GitHub ↗
(state: State, action: Action)

Source from the content-addressed store, hash-verified

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

Callers 1

dispatchFunction · 0.85

Calls 1

addToRemoveQueueFunction · 0.85

Tested by

no test coverage detected