The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
fix QR code
Steven Tey1b3cd44eb14b
diff --git a/apps/web/app/api/qr/route.tsx b/apps/web/app/api/qr/route.tsxindex f1d9e653b7..99d5f80a76 100644--- a/apps/web/app/api/qr/route.tsx+++ b/apps/web/app/api/qr/route.tsx@@ -1,10 +1,9 @@-import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";-import { extractPublishableKey, ratelimitOrThrow } from "@/lib/api/utils";-import { getWorkspaceByPublishableKey } from "@/lib/planetscale";+import { handleAndReturnErrorResponse } from "@/lib/api/errors";+import { ratelimitOrThrow } from "@/lib/api/utils";+import { getShortLinkViaEdge, getWorkspaceViaEdge } from "@/lib/planetscale";import { QRCodeSVG } from "@/lib/qr/utils";import { getQRCodeQuerySchema } from "@/lib/zod/schemas/qr";-import { getSearchParams } from "@dub/utils";-import { DUB_QR_LOGO } from "@dub/utils/src/constants";+import { DUB_QR_LOGO, getSearchParams } from "@dub/utils";import { ImageResponse } from "next/og";import { NextRequest } from "next/server";@@ -14,21 +13,21 @@ export async function GET(req: NextRequest) {try {const params = getSearchParams(req.url);- const { url, logo, size, level, fgColor, bgColor, includeMargin } =+ let { url, logo, size, level, fgColor, bgColor, hideLogo, includeMargin } =getQRCodeQuerySchema.parse(params);- if (logo !== DUB_QR_LOGO) {- const publishableKey = extractPublishableKey(req);- await ratelimitOrThrow(req, "qr"); // rate limit in case of DDoS- const workspace = await getWorkspaceByPublishableKey(publishableKey);+ await ratelimitOrThrow(req, "qr");+ const shortLink = await getShortLinkViaEdge(url.split("?")[0]);+ if (shortLink) {+ const workspace = await getWorkspaceViaEdge(shortLink.projectId);if (!workspace || workspace.plan === "free") {- throw new DubApiError({- code: "unauthorized",- message:- "You can only use a logo with a paid plan and by authenticating via a publishable key. Learn more: https://d.to/pk",- });+ logo = DUB_QR_LOGO;+ } else if (workspace.logo && !hideLogo) {+ logo = workspace.logo;}+ } else {+ logo = DUB_QR_LOGO;}return new ImageResponse(@@ -39,12 +38,16 @@ export async function GET(req: NextRequest) {includeMargin,fgColor,bgColor,- imageSettings: {- src: logo,- height: size / 4,- width: size / 4,- excavate: true,- },+ ...(logo+ ? {+ imageSettings: {+ src: logo,+ height: size / 4,+ width: size / 4,+ excavate: true,+ },+ }+ : {}),isOGContext: true,}),{diff --git a/apps/web/lib/planetscale.ts b/apps/web/lib/planetscale.tsindex b2c3890189..0c7a1d7628 100644--- a/apps/web/lib/planetscale.ts+++ b/apps/web/lib/planetscale.ts@@ -46,6 +46,28 @@ export const checkIfUserExists = async (userId: string) => {return rows && Array.isArray(rows) && rows.length > 0;};+interface EdgeLinkProps {+ id: string;+ domain: string;+ key: string;+ url: string;+ proxy: number;+ title: string;+ description: string;+ image: string;+ video: string;+ rewrite: number;+ password: string | null;+ expiresAt: string | null;+ ios: string | null;+ android: string | null;+ geo: object | null;+ projectId: string;+ publicStats: number;+ expiredUrl: string | null;+ createdAt: string;+}+export const getLinkViaEdge = async (domain: string, key: string) => {if (!DATABASE_URL) return null;@@ -56,27 +78,20 @@ export const getLinkViaEdge = async (domain: string, key: string) => {)) || {};return rows && Array.isArray(rows) && rows.length > 0- ? (rows[0] as {- id: string;- domain: string;- key: string;- url: string;- proxy: number;- title: string;- description: string;- image: string;- video: string;- rewrite: number;- password: string | null;- expiresAt: string | null;- ios: string | null;- android: string | null;- geo: object | null;- projectId: string;- publicStats: number;- expiredUrl: string | null;- createdAt: string;- })+ ? (rows[0] as EdgeLinkProps)+ : null;+};++export const getShortLinkViaEdge = async (shortLink: string) => {+ if (!DATABASE_URL) return null;++ const { rows } =+ (await conn.execute("SELECT * FROM Link WHERE shortLink = ?", [+ shortLink,+ ])) || {};++ return rows && Array.isArray(rows) && rows.length > 0+ ? (rows[0] as EdgeLinkProps): null;};diff --git a/apps/web/lib/qr/index.tsx b/apps/web/lib/qr/index.tsxindex 4588647bb1..a107259e95 100644--- a/apps/web/lib/qr/index.tsx+++ b/apps/web/lib/qr/index.tsx@@ -346,12 +346,12 @@ export async function getQRAsCanvas(export function getQRData({url,fgColor,- showLogo,+ hideLogo,logo,}: {url: string;fgColor?: string;- showLogo?: boolean;+ hideLogo?: boolean;logo?: string;scale?: number;}) {@@ -361,8 +361,9 @@ export function getQRData({fgColor,size: 1024,level: "Q", // QR Code error correction level: https://blog.qrstuff.com/general/qr-code-error-correction+ hideLogo,includeMargin: false,- ...(showLogo && {+ ...(!hideLogo && {imageSettings: {src: logo || DUB_QR_LOGO,height: 256,diff --git a/apps/web/lib/zod/schemas/qr.ts b/apps/web/lib/zod/schemas/qr.tsindex 2e6e6c2cde..7872181a12 100644--- a/apps/web/lib/zod/schemas/qr.ts+++ b/apps/web/lib/zod/schemas/qr.ts@@ -5,7 +5,6 @@ import {QR_LEVELS,} from "@/lib/qr/constants";import z from "@/lib/zod";-import { DUB_QR_LOGO } from "@dub/utils/src/constants";import { booleanQuerySchema } from "./misc";import { parseUrlSchema } from "./utils";@@ -14,9 +13,8 @@ export const getQRCodeQuerySchema = z.object({logo: z.string().optional()- .default(DUB_QR_LOGO).describe(- "The logo to include in the QR code. Can only be used with a paid plan – please authenticate via a publishable key. Learn more: https://d.to/pk",+ "The logo to include in the QR code. Can only be used with a paid plan on Dub.co.",),size: z.coerce.number()@@ -46,6 +44,12 @@ export const getQRCodeQuerySchema = z.object({.describe("The background color of the QR code in hex format. Defaults to `#ffffff` if not provided.",),+ hideLogo: booleanQuerySchema+ .optional()+ .default("false")+ .describe(+ "Whether to hide the logo in the QR code. Can only be used with a paid plan on Dub.co.",+ ),includeMargin: booleanQuerySchema.optional().default(`${DEFAULT_INCLUDEMARGIN}`)diff --git a/apps/web/ui/modals/link-builder/qr-code-preview.tsx b/apps/web/ui/modals/link-builder/qr-code-preview.tsxindex cec6f27ebe..3f5ea6fde5 100644--- a/apps/web/ui/modals/link-builder/qr-code-preview.tsx+++ b/apps/web/ui/modals/link-builder/qr-code-preview.tsx@@ -30,7 +30,7 @@ export function QRCodePreview() {`qr-code-design-${workspaceId}`,{fgColor: "#000000",- showLogo: true,+ hideLogo: true,},);@@ -38,7 +38,7 @@ export function QRCodePreview() {return key && domain ? linkConstructor({ key, domain }) : undefined;}, [key, domain]);- const showLogo = data.showLogo || workspacePlan === "free";+ const hideLogo = data.hideLogo && workspacePlan !== "free";const logo =workspaceLogo && workspacePlan !== "free" ? workspaceLogo : DUB_QR_LOGO;@@ -89,7 +89,7 @@ export function QRCodePreview() {<QRCodeurl={shortLinkUrl}fgColor={data.fgColor}- showLogo={showLogo}+ hideLogo={hideLogo}logo={logo}scale={0.5}/>diff --git a/apps/web/ui/modals/link-qr-modal.tsx b/apps/web/ui/modals/link-qr-modal.tsxindex d8c329f8c9..db93fdcc91 100644--- a/apps/web/ui/modals/link-qr-modal.tsx+++ b/apps/web/ui/modals/link-qr-modal.tsx@@ -54,7 +54,7 @@ const DEFAULT_COLORS = [export type QRCodeDesign = {fgColor: string;- showLogo: boolean;+ hideLogo: boolean;};type LinkQRModalProps = {@@ -102,14 +102,14 @@ function LinkQRModalInner({`qr-code-design-${workspaceId}`,{fgColor: "#000000",- showLogo: true,+ hideLogo: false,},);const [data, setData] = useState(dataPersisted);const logo = workspaceLogo && plan !== "free" ? workspaceLogo : DUB_QR_LOGO;- const showLogo = data.showLogo || plan === "free";+ const hideLogo = data.hideLogo && plan !== "free";const qrData = useMemo(() =>@@ -117,11 +117,11 @@ function LinkQRModalInner({? getQRData({url,fgColor: data.fgColor,- showLogo,+ hideLogo,logo,}): null,- [url, data, logo],+ [url, data, hideLogo, logo],);return (@@ -205,7 +205,7 @@ function LinkQRModalInner({{url && (<AnimatePresence mode="wait"><motion.div- key={data.fgColor + data.showLogo}+ key={data.fgColor + data.hideLogo}initial={{ filter: "blur(2px)", opacity: 0.4 }}animate={{ filter: "blur(0px)", opacity: 1 }}exit={{ filter: "blur(2px)", opacity: 0.4 }}@@ -215,7 +215,7 @@ function LinkQRModalInner({<QRCodeurl={url}fgColor={data.fgColor}- showLogo={data.showLogo}+ hideLogo={data.hideLogo}logo={logo}scale={1}/>@@ -245,10 +245,10 @@ function LinkQRModalInner({/></div><Switch- id={`${id}-show-logo`}- checked={data.showLogo}- fn={(checked) => {- setData((d) => ({ ...d, showLogo: checked }));+ id={`${id}-hide-logo`}+ checked={!data.hideLogo}+ fn={() => {+ setData((d) => ({ ...d, hideLogo: !d.hideLogo }));}}disabledTooltip={plan === "free" ? (@@ -493,7 +493,7 @@ function CopyPopover({searchParams: {qr: "1",},- })}${qrData.imageSettings?.src ? `&logo=${qrData.imageSettings.src}` : ""}`,+ })}${qrData.hideLogo ? "&hideLogo=true" : ""}`,);toast.success("Copied QR code URL to clipboard!");setCopiedURL(true);diff --git a/apps/web/ui/shared/qr-code.tsx b/apps/web/ui/shared/qr-code.tsxindex cdc77a273d..45880988bb 100644--- a/apps/web/ui/shared/qr-code.tsx+++ b/apps/web/ui/shared/qr-code.tsx@@ -4,19 +4,19 @@ import { useMemo } from "react";export function QRCode({url,fgColor,- showLogo,+ hideLogo,logo,scale = 1,}: {url: string;fgColor?: string;- showLogo?: boolean;+ hideLogo?: boolean;logo?: string;scale?: number;}) {const qrData = useMemo(- () => getQRData({ url, fgColor, showLogo, logo }),- [url, fgColor, showLogo, logo],+ () => getQRData({ url, fgColor, hideLogo, logo }),+ [url, fgColor, hideLogo, logo],);return (