]> Untitled Git - lemmy-ui.git/blob - src/shared/toast.ts
b8ab0623d2a6f138362fb19b28534c20d7ffcc7a
[lemmy-ui.git] / src / shared / toast.ts
1 import { isBrowser } from "@utils/browser";
2 import { ThemeColor } from "@utils/types";
3 import Toastify from "toastify-js";
4 import { i18n } from "./i18next";
5
6 export function toast(text: string, background: ThemeColor = "success") {
7   if (isBrowser()) {
8     const backgroundColor = `var(--bs-${background})`;
9     Toastify({
10       text: text,
11       backgroundColor: backgroundColor,
12       gravity: "bottom",
13       position: "left",
14       duration: 5000,
15     }).showToast();
16   }
17 }
18
19 export function pictrsDeleteToast(filename: string, deleteUrl: string) {
20   if (isBrowser()) {
21     const clickToDeleteText = i18n.t("click_to_delete_picture", { filename });
22     const deletePictureText = i18n.t("picture_deleted", {
23       filename,
24     });
25     const failedDeletePictureText = i18n.t("failed_to_delete_picture", {
26       filename,
27     });
28
29     const backgroundColor = `var(--bs-light)`;
30
31     const toast = Toastify({
32       text: clickToDeleteText,
33       backgroundColor: backgroundColor,
34       gravity: "top",
35       position: "right",
36       duration: 10000,
37       onClick: () => {
38         if (toast) {
39           fetch(deleteUrl).then(res => {
40             toast.hideToast();
41             if (res.ok === true) {
42               alert(deletePictureText);
43             } else {
44               alert(failedDeletePictureText);
45             }
46           });
47         }
48       },
49       close: true,
50     });
51
52     toast.showToast();
53   }
54 }