|
| 1 | +import { runIf } from "@resolid/utils"; |
| 2 | +import { type PropsWithChildren, type ReactElement, useReducer } from "react"; |
| 3 | +import { PortalLite } from "../portal/portal-lite"; |
| 4 | +import { |
| 5 | + type ToastConfig, |
| 6 | + ToastContext, |
| 7 | + type ToastContextValue, |
| 8 | + type ToastId, |
| 9 | + type ToastPlacement, |
| 10 | +} from "./toast-context"; |
| 11 | +import { ToastRegion, type ToastRegionBaseProps } from "./toast-region"; |
| 12 | + |
| 13 | +export type ToastProviderProps = { |
| 14 | + /** |
| 15 | + * 自动关闭延时, 设置为 `null` 时不自动关闭 |
| 16 | + * @default 5000 |
| 17 | + */ |
| 18 | + duration?: number | null; |
| 19 | +} & Partial<ToastRegionBaseProps>; |
| 20 | + |
| 21 | +type ToastState = { |
| 22 | + [K in ToastPlacement]: ToastConfig[]; |
| 23 | +}; |
| 24 | + |
| 25 | +type ToastAction = |
| 26 | + | { type: "ADD"; payload: { toast: ToastConfig } } |
| 27 | + | { type: "UPDATE"; payload: { id: ToastId; component: ReactElement; duration?: number | null } } |
| 28 | + | { type: "DISMISS"; payload: { id: ToastId } } |
| 29 | + | { type: "CLEAR"; payload: { placements?: ToastPlacement[] } } |
| 30 | + | { type: "REMOVE"; payload: { id: ToastId } }; |
| 31 | + |
| 32 | +const reducer = (state: ToastState, action: ToastAction) => { |
| 33 | + switch (action.type) { |
| 34 | + case "ADD": { |
| 35 | + const { toast } = action.payload; |
| 36 | + const placement = toast.placement; |
| 37 | + |
| 38 | + return { |
| 39 | + ...state, |
| 40 | + [placement]: placement.slice(0, 3) == "top" ? [toast, ...state[placement]] : [...state[placement], toast], |
| 41 | + }; |
| 42 | + } |
| 43 | + case "UPDATE": { |
| 44 | + const { id, component, duration } = action.payload; |
| 45 | + |
| 46 | + const [placement, index] = getPlacementAndIndexById(state, id); |
| 47 | + |
| 48 | + if (placement == undefined || index == undefined) { |
| 49 | + return state; |
| 50 | + } |
| 51 | + |
| 52 | + return { |
| 53 | + ...state, |
| 54 | + [placement]: [ |
| 55 | + ...state[placement].slice(0, index), |
| 56 | + { |
| 57 | + id, |
| 58 | + duration: duration !== undefined ? duration : state[placement][index].duration, |
| 59 | + component: () => component, |
| 60 | + update: true, |
| 61 | + dismiss: false, |
| 62 | + }, |
| 63 | + ...state[placement].slice(index + 1), |
| 64 | + ], |
| 65 | + }; |
| 66 | + } |
| 67 | + case "DISMISS": { |
| 68 | + const { id } = action.payload; |
| 69 | + |
| 70 | + const [placement, index] = getPlacementAndIndexById(state, id); |
| 71 | + |
| 72 | + if (placement == undefined || index == undefined) { |
| 73 | + return state; |
| 74 | + } |
| 75 | + |
| 76 | + return { |
| 77 | + ...state, |
| 78 | + [placement]: [ |
| 79 | + ...state[placement].slice(0, index), |
| 80 | + { ...state[placement][index], dismiss: true }, |
| 81 | + ...state[placement].slice(index + 1), |
| 82 | + ], |
| 83 | + }; |
| 84 | + } |
| 85 | + case "CLEAR": { |
| 86 | + const placements = |
| 87 | + !action.payload.placements || action.payload.placements.length == 0 |
| 88 | + ? (["bottom", "bottom-start", "bottom-end", "top", "top-start", "top-end"] as ToastPlacement[]) |
| 89 | + : action.payload.placements; |
| 90 | + |
| 91 | + const result = { ...state }; |
| 92 | + |
| 93 | + for (const placement of placements) { |
| 94 | + result[placement] = state[placement].map((toast) => ({ ...toast, dismiss: true })); |
| 95 | + } |
| 96 | + |
| 97 | + return result; |
| 98 | + } |
| 99 | + case "REMOVE": { |
| 100 | + const { id } = action.payload; |
| 101 | + |
| 102 | + const [placement, index] = getPlacementAndIndexById(state, id); |
| 103 | + |
| 104 | + if (placement == undefined || index == undefined) { |
| 105 | + return state; |
| 106 | + } |
| 107 | + |
| 108 | + return { |
| 109 | + ...state, |
| 110 | + [placement]: [...state[placement].slice(0, index), ...state[placement].slice(index + 1)], |
| 111 | + }; |
| 112 | + } |
| 113 | + default: |
| 114 | + return state; |
| 115 | + } |
| 116 | +}; |
| 117 | + |
| 118 | +export const ToastProvider = ({ |
| 119 | + placement = "bottom-end", |
| 120 | + duration = 5000, |
| 121 | + spacing = "0.75rem", |
| 122 | + visibleToasts = 5, |
| 123 | + children, |
| 124 | +}: PropsWithChildren<ToastProviderProps>) => { |
| 125 | + const [state, dispatch] = useReducer(reducer, { |
| 126 | + "top-start": [], |
| 127 | + top: [], |
| 128 | + "top-end": [], |
| 129 | + "bottom-start": [], |
| 130 | + bottom: [], |
| 131 | + "bottom-end": [], |
| 132 | + }); |
| 133 | + |
| 134 | + const context: ToastContextValue = { |
| 135 | + show: (component, options) => { |
| 136 | + const toastId = options?.id ?? `t-${Math.random().toString(36).slice(2, 9)}`; |
| 137 | + const toastDuration = options?.duration === undefined ? duration : options.duration; |
| 138 | + const toastPlacement = options?.placement ?? placement; |
| 139 | + |
| 140 | + dispatch({ |
| 141 | + type: "ADD", |
| 142 | + payload: { |
| 143 | + toast: { |
| 144 | + id: toastId, |
| 145 | + placement: toastPlacement, |
| 146 | + duration: toastDuration, |
| 147 | + component: () => component, |
| 148 | + update: false, |
| 149 | + dismiss: false, |
| 150 | + }, |
| 151 | + }, |
| 152 | + }); |
| 153 | + |
| 154 | + return toastId; |
| 155 | + }, |
| 156 | + update: (id, component) => { |
| 157 | + dispatch({ type: "UPDATE", payload: { id, component } }); |
| 158 | + }, |
| 159 | + dismiss: (id) => { |
| 160 | + dispatch({ type: "DISMISS", payload: { id } }); |
| 161 | + }, |
| 162 | + showPromise: (promise, component, options) => { |
| 163 | + const originalDuration = options?.duration === undefined ? duration : options.duration; |
| 164 | + |
| 165 | + const toastId = context.show(component({ state: "pending" }), { ...options, duration: null }); |
| 166 | + |
| 167 | + runIf(promise) |
| 168 | + .then((data) => { |
| 169 | + dispatch({ |
| 170 | + type: "UPDATE", |
| 171 | + payload: { id: toastId, component: component({ state: "success", data }), duration: originalDuration }, |
| 172 | + }); |
| 173 | + }) |
| 174 | + .catch((error) => { |
| 175 | + dispatch({ |
| 176 | + type: "UPDATE", |
| 177 | + payload: { id: toastId, component: component({ state: "failure", error }), duration: originalDuration }, |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + return toastId; |
| 182 | + }, |
| 183 | + clearAll: (...args) => { |
| 184 | + dispatch({ type: "CLEAR", payload: { placements: args } }); |
| 185 | + }, |
| 186 | + }; |
| 187 | + |
| 188 | + const remove = (id: ToastId) => { |
| 189 | + dispatch({ type: "REMOVE", payload: { id } }); |
| 190 | + }; |
| 191 | + |
| 192 | + return ( |
| 193 | + <ToastContext value={context}> |
| 194 | + {children} |
| 195 | + <PortalLite> |
| 196 | + {Object.entries(state).map(([placement, toasts]) => { |
| 197 | + if (toasts.length == 0) { |
| 198 | + return null; |
| 199 | + } |
| 200 | + |
| 201 | + return ( |
| 202 | + <ToastRegion |
| 203 | + key={placement} |
| 204 | + placement={placement as ToastPlacement} |
| 205 | + spacing={spacing} |
| 206 | + visibleToasts={visibleToasts} |
| 207 | + toasts={toasts} |
| 208 | + remove={remove} |
| 209 | + /> |
| 210 | + ); |
| 211 | + })} |
| 212 | + </PortalLite> |
| 213 | + </ToastContext> |
| 214 | + ); |
| 215 | +}; |
| 216 | + |
| 217 | +const getPlacementAndIndexById = (state: ToastState, id: ToastId): [ToastPlacement | undefined, number | undefined] => { |
| 218 | + for (const [placement, toasts] of Object.entries(state)) { |
| 219 | + const index = toasts.findIndex((toast) => toast.id == id); |
| 220 | + |
| 221 | + if (index > -1) { |
| 222 | + return [placement as ToastPlacement, index]; |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + return [undefined, undefined]; |
| 227 | +}; |
0 commit comments