The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add syncing feature
Steven Tey5de68d216d64
diff --git a/apps/web/app/api/links/sync/route.ts b/apps/web/app/api/links/sync/route.tsnew file mode 100644index 0000000000..9aff62462b--- /dev/null+++ b/apps/web/app/api/links/sync/route.ts@@ -0,0 +1,54 @@+import { withAuth } from "@/lib/auth";+import prisma from "@/lib/prisma";+import { NextResponse } from "next/server";+import { LinkProps, SimpleLinkProps } from "@/lib/types";++// POST /api/links/sync – sync user's publicly created links to their accounts+export const POST = withAuth(+ async ({ req, session }) => {+ let links: SimpleLinkProps[] = [];+ try {+ links = await req.json();+ if (!Array.isArray(links)) {+ throw new Error("Invalid request body.");+ }+ } catch (e) {+ return new Response("Invalid request body.", { status: 400 });+ }++ const unclaimedLinks = (await Promise.all(+ links.map(async (link) => {+ return await prisma.link.findUnique({+ where: {+ domain_key: {+ domain: link.domain,+ key: link.key,+ },+ },+ });+ }),+ ).then((links) =>+ links.filter((link) => link && !link.userId),+ )) as LinkProps[];++ if (unclaimedLinks.length === 0) {+ return new Response("No links created.", { status: 400 });+ }++ const response = await prisma.link.updateMany({+ where: {+ id: {+ in: unclaimedLinks.map((link) => link.id),+ },+ },+ data: {+ userId: session.user.id,+ },+ });++ return NextResponse.json(response);+ },+ {+ requiredPlan: ["pro", "enterprise"],+ },+);diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex 661794ab12..3aab7753bb 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -2,7 +2,7 @@ import { DirectorySyncProviders } from "@boxyhq/saml-jackson";export { type Link as LinkProps } from "@prisma/client";export interface SimpleLinkProps {- domain?: string;+ domain: string;key: string;url: string;}diff --git a/apps/web/ui/modals/provider.tsx b/apps/web/ui/modals/provider.tsxindex 294712a123..c4f7159eca 100644--- a/apps/web/ui/modals/provider.tsx+++ b/apps/web/ui/modals/provider.tsx@@ -19,6 +19,9 @@ import {useState,} from "react";import { mutate } from "swr";+import { useCookies } from "@dub/ui";+import { SimpleLinkProps } from "@/lib/types";+import { toast } from "sonner";export const ModalContext = createContext<{setShowAddProjectModal: Dispatch<SetStateAction<boolean>>;@@ -56,6 +59,30 @@ export default function ModalProvider({ children }: { children: ReactNode }) {const params = useParams() as { slug?: string };const { slug } = params;+ const [hashes, setHashes] = useCookies<SimpleLinkProps[]>("hashes__dub", []);++ useEffect(() => {+ if (hashes.length > 0) {+ fetch("/api/links/sync", {+ method: "POST",+ headers: {+ "Content-Type": "application/json",+ },+ body: JSON.stringify(hashes),+ }).then(async (res) => {+ if (res.status === 200) {+ await mutate(+ (key) => typeof key === "string" && key.startsWith("/api/links"),+ undefined,+ { revalidate: true },+ );+ toast.success("Links imported successfully!");+ }+ setHashes([]);+ });+ }+ }, [hashes]);+// special link polling setup to poll links as they're being created (for bitly import)const [pollLinks, setPollLinks] = useState(false);useEffect(() => {diff --git a/packages/ui/package.json b/packages/ui/package.jsonindex 5eac2979ac..14ed951dce 100644--- a/packages/ui/package.json+++ b/packages/ui/package.json@@ -1,7 +1,7 @@{"name": "@dub/ui","description": "UI components for Dub.co",- "version": "0.0.21",+ "version": "0.0.25","sideEffects": false,"main": "./dist/index.js","module": "./dist/index.mjs",@@ -23,6 +23,7 @@"devDependencies": {"@dub/tailwind-config": "workspace:*","@dub/utils": "workspace:*",+ "@types/js-cookie": "^3.0.6","@types/react": "^18.2.5","autoprefixer": "^10.4.16","next": "13.5.4",@@ -41,6 +42,7 @@"@radix-ui/react-switch": "^1.0.1","@radix-ui/react-tooltip": "^1.0.7","class-variance-authority": "^0.7.0",+ "js-cookie": "^3.0.5","lucide-react": "^0.284.0","sonner": "^0.5.0","swr": "^2.1.5",diff --git a/packages/ui/src/footer.tsx b/packages/ui/src/footer.tsxindex fb17c9bf28..cf0f7e0fb2 100644--- a/packages/ui/src/footer.tsx+++ b/packages/ui/src/footer.tsx@@ -40,8 +40,8 @@ export function Footer() {return (<footer className="z-10 border-t border-gray-200 bg-white/50 py-8 backdrop-blur-lg"><MaxWidthWrapper className="pt-10">- <div className="xl:grid xl:grid-cols-5 xl:gap-8">- <div className="space-y-8 xl:col-span-2">+ <div className="xl:grid xl:grid-cols-3 xl:gap-8">+ <div className="space-y-8"><Linkhref={createHref("/")}{...(domain !== "dub.co" && {@@ -92,7 +92,7 @@ export function Footer() {</a></div></div>- <div className="mt-16 grid grid-cols-2 gap-8 xl:col-span-3 xl:mt-0">+ <div className="mt-16 grid grid-cols-2 gap-8 xl:col-span-2 xl:mt-0"><div className="md:grid md:grid-cols-2 md:gap-8"><div><h3 className="text-sm font-semibold text-gray-900">diff --git a/packages/ui/src/hooks/index.ts b/packages/ui/src/hooks/index.tsindex 6d02426ad4..02809d86b7 100644--- a/packages/ui/src/hooks/index.ts+++ b/packages/ui/src/hooks/index.ts@@ -1,3 +1,4 @@+export { default as useCookies } from "./use-cookies";export { default as useCurrentAnchor } from "./use-current-anchor";export { default as useIntersectionObserver } from "./use-intersection-observer";export { default as useLocalStorage } from "./use-local-storage";diff --git a/packages/ui/src/hooks/use-cookies.ts b/packages/ui/src/hooks/use-cookies.tsnew file mode 100644index 0000000000..b8d8f8acd0--- /dev/null+++ b/packages/ui/src/hooks/use-cookies.ts@@ -0,0 +1,41 @@+import Cookies from "js-cookie";+import { useEffect, useState } from "react";++export default function useCookies<T>(+ key: string,+ initialValue: T,+ opts?: Cookies.CookieAttributes,+): [T, (value: T) => void] {+ const [storedValue, setStoredValue] = useState<T>(() => {+ // Retrieve from Cookies+ const item = Cookies.get(key);+ return item ? JSON.parse(item) : initialValue;+ });++ useEffect(() => {+ // Update state if the cookie changes+ const handleStorageChange = () => {+ const item = Cookies.get(key);+ if (item) {+ setStoredValue(JSON.parse(item));+ }+ };++ // Add listener for storage changes+ window.addEventListener("storage", handleStorageChange);++ // Cleanup+ return () => {+ window.removeEventListener("storage", handleStorageChange);+ };+ }, [key]);++ const setValue = (value: T) => {+ // Save state+ setStoredValue(value);+ // Save to Cookies+ Cookies.set(key, JSON.stringify(value));+ };++ return [storedValue, setValue];+}diff --git a/packages/utils/package.json b/packages/utils/package.jsonindex 22184ce6fd..67868b86dd 100644--- a/packages/utils/package.json+++ b/packages/utils/package.json@@ -1,7 +1,7 @@{"name": "@dub/utils","description": "Utility functions and constants for Dub.co",- "version": "0.0.11",+ "version": "0.0.13","sideEffects": false,"main": "./dist/index.js","module": "./dist/index.mjs",diff --git a/packages/utils/src/constants/index.ts b/packages/utils/src/constants/index.tsindex 8a917e39c6..792635f49b 100644--- a/packages/utils/src/constants/index.ts+++ b/packages/utils/src/constants/index.ts@@ -141,6 +141,7 @@ export const DUB_DOMAINS = [];export const ALL_TOOLS = [+ { name: "ChatGPT Link Shortener", slug: "chatgpt-link-shortener" },{ name: "Metatags API", slug: "metatags" },{ name: "QR Code Generator", slug: "qr-code" },{ name: "Link Inspector", slug: "inspector" },diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex cc93009dd0..a4d23426a8 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -632,7 +632,7 @@ importers:version: 1.0.1([email protected])([email protected])'@radix-ui/react-dialog':specifier: 1.0.5- version: 1.0.5(@types/[email protected])(@types/[email protected])([email protected])([email protected])'@radix-ui/react-navigation-menu':specifier: ^1.1.2version: 1.1.2([email protected])([email protected])@@ -651,6 +651,9 @@ importers:class-variance-authority:specifier: ^0.7.0version: 0.7.0+ js-cookie:+ specifier: ^3.0.5+ version: 3.0.5lucide-react:specifier: ^0.284.0version: 0.284.0([email protected])@@ -673,6 +676,9 @@ importers:'@dub/utils':specifier: workspace:*version: link:../utils+ '@types/js-cookie':+ specifier: ^3.0.6+ version: 3.0.6'@types/react':specifier: ^18.2.5version: 18.2.14@@ -739,7 +745,7 @@ importers:version: link:../tsconfigtsup:specifier: ^6.1.3- version: 6.1.3([email protected])([email protected])+ version: 6.1.3([email protected])typescript:specifier: ^5.1.6version: 5.1.6@@ -5394,11 +5400,11 @@ packages:'@radix-ui/primitive': 1.0.1'@radix-ui/react-compose-refs': 1.0.1(@types/[email protected])([email protected])'@radix-ui/react-context': 1.0.1(@types/[email protected])([email protected])- '@radix-ui/react-dismissable-layer': 1.0.5(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-dismissable-layer': 1.0.5(@types/[email protected])([email protected])([email protected])'@radix-ui/react-focus-guards': 1.0.1(@types/[email protected])([email protected])- '@radix-ui/react-focus-scope': 1.0.4(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-focus-scope': 1.0.4(@types/[email protected])([email protected])([email protected])'@radix-ui/react-id': 1.0.1(@types/[email protected])([email protected])- '@radix-ui/react-portal': 1.0.4(@types/[email protected])(@types/[email protected])([email protected])([email protected])'@radix-ui/react-presence': 1.0.1(@types/[email protected])(@types/[email protected])([email protected])([email protected])'@radix-ui/react-primitive': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])'@radix-ui/react-slot': 1.0.2(@types/[email protected])([email protected])@@ -5517,6 +5523,30 @@ packages:react-dom: 18.2.0([email protected])dev: false+ resolution: {integrity: sha512-aJeDjQhywg9LBu2t/At58hCvr7pEm0o2Ke1x33B+MhjNmmZ17sy4KImo0KPLgsnc/zN7GPdce8Cnn0SWvwZO7g==}+ peerDependencies:+ '@types/react': '*'+ '@types/react-dom': '*'+ react: ^16.8 || ^17.0 || ^18.0+ react-dom: ^16.8 || ^17.0 || ^18.0+ peerDependenciesMeta:+ '@types/react':+ optional: true+ '@types/react-dom':+ optional: true+ dependencies:+ '@babel/runtime': 7.23.2+ '@radix-ui/primitive': 1.0.1+ '@radix-ui/react-compose-refs': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-primitive': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-use-callback-ref': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-use-escape-keydown': 1.0.3(@types/[email protected])([email protected])+ '@types/react': 18.2.14+ react: 18.2.0+ react-dom: 18.2.0([email protected])+ dev: false+/@radix-ui/[email protected]([email protected]):resolution: {integrity: sha512-UagjDk4ijOAnGu4WMUPj9ahi7/zJJqNZ9ZAiGPp7waUWJO0O1aWXi/udPphI0IUjvrhBsZJGSN66dR2dsueLWQ==}peerDependencies:@@ -5628,6 +5658,28 @@ packages:react-dom: 18.2.0([email protected])dev: false+ resolution: {integrity: sha512-sL04Mgvf+FmyvZeYfNu1EPAaaxD+aw7cYeIB9L9Fvq8+urhltTRaEo5ysKOpHuKPclsZcSUMKlN05x4u+CINpA==}+ peerDependencies:+ '@types/react': '*'+ '@types/react-dom': '*'+ react: ^16.8 || ^17.0 || ^18.0+ react-dom: ^16.8 || ^17.0 || ^18.0+ peerDependenciesMeta:+ '@types/react':+ optional: true+ '@types/react-dom':+ optional: true+ dependencies:+ '@babel/runtime': 7.23.2+ '@radix-ui/react-compose-refs': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-primitive': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-use-callback-ref': 1.0.1(@types/[email protected])([email protected])+ '@types/react': 18.2.14+ react: 18.2.0+ react-dom: 18.2.0([email protected])+ dev: false+/@radix-ui/[email protected]([email protected]):resolution: {integrity: sha512-Q6iAB/U7Tq3NTolBBQbHTgclPmGWE3OlktGGqrClPozSw4vkQ1DfQAOtzgRPecKsMdJINE05iaoDUG8tRzCBjw==}peerDependencies:@@ -5738,12 +5790,12 @@ packages:'@radix-ui/primitive': 1.0.1'@radix-ui/react-compose-refs': 1.0.1(@types/[email protected])([email protected])'@radix-ui/react-context': 1.0.1(@types/[email protected])([email protected])- '@radix-ui/react-dismissable-layer': 1.0.5(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-dismissable-layer': 1.0.5(@types/[email protected])([email protected])([email protected])'@radix-ui/react-focus-guards': 1.0.1(@types/[email protected])([email protected])- '@radix-ui/react-focus-scope': 1.0.4(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-focus-scope': 1.0.4(@types/[email protected])([email protected])([email protected])'@radix-ui/react-id': 1.0.1(@types/[email protected])([email protected])- '@radix-ui/react-portal': 1.0.4(@types/[email protected])(@types/[email protected])([email protected])([email protected])'@radix-ui/react-presence': 1.0.1(@types/[email protected])(@types/[email protected])([email protected])([email protected])'@radix-ui/react-primitive': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])'@radix-ui/react-slot': 1.0.2(@types/[email protected])([email protected])@@ -5872,6 +5924,26 @@ packages:react-dom: 18.2.0([email protected])dev: false+ resolution: {integrity: sha512-Qki+C/EuGUVCQTOTD5vzJzJuMUlewbzuKyUy+/iHM2uwGiru9gZeBJtHAPKAEkB5KWGi9mP/CHKcY0wt1aW45Q==}+ peerDependencies:+ '@types/react': '*'+ '@types/react-dom': '*'+ react: ^16.8 || ^17.0 || ^18.0+ react-dom: ^16.8 || ^17.0 || ^18.0+ peerDependenciesMeta:+ '@types/react':+ optional: true+ '@types/react-dom':+ optional: true+ dependencies:+ '@babel/runtime': 7.23.2+ '@radix-ui/react-primitive': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@types/react': 18.2.14+ react: 18.2.0+ react-dom: 18.2.0([email protected])+ dev: false+resolution: {integrity: sha512-A+6XEvN01NfVWiKu38ybawfHsBjWum42MRPnEuqPsBZ4eV7e/7K321B5VgYMPv3Xx5An6o1/l9ZuDBgmcmWK3w==}peerDependencies:@@ -6169,10 +6241,10 @@ packages:'@radix-ui/primitive': 1.0.1'@radix-ui/react-compose-refs': 1.0.1(@types/[email protected])([email protected])'@radix-ui/react-context': 1.0.1(@types/[email protected])([email protected])- '@radix-ui/react-dismissable-layer': 1.0.5(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-dismissable-layer': 1.0.5(@types/[email protected])([email protected])([email protected])'@radix-ui/react-id': 1.0.1(@types/[email protected])([email protected])- '@radix-ui/react-portal': 1.0.4(@types/[email protected])(@types/[email protected])([email protected])([email protected])'@radix-ui/react-presence': 1.0.1(@types/[email protected])(@types/[email protected])([email protected])([email protected])'@radix-ui/react-primitive': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])'@radix-ui/react-slot': 1.0.2(@types/[email protected])([email protected])@@ -7743,6 +7815,10 @@ packages:'@types/istanbul-lib-report': 3.0.1dev: false+ /@types/[email protected]:+ resolution: {integrity: sha512-wkw9yd1kEXOPnvEeEV1Go1MmxtBJL0RR79aOTAApecWFVu7w0NNXNqhcWgvw2YgZDYadliXkl14pa3WXw5jlCQ==}+ dev: true+/@types/[email protected]:resolution: {integrity: sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==}dev: true@@ -16399,6 +16475,42 @@ packages:- ts-nodedev: true+ resolution: {integrity: sha512-eRpBnbfpDFng+EJNTQ90N7QAf4HAGGC7O3buHIjroKWK7D1ibk9/YnR/3cS8HsMU5T+6Oi+cnF+yU5WmCnB//Q==}+ engines: {node: '>=14'}+ hasBin: true+ peerDependencies:+ '@swc/core': ^1+ postcss: ^8.4.12+ typescript: ^4.1.0+ peerDependenciesMeta:+ '@swc/core':+ optional: true+ postcss:+ optional: true+ typescript:+ optional: true+ dependencies:+ bundle-require: 3.1.2([email protected])+ cac: 6.7.14+ chokidar: 3.5.3+ debug: 4.3.4+ esbuild: 0.14.54+ execa: 5.1.1+ globby: 11.1.0+ joycon: 3.1.1+ postcss-load-config: 3.1.4([email protected])+ resolve-from: 5.0.0+ rollup: 2.79.1+ source-map: 0.8.0-beta.0+ sucrase: 3.34.0+ tree-kill: 1.2.2+ typescript: 5.1.6+ transitivePeerDependencies:+ - supports-color+ - ts-node+ dev: true+resolution: {integrity: sha512-mHKK3iUXL+3UF6xL5k0PEhKRUBKPBCv/+RkEOpjRWxxx27KKRBmmA60A9pgOUvMi8GKhRMPEmjBRPzs2W7O1OA==}engines: {node: '>= 6'}