@orsetra/shared-ui 1.0.3 → 1.0.5

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.
Files changed (2) hide show
  1. package/hooks/use-toast.ts +187 -6
  2. package/package.json +5 -1
@@ -1,8 +1,189 @@
1
- // Placeholder - will be implemented with actual toast logic
2
- export function useToast() {
3
- return {
4
- toast: (options: any) => {
5
- console.log('Toast:', options);
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+
5
+ const TOAST_LIMIT = 1
6
+ const TOAST_REMOVE_DELAY = 1000000
7
+
8
+ type ToasterToast = {
9
+ id: string
10
+ title?: React.ReactNode
11
+ description?: React.ReactNode
12
+ action?: React.ReactElement
13
+ open?: boolean
14
+ onOpenChange?: (open: boolean) => void
15
+ variant?: "default" | "destructive"
16
+ }
17
+
18
+ const actionTypes = {
19
+ ADD_TOAST: "ADD_TOAST",
20
+ UPDATE_TOAST: "UPDATE_TOAST",
21
+ DISMISS_TOAST: "DISMISS_TOAST",
22
+ REMOVE_TOAST: "REMOVE_TOAST",
23
+ } as const
24
+
25
+ let count = 0
26
+
27
+ function genId() {
28
+ count = (count + 1) % Number.MAX_SAFE_INTEGER
29
+ return count.toString()
30
+ }
31
+
32
+ type ActionType = typeof actionTypes
33
+
34
+ type Action =
35
+ | {
36
+ type: ActionType["ADD_TOAST"]
37
+ toast: ToasterToast
38
+ }
39
+ | {
40
+ type: ActionType["UPDATE_TOAST"]
41
+ toast: Partial<ToasterToast>
42
+ }
43
+ | {
44
+ type: ActionType["DISMISS_TOAST"]
45
+ toastId?: ToasterToast["id"]
46
+ }
47
+ | {
48
+ type: ActionType["REMOVE_TOAST"]
49
+ toastId?: ToasterToast["id"]
50
+ }
51
+
52
+ interface State {
53
+ toasts: ToasterToast[]
54
+ }
55
+
56
+ const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>()
57
+
58
+ const addToRemoveQueue = (toastId: string) => {
59
+ if (toastTimeouts.has(toastId)) {
60
+ return
61
+ }
62
+
63
+ const timeout = setTimeout(() => {
64
+ toastTimeouts.delete(toastId)
65
+ dispatch({
66
+ type: "REMOVE_TOAST",
67
+ toastId: toastId,
68
+ })
69
+ }, TOAST_REMOVE_DELAY)
70
+
71
+ toastTimeouts.set(toastId, timeout)
72
+ }
73
+
74
+ export const reducer = (state: State, action: Action): State => {
75
+ switch (action.type) {
76
+ case "ADD_TOAST":
77
+ return {
78
+ ...state,
79
+ toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT),
80
+ }
81
+
82
+ case "UPDATE_TOAST":
83
+ return {
84
+ ...state,
85
+ toasts: state.toasts.map((t) =>
86
+ t.id === action.toast.id ? { ...t, ...action.toast } : t
87
+ ),
88
+ }
89
+
90
+ case "DISMISS_TOAST": {
91
+ const { toastId } = action
92
+
93
+ if (toastId) {
94
+ addToRemoveQueue(toastId)
95
+ } else {
96
+ state.toasts.forEach((toast) => {
97
+ addToRemoveQueue(toast.id)
98
+ })
99
+ }
100
+
101
+ return {
102
+ ...state,
103
+ toasts: state.toasts.map((t) =>
104
+ t.id === toastId || toastId === undefined
105
+ ? {
106
+ ...t,
107
+ open: false,
108
+ }
109
+ : t
110
+ ),
111
+ }
112
+ }
113
+ case "REMOVE_TOAST":
114
+ if (action.toastId === undefined) {
115
+ return {
116
+ ...state,
117
+ toasts: [],
118
+ }
119
+ }
120
+ return {
121
+ ...state,
122
+ toasts: state.toasts.filter((t) => t.id !== action.toastId),
123
+ }
124
+ }
125
+ }
126
+
127
+ const listeners: Array<(state: State) => void> = []
128
+
129
+ let memoryState: State = { toasts: [] }
130
+
131
+ function dispatch(action: Action) {
132
+ memoryState = reducer(memoryState, action)
133
+ listeners.forEach((listener) => {
134
+ listener(memoryState)
135
+ })
136
+ }
137
+
138
+ type Toast = Omit<ToasterToast, "id">
139
+
140
+ function toast({ ...props }: Toast) {
141
+ const id = genId()
142
+
143
+ const update = (props: ToasterToast) =>
144
+ dispatch({
145
+ type: "UPDATE_TOAST",
146
+ toast: { ...props, id },
147
+ })
148
+ const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id })
149
+
150
+ dispatch({
151
+ type: "ADD_TOAST",
152
+ toast: {
153
+ ...props,
154
+ id,
155
+ open: true,
156
+ onOpenChange: (open) => {
157
+ if (!open) dismiss()
158
+ },
6
159
  },
7
- };
160
+ })
161
+
162
+ return {
163
+ id: id,
164
+ dismiss,
165
+ update,
166
+ }
167
+ }
168
+
169
+ function useToast() {
170
+ const [state, setState] = React.useState<State>(memoryState)
171
+
172
+ React.useEffect(() => {
173
+ listeners.push(setState)
174
+ return () => {
175
+ const index = listeners.indexOf(setState)
176
+ if (index > -1) {
177
+ listeners.splice(index, 1)
178
+ }
179
+ }
180
+ }, [state])
181
+
182
+ return {
183
+ ...state,
184
+ toast,
185
+ dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId }),
186
+ }
8
187
  }
188
+
189
+ export { useToast, toast }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orsetra/shared-ui",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Shared UI components for Orsetra platform",
5
5
  "main": "./index.ts",
6
6
  "types": "./index.ts",
@@ -41,6 +41,10 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "react-avatar": "^5.0.3",
44
+ "react-hook-form": "^7.54.0",
45
+ "react-resizable-panels": "^2.1.7",
46
+ "@hookform/resolvers": "^3.9.1",
47
+ "zod": "^3.24.1",
44
48
  "clsx": "^2.1.1",
45
49
  "tailwind-merge": "^2.5.5",
46
50
  "class-variance-authority": "^0.7.1",