Refactor code structure for improved readability and maintainability

This commit is contained in:
Poyraz Avsever
2026-05-08 11:18:55 +03:00
parent e98972597d
commit 9adc34418e
21 changed files with 1194 additions and 393 deletions
+20 -23
View File
@@ -10,6 +10,10 @@ import type {
const TOAST_LIMIT = 1
const TOAST_REMOVE_DELAY = 1000000
const ADD_TOAST = "ADD_TOAST"
const UPDATE_TOAST = "UPDATE_TOAST"
const DISMISS_TOAST = "DISMISS_TOAST"
const REMOVE_TOAST = "REMOVE_TOAST"
type ToasterToast = ToastProps & {
id: string
@@ -18,13 +22,6 @@ type ToasterToast = ToastProps & {
action?: ToastActionElement
}
const actionTypes = {
ADD_TOAST: "ADD_TOAST",
UPDATE_TOAST: "UPDATE_TOAST",
DISMISS_TOAST: "DISMISS_TOAST",
REMOVE_TOAST: "REMOVE_TOAST",
} as const
let count = 0
function genId() {
@@ -32,23 +29,21 @@ function genId() {
return count.toString()
}
type ActionType = typeof actionTypes
type Action =
| {
type: ActionType["ADD_TOAST"]
type: typeof ADD_TOAST
toast: ToasterToast
}
| {
type: ActionType["UPDATE_TOAST"]
type: typeof UPDATE_TOAST
toast: Partial<ToasterToast>
}
| {
type: ActionType["DISMISS_TOAST"]
type: typeof DISMISS_TOAST
toastId?: ToasterToast["id"]
}
| {
type: ActionType["REMOVE_TOAST"]
type: typeof REMOVE_TOAST
toastId?: ToasterToast["id"]
}
@@ -66,7 +61,7 @@ const addToRemoveQueue = (toastId: string) => {
const timeout = setTimeout(() => {
toastTimeouts.delete(toastId)
dispatch({
type: "REMOVE_TOAST",
type: REMOVE_TOAST,
toastId: toastId,
})
}, TOAST_REMOVE_DELAY)
@@ -76,13 +71,13 @@ const addToRemoveQueue = (toastId: string) => {
export const reducer = (state: State, action: Action): State => {
switch (action.type) {
case "ADD_TOAST":
case ADD_TOAST:
return {
...state,
toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT),
}
case "UPDATE_TOAST":
case UPDATE_TOAST:
return {
...state,
toasts: state.toasts.map((t) =>
@@ -90,7 +85,7 @@ export const reducer = (state: State, action: Action): State => {
),
}
case "DISMISS_TOAST": {
case DISMISS_TOAST: {
const { toastId } = action
// ! Side effects ! - This could be extracted into a dismissToast() action,
@@ -115,7 +110,7 @@ export const reducer = (state: State, action: Action): State => {
),
}
}
case "REMOVE_TOAST":
case REMOVE_TOAST:
if (action.toastId === undefined) {
return {
...state,
@@ -126,6 +121,8 @@ export const reducer = (state: State, action: Action): State => {
...state,
toasts: state.toasts.filter((t) => t.id !== action.toastId),
}
default:
return state
}
}
@@ -147,13 +144,13 @@ function toast({ ...props }: Toast) {
const update = (props: ToasterToast) =>
dispatch({
type: "UPDATE_TOAST",
type: UPDATE_TOAST,
toast: { ...props, id },
})
const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id })
const dismiss = () => dispatch({ type: DISMISS_TOAST, toastId: id })
dispatch({
type: "ADD_TOAST",
type: ADD_TOAST,
toast: {
...props,
id,
@@ -182,12 +179,12 @@ function useToast() {
listeners.splice(index, 1)
}
}
}, [state])
}, [])
return {
...state,
toast,
dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId }),
dismiss: (toastId?: string) => dispatch({ type: DISMISS_TOAST, toastId }),
}
}