@orsetra/shared-ui 1.0.22 → 1.0.24

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.
@@ -0,0 +1,131 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import { cva, type VariantProps } from "class-variance-authority"
5
+ import { X, CheckCircle, AlertCircle, AlertTriangle, Info } from "lucide-react"
6
+ import { cn } from "../../lib/utils"
7
+
8
+ const alertBannerVariants = cva(
9
+ "relative w-full flex items-center gap-3 px-4 py-3 text-sm font-medium transition-all duration-300",
10
+ {
11
+ variants: {
12
+ variant: {
13
+ success: "bg-green-50 text-green-800 border-b border-green-200",
14
+ error: "bg-red-50 text-red-800 border-b border-red-200",
15
+ warning: "bg-amber-50 text-amber-800 border-b border-amber-200",
16
+ info: "bg-blue-50 text-blue-800 border-b border-blue-200",
17
+ },
18
+ },
19
+ defaultVariants: {
20
+ variant: "info",
21
+ },
22
+ }
23
+ )
24
+
25
+ const iconMap = {
26
+ success: CheckCircle,
27
+ error: AlertCircle,
28
+ warning: AlertTriangle,
29
+ info: Info,
30
+ }
31
+
32
+ export interface AlertBannerProps
33
+ extends React.HTMLAttributes<HTMLDivElement>,
34
+ VariantProps<typeof alertBannerVariants> {
35
+ message: string
36
+ onClose?: () => void
37
+ closable?: boolean
38
+ icon?: React.ReactNode
39
+ }
40
+
41
+ const AlertBanner = React.forwardRef<HTMLDivElement, AlertBannerProps>(
42
+ ({ className, variant = "info", message, onClose, closable = true, icon, ...props }, ref) => {
43
+ const Icon = iconMap[variant || "info"]
44
+
45
+ return (
46
+ <div
47
+ ref={ref}
48
+ role="alert"
49
+ className={cn(alertBannerVariants({ variant }), className)}
50
+ {...props}
51
+ >
52
+ {icon || <Icon className="h-5 w-5 flex-shrink-0" />}
53
+ <span className="flex-1">{message}</span>
54
+ {closable && onClose && (
55
+ <button
56
+ onClick={onClose}
57
+ className="flex-shrink-0 p-1 rounded-md hover:bg-black/5 transition-colors"
58
+ aria-label="Fermer"
59
+ >
60
+ <X className="h-4 w-4" />
61
+ </button>
62
+ )}
63
+ </div>
64
+ )
65
+ }
66
+ )
67
+ AlertBanner.displayName = "AlertBanner"
68
+
69
+ // Hook pour gérer les alertes
70
+ export interface AlertState {
71
+ show: boolean
72
+ variant: "success" | "error" | "warning" | "info"
73
+ message: string
74
+ }
75
+
76
+ export function useAlertBanner() {
77
+ const [alert, setAlert] = React.useState<AlertState>({
78
+ show: false,
79
+ variant: "info",
80
+ message: "",
81
+ })
82
+
83
+ const showAlert = React.useCallback(
84
+ (variant: AlertState["variant"], message: string, duration = 5000) => {
85
+ setAlert({ show: true, variant, message })
86
+
87
+ if (duration > 0) {
88
+ setTimeout(() => {
89
+ setAlert((prev) => ({ ...prev, show: false }))
90
+ }, duration)
91
+ }
92
+ },
93
+ []
94
+ )
95
+
96
+ const hideAlert = React.useCallback(() => {
97
+ setAlert((prev) => ({ ...prev, show: false }))
98
+ }, [])
99
+
100
+ const showSuccess = React.useCallback(
101
+ (message: string, duration?: number) => showAlert("success", message, duration),
102
+ [showAlert]
103
+ )
104
+
105
+ const showError = React.useCallback(
106
+ (message: string, duration?: number) => showAlert("error", message, duration),
107
+ [showAlert]
108
+ )
109
+
110
+ const showWarning = React.useCallback(
111
+ (message: string, duration?: number) => showAlert("warning", message, duration),
112
+ [showAlert]
113
+ )
114
+
115
+ const showInfo = React.useCallback(
116
+ (message: string, duration?: number) => showAlert("info", message, duration),
117
+ [showAlert]
118
+ )
119
+
120
+ return {
121
+ alert,
122
+ showAlert,
123
+ hideAlert,
124
+ showSuccess,
125
+ showError,
126
+ showWarning,
127
+ showInfo,
128
+ }
129
+ }
130
+
131
+ export { AlertBanner }
@@ -21,6 +21,7 @@ export { ImageCropDialog } from './image-crop-dialog'
21
21
  export { Accordion, AccordionItem, AccordionTrigger, AccordionContent } from './accordion'
22
22
  export { AlertDialog, AlertDialogTrigger, AlertDialogContent, AlertDialogHeader, AlertDialogFooter, AlertDialogTitle, AlertDialogDescription, AlertDialogAction, AlertDialogCancel } from './alert-dialog'
23
23
  export { Alert, AlertTitle, AlertDescription } from './alert'
24
+ export { AlertBanner, useAlertBanner, type AlertBannerProps, type AlertState } from './alert-banner'
24
25
  export { AspectRatio } from './aspect-ratio'
25
26
  export { Badge } from './badge'
26
27
  export { Breadcrumb, BreadcrumbList, BreadcrumbItem, BreadcrumbLink, BreadcrumbPage, BreadcrumbSeparator, BreadcrumbEllipsis } from './breadcrumb'
@@ -56,7 +56,20 @@ class HttpClient {
56
56
  });
57
57
 
58
58
  if (!response.ok) {
59
- throw new Error(`Request failed: ${response.status}`);
59
+ // Try to extract error message from API response
60
+ let errorMessage = `Request failed: ${response.status}`;
61
+ try {
62
+ const errorBody = await response.json();
63
+ // Zitadel API returns { code, message, details } on error
64
+ if (errorBody?.message) {
65
+ errorMessage = errorBody.message;
66
+ } else if (errorBody?.error) {
67
+ errorMessage = errorBody.error;
68
+ }
69
+ } catch {
70
+ // If we can't parse JSON, use the default message
71
+ }
72
+ throw new Error(errorMessage);
60
73
  }
61
74
 
62
75
  if (response.status === 204) {
@@ -89,6 +102,14 @@ class HttpClient {
89
102
  });
90
103
  }
91
104
 
105
+ async patch<T>(url: string, body?: any, options: RequestInit = {}): Promise<T | null> {
106
+ return this.request<T>(url, {
107
+ ...options,
108
+ method: 'PATCH',
109
+ body: body ? JSON.stringify(body) : undefined
110
+ });
111
+ }
112
+
92
113
  async delete<T>(url: string, options: RequestInit = {}): Promise<T | null> {
93
114
  return this.request<T>(url, {
94
115
  ...options,
@@ -113,8 +134,23 @@ class HttpClient {
113
134
  });
114
135
 
115
136
  if (!response.ok) {
116
- const errorText = await response.text();
117
- throw new Error(`Upload failed: ${response.status} ${response.statusText} - ${errorText}`);
137
+ // Try to extract error message from API response
138
+ let errorMessage = `Upload failed: ${response.status} ${response.statusText}`;
139
+ try {
140
+ const errorBody = await response.json();
141
+ if (errorBody?.message) {
142
+ errorMessage = errorBody.message;
143
+ } else if (errorBody?.error) {
144
+ errorMessage = errorBody.error;
145
+ }
146
+ } catch {
147
+ // Try text if JSON fails
148
+ try {
149
+ const errorText = await response.text();
150
+ if (errorText) errorMessage = errorText;
151
+ } catch { /* ignore */ }
152
+ }
153
+ throw new Error(errorMessage);
118
154
  }
119
155
 
120
156
  if (response.status === 204) {
@@ -135,7 +171,17 @@ class HttpClient {
135
171
  });
136
172
 
137
173
  if (!response.ok) {
138
- throw new Error(`Download failed: ${response.status} ${response.statusText}`);
174
+ // Try to extract error message from API response
175
+ let errorMessage = `Download failed: ${response.status} ${response.statusText}`;
176
+ try {
177
+ const errorBody = await response.json();
178
+ if (errorBody?.message) {
179
+ errorMessage = errorBody.message;
180
+ } else if (errorBody?.error) {
181
+ errorMessage = errorBody.error;
182
+ }
183
+ } catch { /* ignore */ }
184
+ throw new Error(errorMessage);
139
185
  }
140
186
 
141
187
  return await response.blob();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orsetra/shared-ui",
3
- "version": "1.0.22",
3
+ "version": "1.0.24",
4
4
  "description": "Shared UI components for Orsetra platform",
5
5
  "main": "./index.ts",
6
6
  "types": "./index.ts",
@@ -92,4 +92,4 @@
92
92
  "next": "^16.0.7",
93
93
  "typescript": "^5"
94
94
  }
95
- }
95
+ }