Skip to content

Commit f6edcac

Browse files
committed
use zustand for toast state
1 parent fc16e78 commit f6edcac

File tree

4 files changed

+32
-49
lines changed

4 files changed

+32
-49
lines changed

app/hooks/use-toast/ToastStack.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ import { animated, useTransition } from '@react-spring/web'
22

33
import { Toast } from '@oxide/ui'
44

5-
import type { Toast as ToastModel } from './types'
5+
import { useToastStore } from './index'
66

7-
interface ToastStackProps {
8-
toasts: ToastModel[]
9-
onRemoveToast: (id: string) => void
10-
}
7+
export function ToastStack() {
8+
const { toasts, remove } = useToastStore(({ toasts, remove }) => ({ toasts, remove }))
119

12-
export const ToastStack = ({ toasts, onRemoveToast }: ToastStackProps) => {
1310
const transition = useTransition(toasts, {
1411
keys: (toast) => toast.id,
1512
from: { opacity: 0, y: 10, scale: 95 },
@@ -32,7 +29,7 @@ export const ToastStack = ({ toasts, onRemoveToast }: ToastStackProps) => {
3229
key={item.id}
3330
{...item.options}
3431
onClose={() => {
35-
onRemoveToast(item.id)
32+
remove(item.id)
3633
item.options.onClose?.()
3734
}}
3835
/>

app/hooks/use-toast/index.tsx

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
1-
import type { FC } from 'react'
2-
import { createContext, useContext, useState } from 'react'
31
import { v4 as uuid } from 'uuid'
2+
import create from 'zustand'
43

5-
import { ToastStack } from './ToastStack'
6-
import type { Toast } from './types'
4+
import type { ToastProps } from '@oxide/ui'
75

8-
type AddToast = (options: Toast['options']) => void
9-
10-
const ToastContext = createContext<AddToast>(() => {})
11-
12-
export const useToast = () => useContext(ToastContext)
13-
14-
export const ToastProvider: FC = ({ children }) => {
15-
const [toasts, setToasts] = useState<Toast[]>([])
6+
type Toast = {
7+
id: string
8+
options: Optional<ToastProps, 'onClose'>
9+
}
1610

17-
const addToast: AddToast = (options) => {
18-
setToasts((toasts) => [...toasts, { id: uuid(), options }])
19-
}
11+
type StoreState = {
12+
toasts: Toast[]
13+
add: (options: Toast['options']) => void
14+
remove: (id: Toast['id']) => void
15+
}
2016

21-
const removeToast = (id: string) => {
22-
setToasts((toasts) => toasts.filter((t) => t.id !== id))
23-
}
17+
export const useToastStore = create<StoreState>((set) => ({
18+
toasts: [],
19+
add: (options) => set(({ toasts }) => ({ toasts: [...toasts, { id: uuid(), options }] })),
20+
remove: (id) => set(({ toasts }) => ({ toasts: toasts.filter((t) => t.id !== id) })),
21+
}))
2422

25-
return (
26-
<ToastContext.Provider value={addToast}>
27-
{children}
28-
<ToastStack toasts={toasts} onRemoveToast={removeToast} />
29-
</ToastContext.Provider>
30-
)
31-
}
23+
export const useToast = () => useToastStore(({ add }) => add)

app/hooks/use-toast/types.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

app/main.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import ReactDOM from 'react-dom'
55
import { SkipLink } from '@oxide/ui'
66

77
import { ErrorBoundary } from './components/ErrorBoundary'
8-
import { QuickActions, ReduceMotion, ToastProvider } from './hooks'
8+
import { QuickActions, ReduceMotion } from './hooks'
9+
import { ToastStack } from './hooks/use-toast/ToastStack'
910
import { Router } from './routes'
1011

1112
if (process.env.SHA) {
@@ -28,16 +29,15 @@ const queryClient = new QueryClient({
2829
function render() {
2930
ReactDOM.render(
3031
<StrictMode>
31-
<ToastProvider>
32-
<QueryClientProvider client={queryClient}>
33-
<ErrorBoundary>
34-
<QuickActions />
35-
<SkipLink id="skip-nav" />
36-
<ReduceMotion />
37-
<Router />
38-
</ErrorBoundary>
39-
</QueryClientProvider>
40-
</ToastProvider>
32+
<QueryClientProvider client={queryClient}>
33+
<ErrorBoundary>
34+
<QuickActions />
35+
<SkipLink id="skip-nav" />
36+
<ReduceMotion />
37+
<Router />
38+
</ErrorBoundary>
39+
</QueryClientProvider>
40+
<ToastStack />
4141
</StrictMode>,
4242
document.getElementById('root')
4343
)

0 commit comments

Comments
 (0)