Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

add targetResolution support to FileUpload

Steven Tey4a009df7205e
diff --git a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/page-client.tsx b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/page-client.tsx
index cd2d7bf130..f9196bc6cd 100644
--- a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/page-client.tsx
+++ b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/page-client.tsx
@@ -103,6 +103,7 @@ function ProfileForm({ partner }: { partner: PartnerProps }) {
onChange={({ src }) => field.onChange(src)}
content={null}
maxFileSizeMB={2}
+ targetResolution={{ width: 160, height: 160 }}
/>
)}
/>
diff --git a/apps/web/app/partners.dub.co/(onboarding)/onboarding/onboarding-form.tsx b/apps/web/app/partners.dub.co/(onboarding)/onboarding/onboarding-form.tsx
index fd0b98aac0..d07e51b2cb 100644
--- a/apps/web/app/partners.dub.co/(onboarding)/onboarding/onboarding-form.tsx
+++ b/apps/web/app/partners.dub.co/(onboarding)/onboarding/onboarding-form.tsx
@@ -100,6 +100,7 @@ export function OnboardingForm() {
onChange={({ src }) => field.onChange(src)}
content={null}
maxFileSizeMB={2}
+ targetResolution={{ width: 160, height: 160 }}
/>
)}
/>
diff --git a/apps/web/ui/account/upload-avatar.tsx b/apps/web/ui/account/upload-avatar.tsx
index 6fcf1e00bc..1a05174fd9 100644
--- a/apps/web/ui/account/upload-avatar.tsx
+++ b/apps/web/ui/account/upload-avatar.tsx
@@ -56,6 +56,7 @@ export default function UploadAvatar() {
onChange={({ src }) => setImage(src)}
content={null}
maxFileSizeMB={2}
+ targetResolution={{ width: 160, height: 160 }}
/>
</div>
</div>
diff --git a/apps/web/ui/workspaces/upload-logo.tsx b/apps/web/ui/workspaces/upload-logo.tsx
index 25cf48151b..8015d5ece9 100644
--- a/apps/web/ui/workspaces/upload-logo.tsx
+++ b/apps/web/ui/workspaces/upload-logo.tsx
@@ -60,6 +60,7 @@ export default function UploadLogo() {
onChange={({ src }) => setImage(src)}
content={null}
maxFileSizeMB={2}
+ targetResolution={{ width: 240, height: 240 }}
/>
</div>
</div>
diff --git a/packages/ui/src/file-upload.tsx b/packages/ui/src/file-upload.tsx
index bd3649c94d..085ce84f68 100644
--- a/packages/ui/src/file-upload.tsx
+++ b/packages/ui/src/file-upload.tsx
@@ -1,4 +1,4 @@
-import { cn } from "@dub/utils";
+import { cn, resizeImage } from "@dub/utils";
import { VariantProps, cva } from "class-variance-authority";
import { UploadCloud } from "lucide-react";
import { DragEvent, ReactNode, useState } from "react";
@@ -116,6 +116,7 @@ export function FileUpload({
showHoverOverlay = true,
content,
maxFileSizeMB = 0,
+ targetResolution,
accessibilityLabel = "File upload",
disabled = false,
}: FileUploadProps) {
@@ -147,16 +148,30 @@ export function FileUpload({
return;
}
+ let fileToUse = file;
+
+ // Add image resizing logic
+ if (targetResolution && file.type.startsWith("image/")) {
+ try {
+ const resizedFile = await resizeImage(file, targetResolution);
+ const blob = await fetch(resizedFile).then((r) => r.blob());
+ fileToUse = new File([blob], file.name, { type: file.type });
+ } catch (error) {
+ console.error("Error resizing image:", error);
+ // Fallback to original file if resize fails
+ }
+ }
+
+ // File reading logic
if (readFile) {
const reader = new FileReader();
reader.onload = (e) =>
- onChange?.({ src: e.target?.result as string, file });
- reader.readAsDataURL(file);
-
+ onChange?.({ src: e.target?.result as string, file: fileToUse });
+ reader.readAsDataURL(fileToUse);
return;
}
- onChange?.({ file });
+ onChange?.({ file: fileToUse });
};
return (
diff --git a/packages/utils/src/functions/index.ts b/packages/utils/src/functions/index.ts
index cd999ad8da..046f4f9aca 100644
--- a/packages/utils/src/functions/index.ts
+++ b/packages/utils/src/functions/index.ts
@@ -11,7 +11,6 @@ export * from "./domains";
export * from "./fetch-with-timeout";
export * from "./fetcher";
export * from "./hash-string";
-export * from "./images";
export * from "./is-click-on-interactive-child";
export * from "./is-iframeable";
export * from "./keys";
@@ -23,6 +22,7 @@ export * from "./normalize-string";
export * from "./pluralize";
export * from "./punycode";
export * from "./random-value";
+export * from "./resize-image";
export * from "./smart-truncate";
export * from "./stable-sort";
export * from "./time-ago";
diff --git a/packages/utils/src/functions/images.ts b/packages/utils/src/functions/resize-image.ts
similarity index 99%
rename from packages/utils/src/functions/images.ts
rename to packages/utils/src/functions/resize-image.ts
index 3322905a2e..ac1ce97749 100644
--- a/packages/utils/src/functions/images.ts
+++ b/packages/utils/src/functions/resize-image.ts
@@ -3,7 +3,7 @@ export const resizeImage = (
opts: {
width: number;
height: number;
- quality: number;
+ quality?: number;
} = {
width: 1200, // Desired output width
height: 630, // Desired output height