File size: 6,631 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { get } from 'svelte/store';
import { clientWritable } from './internal/helpers.js';
let toastsCounter = 0;
function createToastState() {
    const toasts = clientWritable([]);
    const heights = clientWritable([]);
    function addToast(data) {
        toasts.update((prev) => [data, ...prev]);
    }
    function create(data) {
        const { message, ...rest } = data;
        const id = typeof data?.id === 'number' || (data.id && data.id?.length > 0)
            ? data.id
            : toastsCounter++;
        const dismissable = data.dismissable === undefined ? true : data.dismissable;
        const type = data.type === undefined ? 'default' : data.type;
        const $toasts = get(toasts);
        const alreadyExists = $toasts.find((toast) => {
            return toast.id === id;
        });
        if (alreadyExists) {
            toasts.update((prev) => prev.map((toast) => {
                if (toast.id === id) {
                    return {
                        ...toast,
                        ...data,
                        id,
                        title: message,
                        dismissable,
                        type,
                        updated: true
                    };
                }
                return {
                    ...toast,
                    updated: false
                };
            }));
        }
        else {
            addToast({ ...rest, id, title: message, dismissable, type });
        }
        return id;
    }
    function dismiss(id) {
        if (id === undefined) {
            toasts.update((prev) => prev.map((toast) => ({ ...toast, dismiss: true })));
            return;
        }
        toasts.update((prev) => prev.map((toast) => (toast.id === id ? { ...toast, dismiss: true } : toast)));
        return id;
    }
    function remove(id) {
        if (id === undefined) {
            toasts.set([]);
            return;
        }
        toasts.update((prev) => prev.filter((toast) => toast.id !== id));
        return id;
    }
    function message(message, data) {
        return create({ ...data, type: 'default', message });
    }
    function error(message, data) {
        return create({ ...data, type: 'error', message });
    }
    function success(message, data) {
        return create({ ...data, type: 'success', message });
    }
    function info(message, data) {
        return create({ ...data, type: 'info', message });
    }
    function warning(message, data) {
        return create({ ...data, type: 'warning', message });
    }
    function loading(message, data) {
        return create({ ...data, type: 'loading', message });
    }
    function promise(promise, data) {
        if (!data) {
            // Nothing to show
            return;
        }
        let id = undefined;
        if (data.loading !== undefined) {
            id = create({
                ...data,
                promise,
                type: 'loading',
                message: data.loading
            });
        }
        const p = promise instanceof Promise ? promise : promise();
        let shouldDismiss = id !== undefined;
        p.then((response) => {
            // TODO: Clean up TS here, response has incorrect type
            // @ts-expect-error: Incorrect response type
            if (response && typeof response.ok === 'boolean' && !response.ok) {
                shouldDismiss = false;
                const message = typeof data.error === 'function'
                    ? // @ts-expect-error: Incorrect response type
                        data.error(`HTTP error! status: ${response.status}`)
                    : data.error;
                create({ id, type: 'error', message });
            }
            else if (data.success !== undefined) {
                shouldDismiss = false;
                const message = 
                // @ts-expect-error: TODO: Better function checking
                typeof data.success === 'function' ? data.success(response) : data.success;
                create({ id, type: 'success', message });
            }
        })
            .catch((error) => {
            if (data.error !== undefined) {
                shouldDismiss = false;
                const message = 
                // @ts-expect-error: TODO: Better function checking
                typeof data.error === 'function' ? data.error(error) : data.error;
                create({ id, type: 'error', message });
            }
        })
            .finally(() => {
            if (shouldDismiss) {
                // Toast is still in load state (and will be indefinitely — dismiss it)
                dismiss(id);
                id = undefined;
            }
            data.finally?.();
        });
        return id;
    }
    function custom(component, data) {
        const id = data?.id || toastsCounter++;
        create({ component, id, ...data });
        return id;
    }
    function removeHeight(id) {
        heights.update((prev) => prev.filter((height) => height.toastId !== id));
    }
    function setHeight(data) {
        const exists = get(heights).find((el) => el.toastId === data.toastId);
        if (exists === undefined) {
            heights.update((prev) => [data, ...prev]);
            return;
        }
        heights.update((prev) => prev.map((el) => {
            if (el.toastId === data.toastId) {
                return data;
            }
            else {
                return el;
            }
        }));
    }
    function reset() {
        toasts.set([]);
        heights.set([]);
    }
    return {
        // methods
        create,
        addToast,
        dismiss,
        remove,
        message,
        error,
        success,
        info,
        warning,
        loading,
        promise,
        custom,
        removeHeight,
        setHeight,
        reset,
        // stores
        toasts,
        heights
    };
}
export const toastState = createToastState();
// bind this to the toast function
function toastFunction(message, data) {
    return toastState.create({
        message,
        ...data
    });
}
const basicToast = toastFunction;
// We use `Object.assign` to maintain the correct types as we would lose them otherwise
export const toast = Object.assign(basicToast, {
    success: toastState.success,
    info: toastState.info,
    warning: toastState.warning,
    error: toastState.error,
    custom: toastState.custom,
    message: toastState.message,
    promise: toastState.promise,
    dismiss: toastState.dismiss,
    loading: toastState.loading
});
export const useEffect = (subscribe) => ({ subscribe });