Skip to content

Commit 4877c4f

Browse files
committed
fix: address PR feedback (SVG helper & AddOrganization crash)
1 parent f617561 commit 4877c4f

3 files changed

Lines changed: 25 additions & 27 deletions

File tree

‎apps/dokploy/components/dashboard/application/icon/show-icon-settings.tsx‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import DOMPurify from "dompurify";
21
import { CircuitBoard, GlobeIcon, Pencil, Search, X } from "lucide-react";
32
import { useEffect, useMemo, useState } from "react";
43
import { toast } from "sonner";
@@ -14,6 +13,7 @@ import { Dropzone } from "@/components/ui/dropzone";
1413
import { Input } from "@/components/ui/input";
1514
import { type BundledIcon, bundledIcons } from "@/lib/bundled-icons";
1615
import { api } from "@/utils/api";
16+
import { sanitizeSvg } from "@/utils/sanitize-svg";
1717

1818
interface ShowIconSettingsProps {
1919
serviceId: string;
@@ -89,15 +89,6 @@ export const ShowIconSettings = ({
8989
}
9090
};
9191

92-
const sanitizeSvg = (svgContent: string): string | null => {
93-
const clean = DOMPurify.sanitize(svgContent, {
94-
USE_PROFILES: { svg: true, svgFilters: true },
95-
ADD_TAGS: ["use"],
96-
});
97-
if (!clean) return null;
98-
return `data:image/svg+xml;base64,${btoa(clean)}`;
99-
};
100-
10192
const handleFileUpload = async (files: FileList | null) => {
10293
if (!files || files.length === 0) return;
10394
const file = files[0];

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

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema";
2-
import DOMPurify from "dompurify";
2+
33
import { GlobeIcon, PenBoxIcon, Plus, X } from "lucide-react";
4-
import { useEffect, useState, useRef } from "react";
4+
import { useEffect, useRef, useState } from "react";
55
import { useForm } from "react-hook-form";
66
import { toast } from "sonner";
77
import { z } from "zod";
@@ -26,6 +26,7 @@ import {
2626
} from "@/components/ui/form";
2727
import { Input } from "@/components/ui/input";
2828
import { api } from "@/utils/api";
29+
import { sanitizeSvg } from "@/utils/sanitize-svg";
2930

3031
const organizationSchema = z.object({
3132
name: z.string().min(1, {
@@ -43,20 +44,6 @@ interface Props {
4344
onOpenChange?: (open: boolean) => void;
4445
}
4546

46-
const sanitizeSvg = (svgContent: string): string | null => {
47-
const clean = DOMPurify.sanitize(svgContent, {
48-
USE_PROFILES: { svg: true, svgFilters: true },
49-
ADD_TAGS: ["use"],
50-
});
51-
if (!clean) return null;
52-
const bytes = new TextEncoder().encode(clean);
53-
let binString = "";
54-
for (let i = 0; i < bytes.length; i++) {
55-
binString += String.fromCharCode(bytes[i]!);
56-
}
57-
return `data:image/svg+xml;base64,${btoa(binString)}`;
58-
};
59-
6047
const resizeImage = (file: File, maxSize: number): Promise<string> => {
6148
return new Promise((resolve, reject) => {
6249
const reader = new FileReader();
@@ -106,7 +93,9 @@ export function AddOrganization({
10693
const uploadCounter = useRef(0);
10794
const isControlled = controlledOpen !== undefined;
10895
const open = isControlled ? controlledOpen : internalOpen;
109-
const setOpen = isControlled ? controlledOnOpenChange! : setInternalOpen;
96+
const setOpen = isControlled
97+
? controlledOnOpenChange || (() => {})
98+
: setInternalOpen;
11099
const utils = api.useUtils();
111100
const { data: organization } = api.organization.one.useQuery(
112101
{

‎apps/dokploy/utils/sanitize-svg.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import DOMPurify from "dompurify";
2+
3+
export const sanitizeSvg = (svgContent: string): string | null => {
4+
const clean = DOMPurify.sanitize(svgContent, {
5+
USE_PROFILES: { svg: true, svgFilters: true },
6+
});
7+
8+
if (!clean) return null;
9+
10+
// Fix unicode base64 bug (TextEncoder byte-loop handles non-Latin1 chars)
11+
const bytes = new TextEncoder().encode(clean);
12+
let binString = "";
13+
for (let i = 0; i < bytes.length; i++) {
14+
binString += String.fromCharCode(bytes[i]!);
15+
}
16+
17+
return `data:image/svg+xml;base64,${btoa(binString)}`;
18+
};

0 commit comments

Comments
 (0)