From 0393c2635bf0a4314792d6bd19901b07e81e2977 Mon Sep 17 00:00:00 2001 From: Yash Kumar Date: Wed, 8 Jul 2026 10:17:05 +0530 Subject: [PATCH 1/8] feat: add drag-and-drop logo upload for organizations - Integrated Dropzone component into handle-organization.tsx. - Resolved dialog auto-close bug by decoupling Dialog from DropdownMenu trigger. - Added DOMPurify to sanitize uploaded SVG logos preventing XSS. - Implemented client-side compression via canvas to resize raster images to 256x256 WebP. - Enhanced UX with instant logo preview, masked base64 input strings, and a quick-clear button. - Updated Dropzone to accept classNameContent for local height overrides. --- .../organization/handle-organization.tsx | 232 +++++++++++++++--- apps/dokploy/components/layouts/side.tsx | 46 +++- apps/dokploy/components/ui/dropzone.tsx | 8 +- 3 files changed, 248 insertions(+), 38 deletions(-) diff --git a/apps/dokploy/components/dashboard/organization/handle-organization.tsx b/apps/dokploy/components/dashboard/organization/handle-organization.tsx index 1a3bd919d..a2a61047e 100644 --- a/apps/dokploy/components/dashboard/organization/handle-organization.tsx +++ b/apps/dokploy/components/dashboard/organization/handle-organization.tsx @@ -1,5 +1,6 @@ import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema"; -import { PenBoxIcon, Plus } from "lucide-react"; +import DOMPurify from "dompurify"; +import { GlobeIcon, PenBoxIcon, Plus, X } from "lucide-react"; import { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; @@ -15,6 +16,7 @@ import { DialogTrigger, } from "@/components/ui/dialog"; import { DropdownMenuItem } from "@/components/ui/dropdown-menu"; +import { Dropzone } from "@/components/ui/dropzone"; import { Form, FormControl, @@ -38,10 +40,63 @@ type OrganizationFormValues = z.infer; interface Props { organizationId?: string; children?: React.ReactNode; + open?: boolean; + onOpenChange?: (open: boolean) => void; } -export function AddOrganization({ organizationId }: Props) { - const [open, setOpen] = useState(false); +const sanitizeSvg = (svgContent: string): string | null => { + const clean = DOMPurify.sanitize(svgContent, { + USE_PROFILES: { svg: true, svgFilters: true }, + ADD_TAGS: ["use"], + }); + if (!clean) return null; + return `data:image/svg+xml;base64,${btoa(clean)}`; +}; + +const resizeImage = (file: File, maxSize: number): Promise => { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = (event) => { + const img = new Image(); + img.onload = () => { + let { width, height } = img; + + if (width > maxSize || height > maxSize) { + if (width > height) { + height = Math.round((height * maxSize) / width); + width = maxSize; + } else { + width = Math.round((width * maxSize) / height); + height = maxSize; + } + } + + const canvas = document.createElement("canvas"); + canvas.width = width; + canvas.height = height; + const ctx = canvas.getContext("2d"); + if (!ctx) { + resolve(event.target?.result as string); + return; + } + + ctx.drawImage(img, 0, 0, width, height); + resolve(canvas.toDataURL("image/webp", 0.8)); + }; + img.onerror = reject; + img.src = event.target?.result as string; + }; + reader.onerror = reject; + reader.readAsDataURL(file); + }); +}; + +export function AddOrganization({ organizationId, open: controlledOpen, onOpenChange: controlledOnOpenChange }: Props) { + const [internalOpen, setInternalOpen] = useState(false); + const [uploadedFileName, setUploadedFileName] = useState(null); + const isControlled = controlledOpen !== undefined; + const open = isControlled ? controlledOpen : internalOpen; + const setOpen = isControlled ? controlledOnOpenChange! : setInternalOpen; const utils = api.useUtils(); const { data: organization } = api.organization.one.useQuery( { @@ -69,6 +124,7 @@ export function AddOrganization({ organizationId }: Props) { name: organization.name, logo: organization.logo || "", }); + setUploadedFileName(null); } }, [organization, form]); @@ -80,6 +136,7 @@ export function AddOrganization({ organizationId }: Props) { }) .then(() => { form.reset(); + setUploadedFileName(null); toast.success( `Organization ${organizationId ? "updated" : "created"} successfully`, ); @@ -99,30 +156,86 @@ export function AddOrganization({ organizationId }: Props) { }); }; + const handleFileUpload = async (files: FileList | null) => { + if (!files || files.length === 0) return; + const file = files[0]; + if (!file) return; + + const allowedTypes = [ + "image/jpeg", + "image/jpg", + "image/png", + "image/svg+xml", + "image/webp", + ]; + const fileExtension = file.name.split(".").pop()?.toLowerCase(); + const allowedExtensions = ["jpg", "jpeg", "png", "svg", "webp"]; + + if ( + !allowedTypes.includes(file.type) && + !allowedExtensions.includes(fileExtension || "") + ) { + toast.error("Only JPG, JPEG, PNG, WEBP, and SVG files are allowed"); + return; + } + + if (file.size > 2 * 1024 * 1024) { + toast.error("Image size must be less than 2MB"); + return; + } + + const isSvg = file.type === "image/svg+xml" || fileExtension === "svg"; + + if (isSvg) { + const text = await file.text(); + const sanitizedDataUrl = sanitizeSvg(text); + if (!sanitizedDataUrl) { + toast.error("Invalid SVG file"); + return; + } + form.setValue("logo", sanitizedDataUrl); + form.trigger("logo"); + setUploadedFileName(file.name); + return; + } + + // Resize raster images to max 256x256 and convert to WebP to save space + try { + const resizedDataUrl = await resizeImage(file, 256); + form.setValue("logo", resizedDataUrl); + form.trigger("logo"); + setUploadedFileName(file.name); + } catch (error) { + toast.error("Error processing image"); + } + }; + return ( - - {organizationId ? ( - e.preventDefault()} - > - - - ) : ( - e.preventDefault()} - > -
- -
-
- Add organization -
-
- )} -
+ {!isControlled && ( + + {organizationId ? ( + e.preventDefault()} + > + + + ) : ( + e.preventDefault()} + > +
+ +
+
+ Add organization +
+
+ )} +
+ )} @@ -159,20 +272,71 @@ export function AddOrganization({ organizationId }: Props) { ( + render={({ field }) => { + const isDataUrl = field.value?.startsWith("data:"); + const displayValue = isDataUrl + ? uploadedFileName || "Uploaded image" + : field.value || ""; + + return ( - Logo URL + + Logo URL or Upload + - +
+
+
+ {field.value ? ( + // biome-ignore lint/performance/noImgElement: user uploaded logo preview + Logo preview + ) : ( + + )} +
+
+ { + field.onChange(e); + if (isDataUrl) setUploadedFileName(null); + }} + className="w-full pr-8" + /> + {field.value && ( + + )} +
+
+ +
- )} + ); + }} /> {org.ownerId === session?.user?.id && ( <> - + { + e.preventDefault(); + setEditOrganizationId(org.id); + }} + > + + - + { + e.preventDefault(); + setIsAddOrganizationOpen(true); + }} + > +
+ +
+
+ Add organization +
+
)} @@ -870,6 +895,23 @@ function SidebarLogo() { )} + {editOrganizationId && ( + { + if (!open) setEditOrganizationId(null); + }} + /> + )} + {isAddOrganizationOpen && ( + { + if (!open) setIsAddOrganizationOpen(false); + }} + /> + )} ); } diff --git a/apps/dokploy/components/ui/dropzone.tsx b/apps/dokploy/components/ui/dropzone.tsx index 6d9c05ef6..460935078 100644 --- a/apps/dokploy/components/ui/dropzone.tsx +++ b/apps/dokploy/components/ui/dropzone.tsx @@ -10,13 +10,14 @@ interface DropzoneProps "value" | "onChange" > { classNameWrapper?: string; + classNameContent?: string; className?: string; dropMessage: string; onChange: (acceptedFiles: FileList | null) => void; } export const Dropzone = React.forwardRef( - ({ className, classNameWrapper, dropMessage, onChange, ...props }, ref) => { + ({ className, classNameWrapper, classNameContent, dropMessage, onChange, ...props }, ref) => { const inputRef = useRef(null); // Function to handle drag over event const handleDragOver = (e: React.DragEvent) => { @@ -51,7 +52,10 @@ export const Dropzone = React.forwardRef( )} > Date: Wed, 8 Jul 2026 05:00:40 +0000 Subject: [PATCH 2/8] [autofix.ci] apply automated fixes --- .../organization/handle-organization.tsx | 114 +++++++++--------- apps/dokploy/components/layouts/side.tsx | 4 +- apps/dokploy/components/ui/command.tsx | 4 +- apps/dokploy/components/ui/dropzone.tsx | 14 ++- 4 files changed, 75 insertions(+), 61 deletions(-) diff --git a/apps/dokploy/components/dashboard/organization/handle-organization.tsx b/apps/dokploy/components/dashboard/organization/handle-organization.tsx index a2a61047e..4868ce37f 100644 --- a/apps/dokploy/components/dashboard/organization/handle-organization.tsx +++ b/apps/dokploy/components/dashboard/organization/handle-organization.tsx @@ -91,7 +91,11 @@ const resizeImage = (file: File, maxSize: number): Promise => { }); }; -export function AddOrganization({ organizationId, open: controlledOpen, onOpenChange: controlledOnOpenChange }: Props) { +export function AddOrganization({ + organizationId, + open: controlledOpen, + onOpenChange: controlledOnOpenChange, +}: Props) { const [internalOpen, setInternalOpen] = useState(false); const [uploadedFileName, setUploadedFileName] = useState(null); const isControlled = controlledOpen !== undefined; @@ -277,64 +281,64 @@ export function AddOrganization({ organizationId, open: controlledOpen, onOpenCh const displayValue = isDataUrl ? uploadedFileName || "Uploaded image" : field.value || ""; - + return ( - - - Logo URL or Upload - - -
-
-
- {field.value ? ( - // biome-ignore lint/performance/noImgElement: user uploaded logo preview - Logo preview - ) : ( - - )} -
-
- { - field.onChange(e); - if (isDataUrl) setUploadedFileName(null); - }} - className="w-full pr-8" - /> - {field.value && ( - - )} + className="w-full pr-8" + /> + {field.value && ( + + )} +
+
- - -
- -
+ + + ); }} /> diff --git a/apps/dokploy/components/layouts/side.tsx b/apps/dokploy/components/layouts/side.tsx index 9a7a972e3..1e78604d8 100644 --- a/apps/dokploy/components/layouts/side.tsx +++ b/apps/dokploy/components/layouts/side.tsx @@ -576,7 +576,9 @@ function SidebarLogo() { const [_activeTeam, setActiveTeam] = useState< typeof activeOrganization | null >(null); - const [editOrganizationId, setEditOrganizationId] = useState(null); + const [editOrganizationId, setEditOrganizationId] = useState( + null, + ); const [isAddOrganizationOpen, setIsAddOrganizationOpen] = useState(false); useEffect(() => { diff --git a/apps/dokploy/components/ui/command.tsx b/apps/dokploy/components/ui/command.tsx index 6e5b0921d..99866b79f 100644 --- a/apps/dokploy/components/ui/command.tsx +++ b/apps/dokploy/components/ui/command.tsx @@ -56,9 +56,7 @@ function CommandDialog({ {title} {description}
- - {children} - + {children}
); diff --git a/apps/dokploy/components/ui/dropzone.tsx b/apps/dokploy/components/ui/dropzone.tsx index 460935078..5ae7c9d3e 100644 --- a/apps/dokploy/components/ui/dropzone.tsx +++ b/apps/dokploy/components/ui/dropzone.tsx @@ -17,7 +17,17 @@ interface DropzoneProps } export const Dropzone = React.forwardRef( - ({ className, classNameWrapper, classNameContent, dropMessage, onChange, ...props }, ref) => { + ( + { + className, + classNameWrapper, + classNameContent, + dropMessage, + onChange, + ...props + }, + ref, + ) => { const inputRef = useRef(null); // Function to handle drag over event const handleDragOver = (e: React.DragEvent) => { @@ -54,7 +64,7 @@ export const Dropzone = React.forwardRef( Date: Tue, 1 Sep 2026 21:31:36 +0530 Subject: [PATCH 3/8] Cleanup leftover unused code in side.tsx --- apps/dokploy/components/layouts/side.tsx | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/apps/dokploy/components/layouts/side.tsx b/apps/dokploy/components/layouts/side.tsx index df02dd54b..cd38dc53e 100644 --- a/apps/dokploy/components/layouts/side.tsx +++ b/apps/dokploy/components/layouts/side.tsx @@ -907,23 +907,6 @@ function SidebarLogo() { )} - {editOrganizationId && ( - { - if (!open) setEditOrganizationId(null); - }} - /> - )} - {isAddOrganizationOpen && ( - { - if (!open) setIsAddOrganizationOpen(false); - }} - /> - )} ); } From 6d51c100ae76469ef464c7b39e0bd1bb2d2725ee Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 1 Sep 2026 16:02:55 +0000 Subject: [PATCH 4/8] [autofix.ci] apply automated fixes --- apps/dokploy/components/layouts/side.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/apps/dokploy/components/layouts/side.tsx b/apps/dokploy/components/layouts/side.tsx index cd38dc53e..66504d7e2 100644 --- a/apps/dokploy/components/layouts/side.tsx +++ b/apps/dokploy/components/layouts/side.tsx @@ -29,7 +29,6 @@ import { type LucideIcon, Package, Palette, - Server, ShieldCheck, Smartphone, @@ -755,7 +754,6 @@ function SidebarLogo() { : "Set as default" } > - {isDefault ? ( Date: Wed, 2 Sep 2026 01:55:12 +0530 Subject: [PATCH 5/8] fix: resolve SVG unicode base64 encoding error and image upload race conditions --- .../organization/handle-organization.tsx | 69 +++++++++++++++---- 1 file changed, 56 insertions(+), 13 deletions(-) diff --git a/apps/dokploy/components/dashboard/organization/handle-organization.tsx b/apps/dokploy/components/dashboard/organization/handle-organization.tsx index bc2e015d0..81fc12534 100644 --- a/apps/dokploy/components/dashboard/organization/handle-organization.tsx +++ b/apps/dokploy/components/dashboard/organization/handle-organization.tsx @@ -1,7 +1,7 @@ import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema"; import DOMPurify from "dompurify"; import { GlobeIcon, PenBoxIcon, Plus, X } from "lucide-react"; -import { useEffect, useState } from "react"; +import { useEffect, useState, useRef } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; @@ -49,7 +49,12 @@ const sanitizeSvg = (svgContent: string): string | null => { ADD_TAGS: ["use"], }); if (!clean) return null; - return `data:image/svg+xml;base64,${btoa(clean)}`; + const bytes = new TextEncoder().encode(clean); + let binString = ""; + for (let i = 0; i < bytes.length; i++) { + binString += String.fromCharCode(bytes[i]!); + } + return `data:image/svg+xml;base64,${btoa(binString)}`; }; const resizeImage = (file: File, maxSize: number): Promise => { @@ -97,6 +102,8 @@ export function AddOrganization({ }: Props) { const [internalOpen, setInternalOpen] = useState(false); const [uploadedFileName, setUploadedFileName] = useState(null); + const [isUploading, setIsUploading] = useState(false); + const uploadCounter = useRef(0); const isControlled = controlledOpen !== undefined; const open = isControlled ? controlledOpen : internalOpen; const setOpen = isControlled ? controlledOnOpenChange! : setInternalOpen; @@ -124,6 +131,8 @@ export function AddOrganization({ useEffect(() => { if (organization) { + uploadCounter.current++; + setIsUploading(false); form.reset({ name: organization.name, logo: organization.logo || "", @@ -133,6 +142,7 @@ export function AddOrganization({ }, [organization, form]); const onSubmit = async (values: OrganizationFormValues) => { + if (isUploading) return; await mutateAsync({ name: values.name, logo: values.logo, @@ -165,6 +175,9 @@ export function AddOrganization({ const file = files[0]; if (!file) return; + const currentUploadId = ++uploadCounter.current; + setIsUploading(true); + const allowedTypes = [ "image/jpeg", "image/jpg", @@ -180,42 +193,68 @@ export function AddOrganization({ !allowedExtensions.includes(fileExtension || "") ) { toast.error("Only JPG, JPEG, PNG, WEBP, and SVG files are allowed"); + setIsUploading(false); return; } if (file.size > 2 * 1024 * 1024) { toast.error("Image size must be less than 2MB"); + setIsUploading(false); return; } const isSvg = file.type === "image/svg+xml" || fileExtension === "svg"; if (isSvg) { - const text = await file.text(); - const sanitizedDataUrl = sanitizeSvg(text); - if (!sanitizedDataUrl) { - toast.error("Invalid SVG file"); - return; + try { + const text = await file.text(); + const sanitizedDataUrl = sanitizeSvg(text); + if (currentUploadId !== uploadCounter.current) return; + if (!sanitizedDataUrl) { + toast.error("Invalid SVG file"); + return; + } + form.setValue("logo", sanitizedDataUrl); + form.trigger("logo"); + setUploadedFileName(file.name); + } catch (error) { + if (currentUploadId === uploadCounter.current) { + toast.error("Error processing SVG"); + } + } finally { + if (currentUploadId === uploadCounter.current) { + setIsUploading(false); + } } - form.setValue("logo", sanitizedDataUrl); - form.trigger("logo"); - setUploadedFileName(file.name); return; } // Resize raster images to max 256x256 and convert to WebP to save space try { const resizedDataUrl = await resizeImage(file, 256); + if (currentUploadId !== uploadCounter.current) return; form.setValue("logo", resizedDataUrl); form.trigger("logo"); setUploadedFileName(file.name); } catch (error) { - toast.error("Error processing image"); + if (currentUploadId === uploadCounter.current) { + toast.error("Error processing image"); + } + } finally { + if (currentUploadId === uploadCounter.current) { + setIsUploading(false); + } } }; return ( - + { + if (!val) { + uploadCounter.current++; + setIsUploading(false); + } + setOpen(val); + }}> {organizationId ? ( From 3893150f5cd1685d7271c90915cb22ad90da5f76 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 1 Sep 2026 20:57:36 +0000 Subject: [PATCH 6/8] [autofix.ci] apply automated fixes --- .../organization/handle-organization.tsx | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/apps/dokploy/components/dashboard/organization/handle-organization.tsx b/apps/dokploy/components/dashboard/organization/handle-organization.tsx index 81fc12534..5c7b20194 100644 --- a/apps/dokploy/components/dashboard/organization/handle-organization.tsx +++ b/apps/dokploy/components/dashboard/organization/handle-organization.tsx @@ -248,13 +248,16 @@ export function AddOrganization({ }; return ( - { - if (!val) { - uploadCounter.current++; - setIsUploading(false); - } - setOpen(val); - }}> + { + if (!val) { + uploadCounter.current++; + setIsUploading(false); + } + setOpen(val); + }} + > {organizationId ? (