The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
wip screenshot uploads
Kiran Keb7feeb22253
diff --git a/apps/web/app/api/oauth/apps/upload/route.ts b/apps/web/app/api/oauth/apps/upload/route.tsnew file mode 100644index 0000000000..e9f93068c7--- /dev/null+++ b/apps/web/app/api/oauth/apps/upload/route.ts@@ -0,0 +1,52 @@+import { withSession, withWorkspace } from "@/lib/auth";+import { plain, upsertPlainCustomer } from "@/lib/plain";+import { AttachmentType } from "@team-plain/typescript-sdk";+import { NextResponse } from "next/server";+import { storage } from "@/lib/storage";++// POST /api/oauth/apps/upload – get a signed URL to upload an integration screenshot+export const GET = withWorkspace(async ({ workspace }) => {+ const response = await storage.createPresignedUrl(+ `integrations/${workspace.id}/screenshots/1`+ );++ console.log("RESPONSE", response.json());+++ return NextResponse.json({"name": "kiran"});++ // const plainCustomer = await plain.getCustomerByEmail({+ // email: session.user.email,+ // });++ // if (plainCustomer.data) {+ // plainCustomerId = plainCustomer.data.id;+ // } else {+ // const { data } = await upsertPlainCustomer(session);+ // if (data) {+ // plainCustomerId = data.customer.id;+ // }+ // }++ // if (!plainCustomerId) {+ // return NextResponse.json({+ // error: "Plain customer not found",+ // });+ // }++ // const res = await plain.createAttachmentUploadUrl({+ // customerId: plainCustomerId,+ // fileName: searchParams.name,+ // fileSizeBytes: parseInt(searchParams.size),+ // attachmentType: AttachmentType.CustomTimelineEntry,+ // });++ // if (res.error) {+ // return NextResponse.json({+ // error: res.error,+ // });+ // } else {+ // console.log("Attachment upload stuff:", res.data);+ // return NextResponse.json(res.data);+ // }+});diff --git a/apps/web/lib/edge-config/get-feature-flags.ts b/apps/web/lib/edge-config/get-feature-flags.tsindex 81dcbfe6cd..ad94a82dc6 100644--- a/apps/web/lib/edge-config/get-feature-flags.ts+++ b/apps/web/lib/edge-config/get-feature-flags.ts@@ -6,7 +6,7 @@ type BetaFeaturesRecord = Record<BetaFeatures, string[]>;export const getFeatureFlags = async (workspaceId: string) => {const workspaceFeatures: Record<BetaFeatures, boolean> = {conversions: false,- integrations: false,+ integrations: true,dublink: false,};diff --git a/apps/web/lib/storage.ts b/apps/web/lib/storage.tsindex 24fdc3de15..618afd5307 100644--- a/apps/web/lib/storage.ts+++ b/apps/web/lib/storage.ts@@ -61,6 +61,20 @@ class StorageClient {return { success: true };}+ async createPresignedUrl(key: string) {+ const result = await this.client.sign(+ `${process.env.STORAGE_ENDPOINT}/${key}`,+ {+ method: "GET",+ aws: {+ signQuery: true,+ },+ },+ );++ return result;+ }+private base64ToArrayBuffer(base64: string, opts?: imageOptions) {const base64Data = base64.replace(/^data:.+;base64,/, "");const paddedBase64Data = base64Data.padEnd(diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex 0f84c6d0e5..1a17990aff 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -188,7 +188,7 @@ export type OAuthAppProps = z.infer<typeof oAuthAppSchema>;export type NewIntegration = Omit<OAuthAppProps,- "id" | "clientId" | "verified" | "installations"+ "id" | "clientId" | "verified" | "installations" | "screenshots">;export type ExistingIntegration = OAuthAppProps;diff --git a/apps/web/ui/integrations/add-edit-integration-form.tsx b/apps/web/ui/integrations/add-edit-integration-form.tsxindex dded992bad..8ab8bf8513 100644--- a/apps/web/ui/integrations/add-edit-integration-form.tsx+++ b/apps/web/ui/integrations/add-edit-integration-form.tsx@@ -6,9 +6,16 @@ import {NewIntegration,OAuthAppProps,} from "@/lib/types";-import { Button, FileUpload, InfoTooltip, Switch } from "@dub/ui";+import {+ Button,+ FileUpload,+ InfoTooltip,+ LoadingSpinner,+ Switch,+} from "@dub/ui";import { nanoid } from "@dub/utils";import slugify from "@sindresorhus/slugify";+import { Paperclip, Trash2 } from "lucide-react";import { redirect, useRouter } from "next/navigation";import { FormEvent, useEffect, useMemo, useState } from "react";import TextareaAutosize from "react-textarea-autosize";@@ -41,6 +48,14 @@ export default function AddEditIntegrationForm({{ id: nanoid(), value: "" },]);+ const [screenshots, setScreenshots] = useState<+ {+ file: File;+ uploading: boolean;+ attachmentId?: string;+ }[]+ >([]);+if (!flags?.integrations) {redirect(`/${workspaceSlug}`);}@@ -117,6 +132,55 @@ export default function AddEditIntegrationForm({}};+ // Handle screenshots upload+ const handleUpload = async (file: File) => {+ setScreenshots((prev) => [...prev, { file, uploading: true }]);++ const {+ attachment: { id: attachmentId },+ uploadFormUrl,+ uploadFormData,+ } = await fetch(+ `/api/oauth/apps/upload?name=${file.name}&size=${file.size}&workspaceId=${workspaceId}`,+ {+ method: "GET",+ },+ ).then((res) => res.json());++ const form = new FormData();+ uploadFormData.forEach(({ key, value }) => {+ form.append(key, value);+ });+ form.append("file", file);++ fetch(uploadFormUrl, {+ method: "POST",+ body: form,+ })+ .then((res) => {+ if (!res.ok) {+ toast.error(res.statusText);+ }+ })+ .catch((err) => {+ console.error("Error uploading file:", err.message ? err.message : err);+ });++ setScreenshots((prev) =>+ prev.map((screenshot) =>+ screenshot.file === file+ ? { ...screenshot, uploading: false, attachmentId }+ : screenshot,+ ),+ );+ setData((prev) => ({+ ...prev,+ // attachmentIds: [...prev.attachmentIds, attachmentId],+ }));+ };++ console.log(screenshots);+const {name,slug,@@ -234,6 +298,56 @@ export default function AddEditIntegrationForm({</div></div>+ <div>+ <label htmlFor="slug" className="flex items-center space-x-2">+ <h2 className="text-sm font-medium text-gray-900">Screenshots</h2>+ <InfoTooltip content="Upload screenshots that will be displayed on the integration page." />+ </label>+ <div className="mt-2 grid w-full gap-2">+ {screenshots.map((screenshot) => (+ <div+ key={screenshot.attachmentId}+ className="flex w-full items-center justify-between rounded-md border border-gray-200"+ >+ <div className="flex flex-1 items-center space-x-2 p-2">+ {screenshot.uploading ? (+ <LoadingSpinner className="h-4 w-4" />+ ) : (+ <Paperclip className="h-4 w-4 text-gray-500" />+ )}+ <p className="text-center text-sm text-gray-500">+ {screenshot.file.name}+ </p>+ </div>+ <button+ className="h-full rounded-r-md border-l border-gray-200 p-2"+ onClick={() => {+ setScreenshots((prev) =>+ prev.filter((i) => i.file.name !== screenshot.file.name),+ );+ setData((prev) => ({+ ...prev,+ // attachmentIds: prev.attachmentIds.filter(+ // (id) => id !== screenshot.attachmentId,+ // ),+ }));+ }}+ >+ <Trash2 className="h-4 w-4 text-gray-500" />+ </button>+ </div>+ ))}+ </div>+ <FileUpload+ accept="any"+ className="mt-2 aspect-[5/1] w-full rounded-md border border-dashed border-gray-300"+ iconClassName="w-5 h-5"+ variant="plain"+ onChange={async ({ file }) => await handleUpload(file)}+ content="Drag and drop or click to upload."+ />+ </div>+<div><label htmlFor="developer" className="flex items-center space-x-2"><h2 className="text-sm font-medium text-gray-900">