11import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema" ;
2- import { PenBoxIcon , Plus } from "lucide-react" ;
2+ import DOMPurify from "dompurify" ;
3+ import { GlobeIcon , PenBoxIcon , Plus , X } from "lucide-react" ;
34import { useEffect , useState } from "react" ;
45import { useForm } from "react-hook-form" ;
56import { toast } from "sonner" ;
@@ -15,6 +16,7 @@ import {
1516 DialogTrigger ,
1617} from "@/components/ui/dialog" ;
1718import { DropdownMenuItem } from "@/components/ui/dropdown-menu" ;
19+ import { Dropzone } from "@/components/ui/dropzone" ;
1820import {
1921 Form ,
2022 FormControl ,
@@ -38,10 +40,63 @@ type OrganizationFormValues = z.infer<typeof organizationSchema>;
3840interface Props {
3941 organizationId ?: string ;
4042 children ?: React . ReactNode ;
43+ open ?: boolean ;
44+ onOpenChange ?: ( open : boolean ) => void ;
4145}
4246
43- export function AddOrganization ( { organizationId } : Props ) {
44- const [ open , setOpen ] = useState ( false ) ;
47+ const sanitizeSvg = ( svgContent : string ) : string | null => {
48+ const clean = DOMPurify . sanitize ( svgContent , {
49+ USE_PROFILES : { svg : true , svgFilters : true } ,
50+ ADD_TAGS : [ "use" ] ,
51+ } ) ;
52+ if ( ! clean ) return null ;
53+ return `data:image/svg+xml;base64,${ btoa ( clean ) } ` ;
54+ } ;
55+
56+ const resizeImage = ( file : File , maxSize : number ) : Promise < string > => {
57+ return new Promise ( ( resolve , reject ) => {
58+ const reader = new FileReader ( ) ;
59+ reader . onload = ( event ) => {
60+ const img = new Image ( ) ;
61+ img . onload = ( ) => {
62+ let { width, height } = img ;
63+
64+ if ( width > maxSize || height > maxSize ) {
65+ if ( width > height ) {
66+ height = Math . round ( ( height * maxSize ) / width ) ;
67+ width = maxSize ;
68+ } else {
69+ width = Math . round ( ( width * maxSize ) / height ) ;
70+ height = maxSize ;
71+ }
72+ }
73+
74+ const canvas = document . createElement ( "canvas" ) ;
75+ canvas . width = width ;
76+ canvas . height = height ;
77+ const ctx = canvas . getContext ( "2d" ) ;
78+ if ( ! ctx ) {
79+ resolve ( event . target ?. result as string ) ;
80+ return ;
81+ }
82+
83+ ctx . drawImage ( img , 0 , 0 , width , height ) ;
84+ resolve ( canvas . toDataURL ( "image/webp" , 0.8 ) ) ;
85+ } ;
86+ img . onerror = reject ;
87+ img . src = event . target ?. result as string ;
88+ } ;
89+ reader . onerror = reject ;
90+ reader . readAsDataURL ( file ) ;
91+ } ) ;
92+ } ;
93+
94+ export function AddOrganization ( { organizationId, open : controlledOpen , onOpenChange : controlledOnOpenChange } : Props ) {
95+ const [ internalOpen , setInternalOpen ] = useState ( false ) ;
96+ const [ uploadedFileName , setUploadedFileName ] = useState < string | null > ( null ) ;
97+ const isControlled = controlledOpen !== undefined ;
98+ const open = isControlled ? controlledOpen : internalOpen ;
99+ const setOpen = isControlled ? controlledOnOpenChange ! : setInternalOpen ;
45100 const utils = api . useUtils ( ) ;
46101 const { data : organization } = api . organization . one . useQuery (
47102 {
@@ -69,6 +124,7 @@ export function AddOrganization({ organizationId }: Props) {
69124 name : organization . name ,
70125 logo : organization . logo || "" ,
71126 } ) ;
127+ setUploadedFileName ( null ) ;
72128 }
73129 } , [ organization , form ] ) ;
74130
@@ -80,6 +136,7 @@ export function AddOrganization({ organizationId }: Props) {
80136 } )
81137 . then ( ( ) => {
82138 form . reset ( ) ;
139+ setUploadedFileName ( null ) ;
83140 toast . success (
84141 `Organization ${ organizationId ? "updated" : "created" } successfully` ,
85142 ) ;
@@ -99,30 +156,86 @@ export function AddOrganization({ organizationId }: Props) {
99156 } ) ;
100157 } ;
101158
159+ const handleFileUpload = async ( files : FileList | null ) => {
160+ if ( ! files || files . length === 0 ) return ;
161+ const file = files [ 0 ] ;
162+ if ( ! file ) return ;
163+
164+ const allowedTypes = [
165+ "image/jpeg" ,
166+ "image/jpg" ,
167+ "image/png" ,
168+ "image/svg+xml" ,
169+ "image/webp" ,
170+ ] ;
171+ const fileExtension = file . name . split ( "." ) . pop ( ) ?. toLowerCase ( ) ;
172+ const allowedExtensions = [ "jpg" , "jpeg" , "png" , "svg" , "webp" ] ;
173+
174+ if (
175+ ! allowedTypes . includes ( file . type ) &&
176+ ! allowedExtensions . includes ( fileExtension || "" )
177+ ) {
178+ toast . error ( "Only JPG, JPEG, PNG, WEBP, and SVG files are allowed" ) ;
179+ return ;
180+ }
181+
182+ if ( file . size > 2 * 1024 * 1024 ) {
183+ toast . error ( "Image size must be less than 2MB" ) ;
184+ return ;
185+ }
186+
187+ const isSvg = file . type === "image/svg+xml" || fileExtension === "svg" ;
188+
189+ if ( isSvg ) {
190+ const text = await file . text ( ) ;
191+ const sanitizedDataUrl = sanitizeSvg ( text ) ;
192+ if ( ! sanitizedDataUrl ) {
193+ toast . error ( "Invalid SVG file" ) ;
194+ return ;
195+ }
196+ form . setValue ( "logo" , sanitizedDataUrl ) ;
197+ form . trigger ( "logo" ) ;
198+ setUploadedFileName ( file . name ) ;
199+ return ;
200+ }
201+
202+ // Resize raster images to max 256x256 and convert to WebP to save space
203+ try {
204+ const resizedDataUrl = await resizeImage ( file , 256 ) ;
205+ form . setValue ( "logo" , resizedDataUrl ) ;
206+ form . trigger ( "logo" ) ;
207+ setUploadedFileName ( file . name ) ;
208+ } catch ( error ) {
209+ toast . error ( "Error processing image" ) ;
210+ }
211+ } ;
212+
102213 return (
103214 < Dialog open = { open } onOpenChange = { setOpen } >
104- < DialogTrigger asChild >
105- { organizationId ? (
106- < DropdownMenuItem
107- className = "group cursor-pointer hover:bg-blue-500/10"
108- onSelect = { ( e ) => e . preventDefault ( ) }
109- >
110- < PenBoxIcon className = "size-3.5 text-primary group-hover:text-blue-500" />
111- </ DropdownMenuItem >
112- ) : (
113- < DropdownMenuItem
114- className = "gap-2 p-2"
115- onSelect = { ( e ) => e . preventDefault ( ) }
116- >
117- < div className = "flex size-6 items-center justify-center rounded-md border bg-background" >
118- < Plus className = "size-4" />
119- </ div >
120- < div className = "font-medium text-muted-foreground" >
121- Add organization
122- </ div >
123- </ DropdownMenuItem >
124- ) }
125- </ DialogTrigger >
215+ { ! isControlled && (
216+ < DialogTrigger asChild >
217+ { organizationId ? (
218+ < DropdownMenuItem
219+ className = "group cursor-pointer hover:bg-blue-500/10"
220+ onSelect = { ( e ) => e . preventDefault ( ) }
221+ >
222+ < PenBoxIcon className = "size-3.5 text-primary group-hover:text-blue-500" />
223+ </ DropdownMenuItem >
224+ ) : (
225+ < DropdownMenuItem
226+ className = "gap-2 p-2"
227+ onSelect = { ( e ) => e . preventDefault ( ) }
228+ >
229+ < div className = "flex size-6 items-center justify-center rounded-md border bg-background" >
230+ < Plus className = "size-4" />
231+ </ div >
232+ < div className = "font-medium text-muted-foreground" >
233+ Add organization
234+ </ div >
235+ </ DropdownMenuItem >
236+ ) }
237+ </ DialogTrigger >
238+ ) }
126239 < DialogContent className = "sm:max-w-[425px]" >
127240 < DialogHeader >
128241 < DialogTitle >
@@ -159,20 +272,71 @@ export function AddOrganization({ organizationId }: Props) {
159272 < FormField
160273 control = { form . control }
161274 name = "logo"
162- render = { ( { field } ) => (
275+ render = { ( { field } ) => {
276+ const isDataUrl = field . value ?. startsWith ( "data:" ) ;
277+ const displayValue = isDataUrl
278+ ? uploadedFileName || "Uploaded image"
279+ : field . value || "" ;
280+
281+ return (
163282 < FormItem className = "gap-4" >
164- < FormLabel className = "text-right" > Logo URL</ FormLabel >
283+ < FormLabel className = "text-right" >
284+ Logo URL or Upload
285+ </ FormLabel >
165286 < FormControl >
166- < Input
167- placeholder = "https://example.com/logo.png"
168- { ...field }
169- value = { field . value || "" }
170- className = "col-span-3"
171- />
287+ < div className = "col-span-3 flex flex-col gap-3" >
288+ < div className = "flex items-center gap-3" >
289+ < div className = "flex size-10 shrink-0 items-center justify-center rounded-md border bg-muted/50 p-1" >
290+ { field . value ? (
291+ // biome-ignore lint/performance/noImgElement: user uploaded logo preview
292+ < img
293+ src = { field . value }
294+ alt = "Logo preview"
295+ className = "size-full object-contain"
296+ />
297+ ) : (
298+ < GlobeIcon className = "size-5 text-muted-foreground" />
299+ ) }
300+ </ div >
301+ < div className = "relative flex-1" >
302+ < Input
303+ placeholder = "https://example.com/logo.png"
304+ { ...field }
305+ value = { displayValue }
306+ readOnly = { isDataUrl }
307+ onChange = { ( e ) => {
308+ field . onChange ( e ) ;
309+ if ( isDataUrl ) setUploadedFileName ( null ) ;
310+ } }
311+ className = "w-full pr-8"
312+ />
313+ { field . value && (
314+ < button
315+ type = "button"
316+ onClick = { ( ) => {
317+ form . setValue ( "logo" , "" ) ;
318+ setUploadedFileName ( null ) ;
319+ } }
320+ className = "absolute right-2 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground"
321+ >
322+ < X className = "size-4" />
323+ </ button >
324+ ) }
325+ </ div >
326+ </ div >
327+ < Dropzone
328+ dropMessage = "Drag & drop a logo or click to upload"
329+ accept = ".jpg,.jpeg,.png,.svg,.webp,image/jpeg,image/png,image/svg+xml,image/webp"
330+ onChange = { handleFileUpload }
331+ classNameWrapper = "border-2 border-dashed border-border hover:border-primary bg-muted/30 hover:bg-muted/50 transition-all rounded-lg"
332+ classNameContent = "h-32"
333+ />
334+ </ div >
172335 </ FormControl >
173336 < FormMessage className = "col-span-3 col-start-2" />
174337 </ FormItem >
175- ) }
338+ ) ;
339+ } }
176340 />
177341 < DialogFooter >
178342 < Button type = "submit" isLoading = { isPending } >
0 commit comments