Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat(#599): Add custom favicon icon in document link (#608)

Ashish Viradiyab178d320d6d2
diff --git a/app/favicon.ico b/app/favicon.ico
deleted file mode 100644
index 129ee060..00000000
Binary files a/app/favicon.ico and /dev/null differ
diff --git a/components/links/link-sheet/index.tsx b/components/links/link-sheet/index.tsx
index bdb53089..11f00d99 100644
--- a/components/links/link-sheet/index.tsx
+++ b/components/links/link-sheet/index.tsx
@@ -60,6 +60,7 @@ export const DEFAULT_LINK_PROPS = (linkType: LinkType) => ({
metaTitle: null,
metaDescription: null,
metaImage: null,
+ metaFavicon: null,
enabledQuestion: false,
questionText: null,
questionType: null,
@@ -91,6 +92,7 @@ export type DEFAULT_LINK_TYPE = {
metaTitle: string | null; // metatags
metaDescription: string | null; // metatags
metaImage: string | null; // metatags
+ metaFavicon: string | null; // metaFavicon
enableQuestion?: boolean; // feedback question
questionText: string | null;
questionType: string | null;
@@ -155,6 +157,20 @@ export default function LinkSheet({
setData({ ...data, metaImage: blobUrl });
}
+ // Upload meta favicon if it's a data URL
+ let blobUrlFavicon: string | null =
+ data.metaFavicon && data.metaFavicon.startsWith("data:")
+ ? null
+ : data.metaFavicon;
+ if (data.metaFavicon && data.metaFavicon.startsWith("data:")) {
+ const blobFavicon = convertDataUrlToFile({ dataUrl: data.metaFavicon });
+ blobUrlFavicon = await uploadImage(blobFavicon);
+ setData({
+ ...data,
+ metaFavicon: blobUrlFavicon,
+ });
+ }
+
let endpoint = "/api/links";
let method = "POST";
@@ -172,6 +188,7 @@ export default function LinkSheet({
body: JSON.stringify({
...data,
metaImage: blobUrl,
+ metaFavicon: blobUrlFavicon,
targetId: targetId,
linkType: linkType,
teamId: teamInfo?.currentTeam?.id,
diff --git a/components/links/link-sheet/og-section.tsx b/components/links/link-sheet/og-section.tsx
index 5b8eb1ed..95b445d3 100644
--- a/components/links/link-sheet/og-section.tsx
+++ b/components/links/link-sheet/og-section.tsx
@@ -1,9 +1,10 @@
-import { useCallback, useEffect, useState } from "react";
+import { ChangeEvent, useCallback, useEffect, useState } from "react";
import { useTeam } from "@/context/team-context";
import { LinkPreset } from "@prisma/client";
+import { Label } from "@radix-ui/react-label";
import { motion } from "framer-motion";
-import { Upload as ArrowUpTrayIcon } from "lucide-react";
+import { Upload as ArrowUpTrayIcon, PlusIcon } from "lucide-react";
import useSWRImmutable from "swr/immutable";
import { Input } from "@/components/ui/input";
@@ -11,7 +12,7 @@ import LoadingSpinner from "@/components/ui/loading-spinner";
import { Textarea } from "@/components/ui/textarea";
import { FADE_IN_ANIMATION_SETTINGS } from "@/lib/constants";
-import { cn, fetcher } from "@/lib/utils";
+import { cn, fetcher, validateImageDimensions } from "@/lib/utils";
import { resizeImage } from "@/lib/utils/resize-image";
import { DEFAULT_LINK_TYPE } from ".";
@@ -35,7 +36,13 @@ export default function OGSection({
}: LinkUpgradeOptions) => void;
editLink: boolean;
}) {
- const { enableCustomMetatag, metaTitle, metaDescription, metaImage } = data;
+ const {
+ enableCustomMetatag,
+ metaTitle,
+ metaDescription,
+ metaImage,
+ metaFavicon,
+ } = data;
const teamInfo = useTeam();
const { data: presets } = useSWRImmutable<LinkPreset>(
`/api/teams/${teamInfo?.currentTeam?.id}/presets`,
@@ -45,6 +52,8 @@ export default function OGSection({
const [enabled, setEnabled] = useState<boolean>(false);
const [fileError, setFileError] = useState<string | null>(null);
const [dragActive, setDragActive] = useState(false);
+ const [faviconFileError, setFaviconFileError] = useState<string | null>(null);
+ const [faviconDragActive, setFaviconDragActive] = useState(false);
const onChangePicture = useCallback(
async (e: any) => {
@@ -72,11 +81,15 @@ export default function OGSection({
}, [enableCustomMetatag]);
useEffect(() => {
- if (presets && !(metaTitle || metaDescription || metaImage)) {
+ if (
+ presets &&
+ !(metaTitle || metaDescription || metaImage || metaFavicon)
+ ) {
const preset = presets;
if (preset) {
setData((prev) => ({
...prev,
+ metaFavicon: prev.metaFavicon || preset.metaFavicon,
metaImage: prev.metaImage || preset.metaImage,
metaTitle: prev.metaTitle || preset.metaTitle,
metaDescription: prev.metaDescription || preset.metaDescription,
@@ -88,6 +101,7 @@ export default function OGSection({
presets,
setData,
editLink,
+ metaFavicon,
enableCustomMetatag,
metaTitle,
metaDescription,
@@ -110,6 +124,48 @@ export default function OGSection({
});
};
+ const onChangeFavicon = useCallback(
+ async (e: ChangeEvent<HTMLInputElement>) => {
+ setFaviconFileError(null);
+ const file = e.target.files && e.target.files[0];
+ if (file) {
+ if (file.size / 1024 / 1024 > 1) {
+ setFaviconFileError("File size too big (max 1MB)");
+ } else if (
+ file.type !== "image/png" &&
+ file.type !== "image/x-icon" &&
+ file.type !== "image/svg+xml"
+ ) {
+ setFaviconFileError(
+ "File type not supported (.png, .ico, .svg only)",
+ );
+ } else {
+ const image = await resizeImage(file, {
+ width: 36,
+ height: 36,
+ quality: 1,
+ });
+ const isValidDimensions = await validateImageDimensions(
+ image,
+ 16,
+ 48,
+ );
+ if (!isValidDimensions) {
+ setFaviconFileError(
+ "Image dimensions must be between 16x16 and 48x48",
+ );
+ } else {
+ setData((prev) => ({
+ ...prev,
+ metaFavicon: image,
+ }));
+ }
+ }
+ }
+ },
+ [setData],
+ );
+
return (
<div className="pb-5">
<LinkItem
@@ -177,7 +233,8 @@ export default function OGSection({
setFileError("File size too big (max 5MB)");
} else if (
file.type !== "image/png" &&
- file.type !== "image/jpeg"
+ file.type !== "image/jpeg" &&
+ file.type !== "image/jpg"
) {
setFileError(
"File type not supported (.png or .jpg only)",
@@ -229,13 +286,137 @@ export default function OGSection({
id="image"
name="image"
type="file"
- accept="image/*"
+ accept="image/png,image/jpeg,image/jpg"
className="sr-only"
onChange={onChangePicture}
/>
</div>
</div>
-
+ <div className="flex flex-col gap-2">
+ <div className="flex items-center justify-between">
+ <Label htmlFor="faviconIcon">
+ <p className="block text-sm font-medium text-foreground">
+ Favicon Icon{" "}
+ <span className="text-sm italic text-muted-foreground">
+ (max 1 MB)
+ </span>
+ </p>
+ </Label>
+ {faviconFileError ? (
+ <p className="text-sm text-red-500">{faviconFileError}</p>
+ ) : null}
+ </div>
+ <label
+ htmlFor="faviconIcon"
+ className="group relative mt-1 flex h-[4rem] w-[12rem] cursor-pointer flex-col items-center justify-center rounded-md border border-gray-300 bg-white shadow-sm transition-all hover:bg-gray-50"
+ style={{
+ backgroundImage:
+ "linear-gradient(45deg, #ccc 25%, transparent 25%), linear-gradient(135deg, #ccc 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #ccc 75%), linear-gradient(135deg, transparent 75%, #ccc 75%)",
+ backgroundSize: "20px 20px",
+ backgroundPosition: "0 0, 10px 0, 10px -10px, 0px 10px",
+ }}
+ >
+ {false && (
+ <div className="absolute z-[5] flex h-full w-full items-center justify-center rounded-md bg-white">
+ <LoadingSpinner />
+ </div>
+ )}
+ <div
+ className="absolute z-[5] h-full w-full rounded-md"
+ onDragOver={(e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setFaviconDragActive(true);
+ }}
+ onDragEnter={(e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setFaviconDragActive(true);
+ }}
+ onDragLeave={(e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setFaviconDragActive(false);
+ }}
+ onDrop={async (e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setFaviconDragActive(false);
+ setFaviconFileError(null);
+ const file = e.dataTransfer.files && e.dataTransfer.files[0];
+ if (file) {
+ if (file.size / 1024 / 1024 > 1) {
+ setFaviconFileError("File size too big (max 1MB)");
+ } else if (
+ file.type !== "image/png" &&
+ file.type !== "image/x-icon" &&
+ file.type !== "image/svg+xml"
+ ) {
+ setFaviconFileError(
+ "File type not supported (.png, .ico, .svg only)",
+ );
+ } else {
+ const image = await resizeImage(file, {
+ width: 36,
+ height: 36,
+ quality: 1,
+ });
+ const isValidDimensions = await validateImageDimensions(
+ image,
+ 16,
+ 48,
+ );
+ if (!isValidDimensions) {
+ setFaviconFileError(
+ "Image dimensions must be between 16x16 and 48x48",
+ );
+ } else {
+ setData((prev) => ({
+ ...prev,
+ metaFavicon: image,
+ }));
+ }
+ }
+ }
+ }}
+ />
+ <div
+ className={`${
+ faviconDragActive
+ ? "cursor-copy border-2 border-black bg-gray-50 opacity-100"
+ : ""
+ } absolute z-[3] flex h-full w-full flex-col items-center justify-center rounded-md bg-white transition-all ${
+ metaFavicon
+ ? "opacity-0 group-hover:opacity-100"
+ : "group-hover:bg-gray-50"
+ }`}
+ >
+ <PlusIcon
+ className={`${
+ faviconDragActive ? "scale-110" : "scale-100"
+ } h-7 w-7 text-gray-500 transition-all duration-75 group-hover:scale-110 group-active:scale-95`}
+ />
+ <span className="sr-only">OG image upload</span>
+ </div>
+ {metaFavicon && (
+ <img
+ src={metaFavicon}
+ alt="Preview"
+ className="h-full w-full rounded-md object-contain"
+ />
+ )}
+ </label>
+ <div className="mt-1 hidden rounded-md shadow-sm">
+ <input
+ id="faviconIcon"
+ name="favicon"
+ type="file"
+ accept="image/png,image/x-icon,image/svg+xml"
+ className="sr-only"
+ onChange={onChangeFavicon}
+ />
+ </div>
+ </div>
<div>
<div className="flex items-center justify-between">
<p className="block text-sm font-medium text-foreground">Title</p>
diff --git a/components/links/links-table.tsx b/components/links/links-table.tsx
index 4ab9b8f7..052230a2 100644
--- a/components/links/links-table.tsx
+++ b/components/links/links-table.tsx
@@ -99,6 +99,7 @@ export default function LinksTable({
metaTitle: link.metaTitle,
metaDescription: link.metaDescription,
metaImage: link.metaImage,
+ metaFavicon: link.metaFavicon,
enableAgreement: link.enableAgreement ? link.enableAgreement : false,
agreementId: link.agreementId,
showBanner: link.showBanner ?? false,
diff --git a/components/settings/og-preview.tsx b/components/settings/og-preview.tsx
index 2a05c790..d6ff325d 100644
--- a/components/settings/og-preview.tsx
+++ b/components/settings/og-preview.tsx
@@ -15,12 +15,14 @@ export default function Preview({
metaImage: string | null;
metaTitle: string | null;
metaDescription: string | null;
+ metaFavicon: string | null;
};
setData: Dispatch<
SetStateAction<{
metaImage: string | null;
metaTitle: string | null;
metaDescription: string | null;
+ metaFavicon: string | null;
}>
>;
}) {
@@ -28,6 +30,7 @@ export default function Preview({
metaTitle: title,
metaDescription: description,
metaImage: image,
+ metaFavicon: favicon,
} = data;
const hostname = "papermark.io";
@@ -54,18 +57,25 @@ export default function Preview({
</div>
</div>
</div>
- <div className="group relative overflow-hidden rounded-2xl border border-border">
+ <div className="group relative overflow-hidden rounded-xl border border-border md:rounded-2xl">
<ImagePreview image={image} />
- {(title || title === "") && (
- <div className="absolute bottom-2 left-2 rounded-md bg-[#414142] px-1.5 py-px">
- <h3 className="max-w-sm truncate text-sm text-white">
+ <div className="absolute bottom-2 left-2 flex min-h-6 flex-row items-center gap-2 rounded-md bg-[#414142] px-2 py-1">
+ <img
+ src={(favicon || "/favicon.ico") as string}
+ alt="Preview"
+ className="h-4 w-4 rounded-md object-contain"
+ />
+ {(title || title === "") && (
+ <h3 className="max-w-64 truncate text-sm text-white">
{title}
</h3>
- </div>
- )}
+ )}
+ </div>
</div>
{hostname && (
- <p className="mt-2 text-[0.8rem] text-[#606770]">{hostname}</p>
+ <div className="flex flex-row">
+ <p className="mt-2 text-[0.8rem] text-[#606770]">{hostname}</p>
+ </div>
)}
</div>
@@ -87,21 +97,29 @@ export default function Preview({
</div>
<div className="relative overflow-hidden rounded-[2px] shadow-[0_0_0_1px_rgba(0,0,0,0.15),0_2px_3px_rgba(0,0,0,0.2)] dark:shadow-[0_0_0_1px_rgba(255,255,255,0.15),0_2px_3px_rgba(255,255,255,0.2)]">
<ImagePreview image={image} />
- <div className="grid gap-1 border-t border-border bg-white p-3">
- {title || title === "" ? (
- <input
- className="truncate border-none bg-transparent p-0 font-semibold text-[#000000E6] outline-none focus:ring-0"
- value={title}
- onChange={(e) => {
- setData((prev) => ({
- ...prev,
- title: e.currentTarget.value,
- }));
- }}
+ <div className="flex flex-col gap-1 border-t border-border bg-white p-3">
+ <div className="flex flex-row items-center gap-2">
+ <img
+ src={(favicon || "/favicon.ico") as string}
+ alt="Preview"
+ className="h-4 w-4 rounded-md object-contain"
/>
- ) : (
- <div className="mb-1 h-5 w-full rounded-md bg-gray-200" />
- )}
+ {title || title === "" ? (
+ <input
+ maxLength={120}
+ className="w-full truncate border-none bg-transparent p-0 font-semibold text-[#000000E6] outline-none focus:rounded-md focus:px-1 focus:ring-1 focus:ring-inset focus:ring-gray-500"
+ value={title}
+ onChange={(e) => {
+ setData((prev) => ({
+ ...prev,
+ metaTitle: e.currentTarget.value,
+ }));
+ }}
+ />
+ ) : (
+ <div className="h-5 w-full rounded-md bg-gray-200" />
+ )}
+ </div>
{hostname ? (
<p className="text-xs text-[#00000099]">{hostname}</p>
) : (
@@ -137,29 +155,39 @@ export default function Preview({
) : (
<div className="mb-1 h-4 w-24 rounded-md bg-gray-200" />
)}
- {title || title === "" ? (
- <input
- className="truncate border-none bg-transparent p-0 font-semibold text-[#1d2129] outline-none focus:ring-0"
- value={title}
- onChange={(e) => {
- setData((prev) => ({
- ...prev,
- title: e.currentTarget.value,
- }));
- }}
+ <div className="flex flex-row items-center gap-2">
+ <img
+ src={(favicon || "/favicon.ico") as string}
+ alt="Preview"
+ className="h-4 w-4 rounded-md object-contain"
/>
- ) : (
- <div className="mb-1 h-5 w-full rounded-md bg-gray-200" />
- )}
+ {title || title === "" ? (
+ <input
+ maxLength={120}
+ name={window.crypto.randomUUID()}
+ className="w-full truncate border-none bg-transparent p-0 font-semibold text-[#000000E6] outline-none focus:rounded-md focus:px-1 focus:ring-1 focus:ring-inset focus:ring-gray-500"
+ value={title}
+ onChange={(e) => {
+ setData((prev) => ({
+ ...prev,
+ metaTitle: e.currentTarget.value,
+ }));
+ }}
+ />
+ ) : (
+ <div className="mb-1 h-5 w-full rounded-md bg-gray-200" />
+ )}
+ </div>
{description || description === "" ? (
<ReactTextareaAutosize
- className="mb-1 line-clamp-2 w-full resize-none rounded-md border-none bg-gray-200 bg-transparent p-0 text-sm text-[#606770] outline-none focus:ring-0"
+ maxLength={240}
+ className="line-clamp-2 w-full resize-none rounded-md border-none bg-gray-200 bg-transparent p-0 text-sm text-[#606770] outline-none focus:rounded-md focus:px-1 focus:ring-1 focus:ring-inset focus:ring-gray-500"
value={description}
maxRows={2}
onChange={(e) => {
setData((prev) => ({
...prev,
- description: e.currentTarget.value,
+ metaDescription: e.currentTarget.value,
}));
}}
/>
@@ -189,7 +217,7 @@ const ImagePreview = ({ image }: { image: string | null }) => {
);
} else {
return (
- <div className="flex aspect-[1200/630] h-full min-h-[250px] w-full flex-col items-center justify-center space-y-4 bg-gray-100">
+ <div className="flex aspect-[1200/630] h-full w-full flex-col items-center justify-center space-y-4 bg-gray-100">
<ImageIcon className="h-8 w-8 text-gray-400" />
<p className="text-sm text-gray-400">
Add an image to generate a preview.
diff --git a/components/view/custom-metatag.tsx b/components/view/custom-metatag.tsx
index 2d41fcf8..2418f4b7 100644
--- a/components/view/custom-metatag.tsx
+++ b/components/view/custom-metatag.tsx
@@ -1,16 +1,18 @@
import Head from "next/head";
-const CustomMetatag = ({
+const CustomMetaTag = ({
enableBranding,
title,
description,
imageUrl,
url,
+ favicon,
}: {
enableBranding: boolean;
title: string | null;
description: string | null;
imageUrl: string | null;
+ favicon: string | null;
url: string | null;
}) => {
return (
@@ -23,6 +25,22 @@ const CustomMetatag = ({
</>
)}
+ {favicon && (
+ <>
+ <link rel="icon" type="image/x-icon" href={favicon} key="favicon" />
+ {favicon.endsWith(".ico") && (
+ <link rel="icon" type="image/x-icon" href={favicon} />
+ )}
+ {favicon.endsWith(".png") && (
+ <link rel="icon" type="image/png" href={favicon} sizes="32x32" />
+ )}
+ {favicon.endsWith(".svg") && (
+ <link rel="icon" type="image/svg+xml" href={favicon} />
+ )}
+ <link rel="apple-touch-icon" href={favicon} />
+ </>
+ )}
+
{/* meta title */}
{enableBranding && title && (
<>
@@ -60,4 +78,4 @@ const CustomMetatag = ({
);
};
-export default CustomMetatag;
+export default CustomMetaTag;
diff --git a/lib/utils.ts b/lib/utils.ts
index d799a6af..3cf5447b 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -419,6 +419,33 @@ export const convertDataUrlToFile = ({
return new File([u8arr], filename, { type: mime });
};
+export const validateImageDimensions = (
+ image: string,
+ minSize: number,
+ maxSize: number,
+): Promise<boolean> => {
+ return new Promise((resolve) => {
+ const img = new Image();
+ img.src = image;
+ img.onload = () => {
+ const { width, height } = img;
+ if (
+ width >= minSize &&
+ height >= minSize &&
+ width <= maxSize &&
+ height <= maxSize
+ ) {
+ resolve(true);
+ } else {
+ resolve(false);
+ }
+ };
+ img.onerror = () => {
+ resolve(false);
+ };
+ });
+};
+
export const uploadImage = async (file: File) => {
const newBlob = await upload(file.name, file, {
access: "public",
diff --git a/lib/utils/resize-image.ts b/lib/utils/resize-image.ts
index 3322905a..30820b8d 100644
--- a/lib/utils/resize-image.ts
+++ b/lib/utils/resize-image.ts
@@ -62,8 +62,11 @@ export const resizeImage = (
targetHeight,
);
+ // Determine the original mime image type
+ const originalType = file.type || "image/png";
+
// Convert the canvas to a base64 string
- const base64Image = canvas.toDataURL("image/jpeg", opts.quality);
+ const base64Image = canvas.toDataURL(originalType, opts.quality);
resolve(base64Image);
};
img.onerror = (error) =>
diff --git a/pages/_app.tsx b/pages/_app.tsx
index c8ec03da..0a56e473 100644
--- a/pages/_app.tsx
+++ b/pages/_app.tsx
@@ -69,7 +69,7 @@ export default function App({
content="https://www.papermark.io/_static/meta-image.png"
key="tw-image"
/>
- <link rel="icon" href="/favicon.ico" />
+ <link rel="icon" href="/favicon.ico" key="favicon" />
</Head>
<SessionProvider session={session}>
<PostHogCustomProvider>
diff --git a/pages/api/file/logo-upload.ts b/pages/api/file/logo-upload.ts
index 4ae5da30..c2a532cb 100644
--- a/pages/api/file/logo-upload.ts
+++ b/pages/api/file/logo-upload.ts
@@ -27,7 +27,14 @@ export default async function handler(
}
return {
- allowedContentTypes: ["image/png", "image/jpeg"],
+ allowedContentTypes: [
+ "image/png",
+ "image/jpeg",
+ "image/jpg",
+ "image/svg+xml",
+ "image/x-icon",
+ "image/ico",
+ ],
maximumSizeInBytes: 5 * 1024 * 1024, // 5 MB
metadata: JSON.stringify({
// optional, sent to your server on upload completion
diff --git a/pages/api/links/[id]/dataroom.ts b/pages/api/links/[id]/dataroom.ts
index 1e5e3f77..2d1a3440 100644
--- a/pages/api/links/[id]/dataroom.ts
+++ b/pages/api/links/[id]/dataroom.ts
@@ -36,6 +36,7 @@ export default async function handle(
metaTitle: true,
metaDescription: true,
metaImage: true,
+ metaFavicon: true,
linkType: true,
dataroom: {
select: {
diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.ts
index 948db158..c8cbfb7c 100644
--- a/pages/api/links/[id]/index.ts
+++ b/pages/api/links/[id]/index.ts
@@ -41,6 +41,7 @@ export default async function handle(
metaTitle: true,
metaDescription: true,
metaImage: true,
+ metaFavicon: true,
enableQuestion: true,
linkType: true,
feedback: {
@@ -204,6 +205,7 @@ export default async function handle(
metaTitle: linkData.metaTitle || null,
metaDescription: linkData.metaDescription || null,
metaImage: linkData.metaImage || null,
+ metaFavicon: linkData.metaFavicon || null,
enableQuestion: linkData.enableQuestion,
feedback: {
upsert: {
diff --git a/pages/api/links/domains/[...domainSlug].ts b/pages/api/links/domains/[...domainSlug].ts
index 24876dc9..d1160191 100644
--- a/pages/api/links/domains/[...domainSlug].ts
+++ b/pages/api/links/domains/[...domainSlug].ts
@@ -51,6 +51,7 @@ export default async function handle(
metaTitle: true,
metaDescription: true,
metaImage: true,
+ metaFavicon: true,
enableQuestion: true,
linkType: true,
feedback: {
diff --git a/pages/api/links/index.ts b/pages/api/links/index.ts
index f538c620..c631b18b 100644
--- a/pages/api/links/index.ts
+++ b/pages/api/links/index.ts
@@ -156,6 +156,7 @@ export default async function handler(
metaTitle: linkData.metaTitle || null,
metaDescription: linkData.metaDescription || null,
metaImage: linkData.metaImage || null,
+ metaFavicon: linkData.metaFavicon || null,
allowList: linkData.allowList,
denyList: linkData.denyList,
audienceType: linkData.audienceType,
diff --git a/pages/api/teams/[teamId]/presets/index.ts b/pages/api/teams/[teamId]/presets/index.ts
index 1a606337..98ff3631 100644
--- a/pages/api/teams/[teamId]/presets/index.ts
+++ b/pages/api/teams/[teamId]/presets/index.ts
@@ -56,10 +56,11 @@ export default async function handle(
return res.status(200).json(presets[0]);
} else if (req.method === "POST") {
// POST /api/teams/:teamId/presets
- const { metaImage, metaTitle, metaDescription } = req.body as {
+ const { metaImage, metaTitle, metaDescription, metaFavicon } = req.body as {
metaImage?: string;
metaTitle?: string;
metaDescription?: string;
+ metaFavicon?: string;
};
// update team with new preset
@@ -68,6 +69,7 @@ export default async function handle(
metaImage,
metaTitle,
metaDescription,
+ metaFavicon,
enableCustomMetaTag: true,
name: "Default Link Metatag",
teamId: teamId,
@@ -77,10 +79,11 @@ export default async function handle(
return res.status(200).json(preset);
} else if (req.method === "PUT") {
// PUT /api/teams/:teamId/presets
- const { metaImage, metaTitle, metaDescription } = req.body as {
+ const { metaImage, metaTitle, metaDescription, metaFavicon } = req.body as {
metaImage?: string;
metaTitle?: string;
metaDescription?: string;
+ metaFavicon?: string;
};
const presets = await prisma.linkPreset.findMany({
@@ -100,6 +103,7 @@ export default async function handle(
metaImage,
metaTitle,
metaDescription,
+ metaFavicon,
},
});
diff --git a/pages/settings/presets.tsx b/pages/settings/presets.tsx
index a6c62eb1..8cbda869 100644
--- a/pages/settings/presets.tsx
+++ b/pages/settings/presets.tsx
@@ -2,7 +2,8 @@ import { ChangeEvent, useCallback, useEffect, useState } from "react";
import { useTeam } from "@/context/team-context";
import { LinkPreset } from "@prisma/client";
-import { Upload as ArrowUpTrayIcon } from "lucide-react";
+import { Upload as ArrowUpTrayIcon, PlusIcon } from "lucide-react";
+import { DropEvent } from "react-dropzone";
import { toast } from "sonner";
import useSWR, { mutate } from "swr";
import useSWRImmutable from "swr/immutable";
@@ -13,11 +14,18 @@ import Preview from "@/components/settings/og-preview";
import { SettingsHeader } from "@/components/settings/settings-header";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
+import { Label } from "@/components/ui/label";
import LoadingSpinner from "@/components/ui/loading-spinner";
import { Textarea } from "@/components/ui/textarea";
import { usePlan } from "@/lib/swr/use-billing";
-import { cn, convertDataUrlToFile, fetcher, uploadImage } from "@/lib/utils";
+import {
+ cn,
+ convertDataUrlToFile,
+ fetcher,
+ uploadImage,
+ validateImageDimensions,
+} from "@/lib/utils";
import { resizeImage } from "@/lib/utils/resize-image";
export default function Presets() {
@@ -29,6 +37,7 @@ export default function Presets() {
);
const [data, setData] = useState<{
+ metaFavicon: string | null;
metaImage: string | null;
metaTitle: string | null;
metaDescription: string | null;
@@ -36,12 +45,15 @@ export default function Presets() {
}>({
metaImage: null,
metaTitle: null,
+ metaFavicon: null,
metaDescription: null,
enableCustomMetatag: false,
});
const [fileError, setFileError] = useState<string | null>(null);
+ const [faviconFileError, setFaviconFileError] = useState<string | null>(null);
const [dragActive, setDragActive] = useState(false);
+ const [faviconDragActive, setFaviconDragActive] = useState(false);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
@@ -51,6 +63,7 @@ export default function Presets() {
metaImage: presets.metaImage,
metaTitle: presets.metaTitle,
metaDescription: presets.metaDescription,
+ metaFavicon: presets.metaFavicon,
});
}
}, [presets]);
@@ -62,7 +75,11 @@ export default function Presets() {
if (file) {
if (file.size / 1024 / 1024 > 5) {
setFileError("File size too big (max 5MB)");
- } else if (file.type !== "image/png" && file.type !== "image/jpeg") {
+ } else if (
+ file.type !== "image/png" &&
+ file.type !== "image/jpeg" &&
+ file.type !== "image/jpg"
+ ) {
setFileError("File type not supported (.png or .jpg only)");
} else {
const image = await resizeImage(file);
@@ -78,17 +95,34 @@ export default function Presets() {
const handleSavePreset = async (e: any) => {
e.preventDefault();
-
setIsLoading(true);
- let blobUrl: string | null =
+ // Upload meta image if it's a data URL
+ let blobUrlImage: string | null =
data.metaImage && data.metaImage.startsWith("data:")
? null
: data.metaImage;
if (data.metaImage && data.metaImage.startsWith("data:")) {
- const blob = convertDataUrlToFile({ dataUrl: data.metaImage });
- blobUrl = await uploadImage(blob);
- setData({ ...data, metaImage: blobUrl });
+ const blobImage = convertDataUrlToFile({ dataUrl: data.metaImage });
+ blobUrlImage = await uploadImage(blobImage);
+ setData({
+ ...data,
+ metaImage: blobUrlImage,
+ });
+ }
+
+ // Upload meta favicon if it's a data URL
+ let blobUrlFavicon: string | null =
+ data.metaFavicon && data.metaFavicon.startsWith("data:")
+ ? null
+ : data.metaFavicon;
+ if (data.metaFavicon && data.metaFavicon.startsWith("data:")) {
+ const blobFavicon = convertDataUrlToFile({ dataUrl: data.metaFavicon });
+ blobUrlFavicon = await uploadImage(blobFavicon);
+ setData({
+ ...data,
+ metaFavicon: blobUrlFavicon,
+ });
}
const res = await fetch(`/api/teams/${teamInfo?.currentTeam?.id}/presets`, {
@@ -122,6 +156,7 @@ export default function Presets() {
metaTitle: null,
metaDescription: null,
enableCustomMetatag: false,
+ metaFavicon: null,
});
setIsLoading(false);
toast.success("Preset reset successfully");
@@ -129,6 +164,48 @@ export default function Presets() {
}
};
+ const onChangeFavicon = useCallback(
+ async (e: ChangeEvent<HTMLInputElement>) => {
+ setFaviconFileError(null);
+ const file = e.target.files && e.target.files[0];
+ if (file) {
+ if (file.size / 1024 / 1024 > 1) {
+ setFaviconFileError("File size too big (max 1MB)");
+ } else if (
+ file.type !== "image/png" &&
+ file.type !== "image/x-icon" &&
+ file.type !== "image/svg+xml"
+ ) {
+ setFaviconFileError(
+ "File type not supported (.png, .ico, .svg only)",
+ );
+ } else {
+ const image = await resizeImage(file, {
+ width: 36,
+ height: 36,
+ quality: 1,
+ });
+ const isValidDimensions = await validateImageDimensions(
+ image,
+ 16,
+ 48,
+ );
+ if (!isValidDimensions) {
+ setFaviconFileError(
+ "Image dimensions must be between 16x16 and 48x48",
+ );
+ } else {
+ setData((prev) => ({
+ ...prev,
+ metaFavicon: image,
+ }));
+ }
+ }
+ }
+ },
+ [setData],
+ );
+
return (
<AppLayout>
<main className="relative mx-2 mb-10 mt-4 space-y-8 overflow-hidden px-1 sm:mx-3 md:mx-5 md:mt-5 lg:mx-7 lg:mt-8 xl:mx-10">
@@ -144,12 +221,12 @@ export default function Presets() {
</p>
</div>
</div>
- <div className="grid w-full gap-x-8 divide-x divide-border overflow-auto scrollbar-hide md:grid-cols-2 md:overflow-hidden">
- <div className="scrollbar-hide md:max-h-[95vh] md:overflow-auto">
- <div className="sticky top-0 z-10 flex h-10 items-center justify-center border-b border-border px-5 sm:h-14">
+ <div className="grid w-full divide-x divide-border overflow-auto scrollbar-hide md:grid-cols-2 md:overflow-hidden">
+ <div className="px-4 scrollbar-hide md:max-h-[95svh] md:overflow-auto">
+ <div className="sticky top-0 z-10 flex h-10 items-center justify-center border-b border-border bg-white px-5 dark:bg-gray-900 sm:h-14">
<h2 className="text-lg font-medium">Social Media Card</h2>
</div>
- <div className="relative mt-4 space-y-3 rounded-md">
+ <div className="relative mt-5 space-y-3 rounded-md px-5">
<div>
<div className="flex items-center justify-between">
<p className="block text-sm font-medium text-foreground">
@@ -160,8 +237,8 @@ export default function Presets() {
) : null}
</div>
<label
- htmlFor="image"
- className="group relative mt-1 flex aspect-[1200/630] h-full min-h-[250px] cursor-pointer flex-col items-center justify-center rounded-md border border-input bg-white shadow-sm transition-all hover:border-muted-foreground hover:bg-gray-50 hover:ring-muted-foreground dark:bg-gray-800 hover:dark:bg-transparent"
+ htmlFor="mainImage"
+ className="group relative mt-1 flex aspect-[1200/630] h-full cursor-pointer flex-col items-center justify-center rounded-md border border-input bg-white shadow-sm transition-all hover:border-muted-foreground hover:bg-gray-50 hover:ring-muted-foreground dark:bg-gray-800 hover:dark:bg-transparent"
>
{false && (
<div className="absolute z-[5] flex h-full w-full items-center justify-center rounded-md bg-white">
@@ -197,7 +274,8 @@ export default function Presets() {
setFileError("File size too big (max 5MB)");
} else if (
file.type !== "image/png" &&
- file.type !== "image/jpeg"
+ file.type !== "image/jpeg" &&
+ file.type !== "image/jpg"
) {
setFileError(
"File type not supported (.png or .jpg only)",
@@ -240,21 +318,142 @@ export default function Presets() {
<img
src={data.metaImage}
alt="Preview"
- className="aspect-[1200/630] h-full w-full rounded-md object-cover"
+ className="h-full w-full rounded-md object-cover"
/>
)}
</label>
- <div className="mt-1 flex rounded-md shadow-sm">
+ <div className="mt-1 hidden rounded-md shadow-sm">
<input
- id="image"
+ id="mainImage"
name="image"
type="file"
- accept="image/*"
+ accept="image/png,image/jpeg,image/jpg"
className="sr-only"
onChange={onChangePicture}
/>
</div>
</div>
+ <div className="flex flex-col gap-2">
+ <div className="flex items-center justify-between">
+ <Label htmlFor="faviconIcon">
+ Favicon Icon{" "}
+ <span className="text-sm italic text-muted-foreground">
+ (max 1 MB)
+ </span>
+ </Label>
+ {faviconFileError ? (
+ <p className="text-sm text-red-500">{faviconFileError}</p>
+ ) : null}
+ </div>
+ <label
+ htmlFor="faviconIcon"
+ className="group relative mt-1 flex h-[4rem] w-[12rem] cursor-pointer flex-col items-center justify-center rounded-md border border-gray-300 bg-white shadow-sm transition-all hover:bg-gray-50"
+ style={{
+ backgroundImage:
+ "linear-gradient(45deg, #ccc 25%, transparent 25%), linear-gradient(135deg, #ccc 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #ccc 75%), linear-gradient(135deg, transparent 75%, #ccc 75%)",
+ backgroundSize: "20px 20px",
+ backgroundPosition: "0 0, 10px 0, 10px -10px, 0px 10px",
+ }}
+ >
+ {false && (
+ <div className="absolute z-[5] flex h-full w-full items-center justify-center rounded-md bg-white">
+ <LoadingSpinner />
+ </div>
+ )}
+ <div
+ className="absolute z-[5] h-full w-full rounded-md"
+ onDragOver={(e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setFaviconDragActive(true);
+ }}
+ onDragEnter={(e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setFaviconDragActive(true);
+ }}
+ onDragLeave={(e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setFaviconDragActive(false);
+ }}
+ onDrop={async (e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setFaviconDragActive(false);
+ setFaviconFileError(null);
+ const file =
+ e.dataTransfer.files && e.dataTransfer.files[0];
+ if (file) {
+ if (file.size / 1024 / 1024 > 1) {
+ setFaviconFileError("File size too big (max 1MB)");
+ } else if (
+ file.type !== "image/png" &&
+ file.type !== "image/x-icon" &&
+ file.type !== "image/svg+xml"
+ ) {
+ setFaviconFileError(
+ "File type not supported (.png, .ico, .svg only)",
+ );
+ } else {
+ const image = await resizeImage(file, {
+ width: 36,
+ height: 36,
+ quality: 1,
+ });
+ const isValidDimensions =
+ await validateImageDimensions(image, 16, 48);
+ if (!isValidDimensions) {
+ setFaviconFileError(
+ "Image dimensions must be between 16x16 and 48x48",
+ );
+ } else {
+ setData((prev) => ({
+ ...prev,
+ metaFavicon: image,
+ }));
+ }
+ }
+ }
+ }}
+ />
+ <div
+ className={`${
+ faviconDragActive
+ ? "cursor-copy border-2 border-black bg-gray-50 opacity-100"
+ : ""
+ } absolute z-[3] flex h-full w-full flex-col items-center justify-center rounded-md bg-white transition-all ${
+ data.metaFavicon
+ ? "opacity-0 group-hover:opacity-100"
+ : "group-hover:bg-gray-50"
+ }`}
+ >
+ <PlusIcon
+ className={`${
+ faviconDragActive ? "scale-110" : "scale-100"
+ } h-7 w-7 text-gray-500 transition-all duration-75 group-hover:scale-110 group-active:scale-95`}
+ />
+ <span className="sr-only">OG image upload</span>
+ </div>
+ {data.metaFavicon && (
+ <img
+ src={data.metaFavicon}
+ alt="Preview"
+ className="h-full w-full rounded-md object-contain"
+ />
+ )}
+ </label>
+ <div className="mt-1 hidden rounded-md shadow-sm">
+ <input
+ id="faviconIcon"
+ name="favicon"
+ type="file"
+ accept="image/png,image/x-icon,image/svg+xml"
+ className="sr-only"
+ onChange={onChangeFavicon}
+ />
+ </div>
+ </div>
<div>
<div className="flex items-center justify-between">
@@ -347,7 +546,7 @@ export default function Presets() {
</div>
</div>
</div>
- <div className="scrollbar-hide md:max-h-[95vh] md:overflow-auto">
+ <div className="px-4 scrollbar-hide md:max-h-[95svh] md:overflow-auto">
<Preview data={data} setData={setData} />
</div>
</div>
diff --git a/pages/view/[linkId]/index.tsx b/pages/view/[linkId]/index.tsx
index be2f90f4..59537827 100644
--- a/pages/view/[linkId]/index.tsx
+++ b/pages/view/[linkId]/index.tsx
@@ -11,7 +11,7 @@ import { ExtendedRecordMap } from "notion-types";
import { parsePageId } from "notion-utils";
import LoadingSpinner from "@/components/ui/loading-spinner";
-import CustomMetatag from "@/components/view/custom-metatag";
+import CustomMetaTag from "@/components/view/custom-metatag";
import DataroomView from "@/components/view/dataroom/dataroom-view";
import DocumentView from "@/components/view/document-view";
@@ -94,6 +94,7 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {
metaTitle: link.metaTitle,
metaDescription: link.metaDescription,
metaImage: link.metaImage,
+ metaFavicon: link.metaFavicon ?? "/favicon.ico",
metaUrl: `https://www.papermark.io/view/${linkId}`,
},
showPoweredByBanner: link.showBanner || teamPlan === "free",
@@ -147,6 +148,7 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {
metaTitle: link.metaTitle,
metaDescription: link.metaDescription,
metaImage: link.metaImage,
+ metaFavicon: link.metaFavicon ?? "/favicon.ico",
metaUrl: `https://www.papermark.io/view/${linkId}`,
},
showPoweredByBanner: false,
@@ -192,6 +194,7 @@ export default function ViewPage({
metaDescription: string | null;
metaImage: string | null;
metaUrl: string | null;
+ metaFavicon: string | null;
};
showPoweredByBanner: boolean;
showAccountCreationSlide: boolean;
@@ -256,7 +259,8 @@ export default function ViewPage({
if (!linkData || status === "loading" || router.isFallback) {
return (
<>
- <CustomMetatag
+ <CustomMetaTag
+ favicon={meta.metaFavicon}
enableBranding={meta.enableCustomMetatag ?? false}
title={
meta.metaTitle ??
@@ -301,7 +305,8 @@ export default function ViewPage({
return (
<>
- <CustomMetatag
+ <CustomMetaTag
+ favicon={meta.metaFavicon}
enableBranding={meta.enableCustomMetatag ?? false}
title={
meta.metaTitle ??
@@ -337,7 +342,8 @@ export default function ViewPage({
if (!link || status === "loading" || router.isFallback) {
return (
<>
- <CustomMetatag
+ <CustomMetaTag
+ favicon={meta.metaFavicon}
enableBranding={meta.enableCustomMetatag ?? false}
title={
meta.metaTitle ??
@@ -382,7 +388,8 @@ export default function ViewPage({
return (
<>
- <CustomMetatag
+ <CustomMetaTag
+ favicon={meta.metaFavicon}
enableBranding={meta.enableCustomMetatag ?? false}
title={
meta.metaTitle ??
diff --git a/pages/view/domains/[domain]/[slug]/index.tsx b/pages/view/domains/[domain]/[slug]/index.tsx
index 9f20fb0d..97463ece 100644
--- a/pages/view/domains/[domain]/[slug]/index.tsx
+++ b/pages/view/domains/[domain]/[slug]/index.tsx
@@ -11,7 +11,7 @@ import { ExtendedRecordMap } from "notion-types";
import { parsePageId } from "notion-utils";
import LoadingSpinner from "@/components/ui/loading-spinner";
-import CustomMetatag from "@/components/view/custom-metatag";
+import CustomMetaTag from "@/components/view/custom-metatag";
import DataroomView from "@/components/view/dataroom/dataroom-view";
import DocumentView from "@/components/view/document-view";
@@ -151,6 +151,7 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {
metaTitle: link.metaTitle,
metaDescription: link.metaDescription,
metaImage: link.metaImage,
+ metaFavicon: link.metaFavicon || "/favicon.ico",
metaUrl: `https://${domain}/${slug}` || null,
},
showPoweredByBanner: false,
@@ -192,6 +193,7 @@ export default function ViewPage({
meta: {
enableCustomMetatag: boolean;
metaTitle: string | null;
+ metaFavicon: string | null;
metaDescription: string | null;
metaImage: string | null;
metaUrl: string | null;
@@ -258,7 +260,8 @@ export default function ViewPage({
if (!link || status === "loading") {
return (
<>
- <CustomMetatag
+ <CustomMetaTag
+ favicon={meta.metaFavicon}
enableBranding={meta.enableCustomMetatag ?? false}
title={
meta.metaTitle ??
@@ -302,7 +305,8 @@ export default function ViewPage({
return (
<>
- <CustomMetatag
+ <CustomMetaTag
+ favicon={meta.metaFavicon}
enableBranding={meta.enableCustomMetatag ?? false}
title={
meta.metaTitle ??
@@ -337,7 +341,8 @@ export default function ViewPage({
if (!link || status === "loading" || router.isFallback) {
return (
<>
- <CustomMetatag
+ <CustomMetaTag
+ favicon={meta.metaFavicon}
enableBranding={meta.enableCustomMetatag ?? false}
title={
meta.metaTitle ??
@@ -382,7 +387,8 @@ export default function ViewPage({
return (
<>
- <CustomMetatag
+ <CustomMetaTag
+ favicon={meta.metaFavicon}
enableBranding={meta.enableCustomMetatag ?? false}
title={
meta.metaTitle ??
diff --git a/prisma/migrations/20241004024010_add_favicon_column/migration.sql b/prisma/migrations/20241004024010_add_favicon_column/migration.sql
new file mode 100644
index 00000000..ba9a54e3
--- /dev/null
+++ b/prisma/migrations/20241004024010_add_favicon_column/migration.sql
@@ -0,0 +1,5 @@
+-- AlterTable
+ALTER TABLE "Link" ADD COLUMN "metaFavicon" TEXT;
+
+-- AlterTable
+ALTER TABLE "LinkPreset" ADD COLUMN "metaFavicon" TEXT;
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index dd8ced26..eaeedd02 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -258,6 +258,7 @@ model Link {
metaTitle String? // This will be the meta title of the link
metaDescription String? // This will be the meta description of the link
metaImage String? // This will be the meta image of the link
+ metaFavicon String? // This will be the meta favicon of the link
enableCustomMetatag Boolean? @default(false) // Optional give user a option to enable the custom metatag
@@unique([domainSlug, slug])
@@ -273,7 +274,8 @@ model LinkPreset {
enableCustomMetaTag Boolean? @default(false) // Optional give user a option to enable the custom metatag
metaTitle String? // This will be the meta title of the link
metaDescription String? // This will be the meta description of the link
- metaImage String? // This will be the meta image of the link
+ metaImage String? // This will be the meta image of the link
+ metaFavicon String? // This will be the meta favicon of the link
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt