feat: enhance project management with duplicate name checks and toast notifications

- Added duplicate name check in NewProjectDialog to warn users of existing project names.
- Integrated toast notifications for actions like project deletion and export in ProjectsPage.
- Implemented project ordering and restoration functionality in platform context.
- Updated ProjectsPage to include search, sorting, and pagination features.
- Created a new Toast component for displaying notifications with optional undo actions.
- Refactored main application entry to include ToastProvider for global toast management.
This commit is contained in:
2026-03-09 22:36:14 +01:00
parent e441d36cf4
commit 57dfcb6458
6 changed files with 880 additions and 270 deletions

View File

@@ -0,0 +1,111 @@
/**
* Simple toast: message + optional Undo action. Auto-dismisses after a delay.
*/
import React, { createContext, useCallback, useContext, useState } from 'react'
import { cn } from '@/lib/utils'
export type ToastItem = {
id: string
message: string
undo?: () => void
}
type ToastContextValue = {
toasts: ToastItem[]
addToast: (message: string, options?: { undo?: () => void; duration?: number }) => void
removeToast: (id: string) => void
}
const ToastContext = createContext<ToastContextValue | null>(null)
const TOAST_DURATION = 5000
export function ToastProvider({ children }: { children: React.ReactNode }) {
const [toasts, setToasts] = useState<ToastItem[]>([])
const timersRef = React.useRef<Record<string, ReturnType<typeof setTimeout>>>({})
const removeToast = useCallback((id: string) => {
setToasts((prev) => prev.filter((t) => t.id !== id))
const t = timersRef.current[id]
if (t) {
clearTimeout(t)
delete timersRef.current[id]
}
}, [])
const addToast = useCallback(
(message: string, options?: { undo?: () => void; duration?: number }) => {
const id = `toast-${Date.now()}-${Math.random().toString(36).slice(2)}`
const duration = options?.duration ?? TOAST_DURATION
setToasts((prev) => [...prev, { id, message, undo: options?.undo }])
const timer = setTimeout(() => removeToast(id), duration)
timersRef.current[id] = timer
},
[removeToast]
)
return (
<ToastContext.Provider value={{ toasts, addToast, removeToast }}>
{children}
<ToastViewport toasts={toasts} removeToast={removeToast} />
</ToastContext.Provider>
)
}
function ToastViewport({
toasts,
removeToast,
}: {
toasts: ToastItem[]
removeToast: (id: string) => void
}) {
if (toasts.length === 0) return null
return (
<div
className="fixed bottom-4 right-4 z-[100] flex max-w-[420px] flex-col gap-2"
role="region"
aria-label="Notifications"
>
{toasts.map((t) => (
<div
key={t.id}
className={cn(
'flex items-center justify-between gap-3 rounded-lg border bg-background px-4 py-3 text-sm shadow-lg',
'animate-in slide-in-from-bottom-2 fade-in-0'
)}
>
<span className="flex-1">{t.message}</span>
<div className="flex shrink-0 gap-2">
{t.undo && (
<button
type="button"
className="font-medium text-primary underline-offset-4 hover:underline"
onClick={() => {
t.undo?.()
removeToast(t.id)
}}
>
Undo
</button>
)}
<button
type="button"
className="text-muted-foreground hover:text-foreground"
onClick={() => removeToast(t.id)}
aria-label="Dismiss"
>
×
</button>
</div>
</div>
))}
</div>
)
}
export function useToast() {
const ctx = useContext(ToastContext)
if (!ctx) throw new Error('useToast must be used within ToastProvider')
return ctx
}