File size: 3,141 Bytes
4995d62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Toast - Notification utility wrapping react-hot-toast.
 *
 * Provides typed helper functions for consistent toast notifications
 * throughout the application. The Toaster component is already rendered
 * in main.jsx; this module only provides the trigger functions.
 *
 * Usage:
 *   import { showSuccess, showError, showInfo } from "@components/common/Toast";
 *   showSuccess("Record saved successfully.");
 *   showError("Failed to fetch data.");
 *   showInfo("New version available.");
 */

import toast from "react-hot-toast";

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/**
 * Show a success toast notification.
 *
 * @param {string} message  - Notification text.
 * @param {object} [options] - react-hot-toast options override.
 * @returns {string} Toast ID.
 */
export function showSuccess(message, options = {}) {
  return toast.success(message, {
    duration: 4000,
    ...options,
  });
}

/**
 * Show an error toast notification.
 *
 * @param {string} message  - Notification text.
 * @param {object} [options] - react-hot-toast options override.
 * @returns {string} Toast ID.
 */
export function showError(message, options = {}) {
  return toast.error(message, {
    duration: 5000,
    ...options,
  });
}

/**
 * Show an informational toast notification.
 *
 * @param {string} message  - Notification text.
 * @param {object} [options] - react-hot-toast options override.
 * @returns {string} Toast ID.
 */
export function showInfo(message, options = {}) {
  return toast(message, {
    duration: 4000,
    icon: "\u2139\uFE0F",
    style: {
      borderLeft: "4px solid #0ea5e9",
    },
    ...options,
  });
}

/**
 * Show a warning toast notification.
 *
 * @param {string} message  - Notification text.
 * @param {object} [options] - react-hot-toast options override.
 * @returns {string} Toast ID.
 */
export function showWarning(message, options = {}) {
  return toast(message, {
    duration: 5000,
    icon: "\u26A0\uFE0F",
    style: {
      borderLeft: "4px solid #f59e0b",
    },
    ...options,
  });
}

/**
 * Show a loading toast that can be updated later.
 *
 * @param {string} message  - Loading text.
 * @returns {string} Toast ID (use with toast.dismiss or toast.success to update).
 */
export function showLoading(message) {
  return toast.loading(message);
}

/**
 * Dismiss a specific toast by ID, or all toasts if no ID is provided.
 *
 * @param {string} [toastId] - Toast ID to dismiss.
 */
export function dismissToast(toastId) {
  if (toastId) {
    toast.dismiss(toastId);
  } else {
    toast.dismiss();
  }
}

/**
 * Promise-based toast that shows loading, success, and error states.
 *
 * @param {Promise} promise - The promise to track.
 * @param {{ loading: string, success: string, error: string }} messages - Status messages.
 * @param {object} [options] - react-hot-toast options override.
 * @returns {Promise} The original promise.
 */
export function showPromise(promise, messages, options = {}) {
  return toast.promise(promise, messages, options);
}