Skip to content

Commit 0393c26

Browse files
committed
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.
1 parent 7871ce7 commit 0393c26

3 files changed

Lines changed: 248 additions & 38 deletions

File tree

‎apps/dokploy/components/dashboard/organization/handle-organization.tsx‎

Lines changed: 198 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { 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";
34
import { useEffect, useState } from "react";
45
import { useForm } from "react-hook-form";
56
import { toast } from "sonner";
@@ -15,6 +16,7 @@ import {
1516
DialogTrigger,
1617
} from "@/components/ui/dialog";
1718
import { DropdownMenuItem } from "@/components/ui/dropdown-menu";
19+
import { Dropzone } from "@/components/ui/dropzone";
1820
import {
1921
Form,
2022
FormControl,
@@ -38,10 +40,63 @@ type OrganizationFormValues = z.infer<typeof organizationSchema>;
3840
interface 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}>

‎apps/dokploy/components/layouts/side.tsx‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import {
2727
type LucideIcon,
2828
Package,
2929
Palette,
30+
PenBoxIcon,
3031
PieChart,
32+
Plus,
3133
Rocket,
3234
Server,
3335
ShieldCheck,
@@ -574,6 +576,8 @@ function SidebarLogo() {
574576
const [_activeTeam, setActiveTeam] = useState<
575577
typeof activeOrganization | null
576578
>(null);
579+
const [editOrganizationId, setEditOrganizationId] = useState<string | null>(null);
580+
const [isAddOrganizationOpen, setIsAddOrganizationOpen] = useState(false);
577581

578582
useEffect(() => {
579583
if (activeOrganization) {
@@ -736,7 +740,15 @@ function SidebarLogo() {
736740
</Button>
737741
{org.ownerId === session?.user?.id && (
738742
<>
739-
<AddOrganization organizationId={org.id} />
743+
<DropdownMenuItem
744+
className="group cursor-pointer hover:bg-blue-500/10"
745+
onSelect={(e) => {
746+
e.preventDefault();
747+
setEditOrganizationId(org.id);
748+
}}
749+
>
750+
<PenBoxIcon className="size-3.5 text-primary group-hover:text-blue-500" />
751+
</DropdownMenuItem>
740752
<DialogAction
741753
title="Delete Organization"
742754
description="Are you sure you want to delete this organization?"
@@ -780,7 +792,20 @@ function SidebarLogo() {
780792
isCloud) && (
781793
<>
782794
<DropdownMenuSeparator />
783-
<AddOrganization />
795+
<DropdownMenuItem
796+
className="gap-2 p-2"
797+
onSelect={(e) => {
798+
e.preventDefault();
799+
setIsAddOrganizationOpen(true);
800+
}}
801+
>
802+
<div className="flex size-6 items-center justify-center rounded-md border bg-background">
803+
<Plus className="size-4" />
804+
</div>
805+
<div className="font-medium text-muted-foreground">
806+
Add organization
807+
</div>
808+
</DropdownMenuItem>
784809
</>
785810
)}
786811
</DropdownMenuContent>
@@ -870,6 +895,23 @@ function SidebarLogo() {
870895
</SidebarMenuItem>
871896
</SidebarMenu>
872897
)}
898+
{editOrganizationId && (
899+
<AddOrganization
900+
organizationId={editOrganizationId}
901+
open={true}
902+
onOpenChange={(open) => {
903+
if (!open) setEditOrganizationId(null);
904+
}}
905+
/>
906+
)}
907+
{isAddOrganizationOpen && (
908+
<AddOrganization
909+
open={true}
910+
onOpenChange={(open) => {
911+
if (!open) setIsAddOrganizationOpen(false);
912+
}}
913+
/>
914+
)}
873915
</>
874916
);
875917
}

‎apps/dokploy/components/ui/dropzone.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ interface DropzoneProps
1010
"value" | "onChange"
1111
> {
1212
classNameWrapper?: string;
13+
classNameContent?: string;
1314
className?: string;
1415
dropMessage: string;
1516
onChange: (acceptedFiles: FileList | null) => void;
1617
}
1718

1819
export const Dropzone = React.forwardRef<HTMLDivElement, DropzoneProps>(
19-
({ className, classNameWrapper, dropMessage, onChange, ...props }, ref) => {
20+
({ className, classNameWrapper, classNameContent, dropMessage, onChange, ...props }, ref) => {
2021
const inputRef = useRef<HTMLInputElement | null>(null);
2122
// Function to handle drag over event
2223
const handleDragOver = (e: React.DragEvent<HTMLDivElement>) => {
@@ -51,7 +52,10 @@ export const Dropzone = React.forwardRef<HTMLDivElement, DropzoneProps>(
5152
)}
5253
>
5354
<CardContent
54-
className="flex flex-col items-center justify-center space-y-2 px-2 py-4 text-xs h-96"
55+
className={cn(
56+
"flex flex-col items-center justify-center space-y-2 px-2 py-4 text-xs h-96",
57+
classNameContent
58+
)}
5559
onDragOver={handleDragOver}
5660
onDrop={handleDrop}
5761
onClick={handleButtonClick}

0 commit comments

Comments
 (0)