@moises.ai/design-system 3.10.12 → 3.10.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moises.ai/design-system",
3
- "version": "3.10.12",
3
+ "version": "3.10.13",
4
4
  "description": "Design System package based on @radix-ui/themes with custom defaults",
5
5
  "private": false,
6
6
  "type": "module",
@@ -1,4 +1,12 @@
1
- import React, { createContext, useCallback, useContext, useMemo, useState } from 'react'
1
+ import React, {
2
+ createContext,
3
+ useCallback,
4
+ useContext,
5
+ useEffect,
6
+ useMemo,
7
+ useRef,
8
+ useState,
9
+ } from 'react'
2
10
  import { Toast } from 'radix-ui'
3
11
  import { Callout } from '../Callout/Callout'
4
12
  import styles from './ToastProvider.module.css'
@@ -30,9 +38,31 @@ export const ToastProvider = ({
30
38
  return 'neutral'
31
39
  }, [])
32
40
 
41
+ const getCalloutStyle = useCallback((variant) => {
42
+ if (variant === 'success') {
43
+ return {
44
+ backgroundColor: '#113B29',
45
+ color: '#BBFFD7',
46
+ }
47
+ }
48
+ if (variant === 'error') {
49
+ return {
50
+ backgroundColor: '#201314',
51
+ color: '#FF9592',
52
+ }
53
+ }
54
+ return undefined
55
+ }, [])
56
+
33
57
  const [toasts, setToasts] = useState([])
58
+ const timeoutIdsRef = useRef(new Map())
34
59
 
35
60
  const dismiss = useCallback((id) => {
61
+ const timeoutId = timeoutIdsRef.current.get(id)
62
+ if (timeoutId) {
63
+ clearTimeout(timeoutId)
64
+ timeoutIdsRef.current.delete(id)
65
+ }
36
66
  setToasts((prev) => prev.filter((toastItem) => toastItem.id !== id))
37
67
  }, [])
38
68
 
@@ -43,10 +73,32 @@ export const ToastProvider = ({
43
73
  ...normalized,
44
74
  id: `${Date.now()}-${toastId++}`,
45
75
  }
46
- setToasts((prev) => [...prev.slice(-(maxToasts - 1)), nextToast])
76
+
77
+ const toastDuration = nextToast.duration ?? duration
78
+ const timeoutId = setTimeout(() => {
79
+ dismiss(nextToast.id)
80
+ }, toastDuration)
81
+
82
+ timeoutIdsRef.current.set(nextToast.id, timeoutId)
83
+ setToasts((prev) => {
84
+ const nextToasts = [...prev.slice(-(maxToasts - 1)), nextToast]
85
+ const nextToastIds = new Set(nextToasts.map((toastItem) => toastItem.id))
86
+
87
+ prev.forEach((toastItem) => {
88
+ if (!nextToastIds.has(toastItem.id)) {
89
+ const staleTimeoutId = timeoutIdsRef.current.get(toastItem.id)
90
+ if (staleTimeoutId) {
91
+ clearTimeout(staleTimeoutId)
92
+ timeoutIdsRef.current.delete(toastItem.id)
93
+ }
94
+ }
95
+ })
96
+
97
+ return nextToasts
98
+ })
47
99
  return nextToast.id
48
100
  },
49
- [maxToasts],
101
+ [dismiss, duration, maxToasts],
50
102
  )
51
103
 
52
104
  const success = useCallback(
@@ -73,6 +125,13 @@ export const ToastProvider = ({
73
125
  [show, success, error, dismiss],
74
126
  )
75
127
 
128
+ useEffect(() => {
129
+ return () => {
130
+ timeoutIdsRef.current.forEach((timeoutId) => clearTimeout(timeoutId))
131
+ timeoutIdsRef.current.clear()
132
+ }
133
+ }, [])
134
+
76
135
  return (
77
136
  <ToastContext.Provider value={value}>
78
137
  <Toast.Provider
@@ -98,6 +157,7 @@ export const ToastProvider = ({
98
157
  title={toastItem.title}
99
158
  text={toastItem.description}
100
159
  className={styles.toastCallout}
160
+ style={getCalloutStyle(toastItem.variant)}
101
161
  />
102
162
  </div>
103
163
  </Toast.Description>