summaryrefslogtreecommitdiff
path: root/src/components/Composables/useToast.ts
blob: 595a3bc5775e94e10681f69e7f3be264b4a8056b (plain)
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
import { useToast as useBootstrapToast } from 'bootstrap-vue-next';
import i18n from '@/i18n';

/**
 * Toast message type - currently restricted to string for simplicity.
 * 
 * TODO: If the API ever needs to pass arrays of VNodes (like in BVToastMixin),
 * expand this type to: string | string[] | { children?: string }[]
 * and update normalizeBody to handle VNode extraction properly.
 */
type ToastMessage = string;

export function useToast() {
  const toast = useBootstrapToast();

  const normalizeBody = (message: ToastMessage): string => {
    // Currently only handles strings
    // If expanded to handle arrays/VNodes, add proper type guards and extraction logic
    return String(message ?? '');
  };

  const successToast = (message: ToastMessage): void => {
    toast.create({
      body: normalizeBody(message),
      props: {
        title: i18n.global.t('global.status.success'),
        variant: 'success',
        isStatus: true,
        // modelValue controls auto-hide delay in milliseconds (10 seconds for success)
        // In bootstrap-vue-next, modelValue can be a number (delay) or false (no auto-hide)
        modelValue: 10000,
        solid: false,
      },
    });
  };

  const errorToast = (message: ToastMessage): void => {
    toast.create({
      body: normalizeBody(message),
      props: {
        title: i18n.global.t('global.status.error'),
        variant: 'danger',
        isStatus: true,
        // modelValue: false disables auto-hide for error toasts (user must dismiss manually)
        modelValue: false,
        solid: false,
      },
    });
  };

  return { successToast, errorToast };
}