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.

Improve importer

Steven Tey4c3b4ee1bd8e
diff --git a/apps/web/app/api/cron/import/bitly/route.ts b/apps/web/app/api/cron/import/bitly/route.ts
index d2d27f485e..c8ade51b0c 100644
--- a/apps/web/app/api/cron/import/bitly/route.ts
+++ b/apps/web/app/api/cron/import/bitly/route.ts
@@ -16,7 +16,7 @@ export async function POST(req: Request) {
await verifyQstashSignature({ req, rawBody });
const body = JSON.parse(rawBody);
- const { workspaceId, bitlyGroup, importTags } = body;
+ const { workspaceId, bitlyGroup, importTags, folderId } = body;
try {
const bitlyApiKey = await redis.get(`import:bitly:${workspaceId}`);
diff --git a/apps/web/app/api/cron/import/bitly/utils.ts b/apps/web/app/api/cron/import/bitly/utils.ts
index dffdd3650d..8245c846b5 100644
--- a/apps/web/app/api/cron/import/bitly/utils.ts
+++ b/apps/web/app/api/cron/import/bitly/utils.ts
@@ -4,7 +4,7 @@ import { redis } from "@/lib/upstash";
import { sendEmail } from "@dub/email";
import { LinksImported } from "@dub/email/templates/links-imported";
import { prisma } from "@dub/prisma";
-import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
+import { APP_DOMAIN_WITH_NGROK, linkConstructorSimple } from "@dub/utils";
// Note: rate limit for /groups/{group_guid}/bitlinks is 1500 per hour or 150 per minute
export const importLinksFromBitly = async ({
@@ -12,6 +12,7 @@ export const importLinksFromBitly = async ({
userId,
bitlyGroup,
domains,
+ folderId,
tagsToId,
bitlyApiKey,
searchAfter = null,
@@ -21,6 +22,7 @@ export const importLinksFromBitly = async ({
userId: string;
bitlyGroup: string;
domains: string[];
+ folderId: string;
tagsToId?: Record<string, string>;
bitlyApiKey: string;
searchAfter?: string | null;
@@ -67,10 +69,15 @@ export const importLinksFromBitly = async ({
domain,
key,
url,
+ shortLink: linkConstructorSimple({
+ domain,
+ key,
+ }),
title,
archived,
createdAt,
tagIds,
+ folderId,
};
return [
@@ -93,6 +100,10 @@ export const importLinksFromBitly = async ({
...linkDetails,
domain: customDomain,
key: customKey,
+ shortLink: linkConstructorSimple({
+ domain: customDomain,
+ key: customKey,
+ }),
};
}) ?? []),
];
@@ -102,25 +113,22 @@ export const importLinksFromBitly = async ({
// check if links are already in the database
const alreadyCreatedLinks = await prisma.link.findMany({
where: {
- domain: {
- in: domains,
- },
- key: {
- in: importedLinks.map((link) => link.key),
+ shortLink: {
+ in: importedLinks.map((link) => link.shortLink),
},
},
select: {
- domain: true,
- key: true,
+ shortLink: true,
},
});
// filter out links that are already in the database
const linksToCreate = importedLinks.filter(
- (link) =>
- !alreadyCreatedLinks.some(
- (l) => l.domain === link.domain && l.key === link.key,
- ),
+ (link) => !alreadyCreatedLinks.some((l) => l.shortLink === link.shortLink),
+ );
+
+ console.log(
+ `Found ${alreadyCreatedLinks.length} links that have already been imported, skipping them and creating ${linksToCreate.length} new links...`,
);
// bulk create links
@@ -217,6 +225,7 @@ export const importLinksFromBitly = async ({
userId,
bitlyGroup,
domains,
+ folderId,
importTags: tagsToId ? true : false,
searchAfter: nextSearchAfter,
count,
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/import/bitly/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/import/bitly/route.ts
index f9935dd9fa..92bcc883c4 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/import/bitly/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/import/bitly/route.ts
@@ -59,7 +59,7 @@ export const GET = withWorkspace(async ({ workspace }) => {
// POST /api/workspaces/[idOrSlug]/import/bitly - create job to import links from bitly
export const POST = withWorkspace(async ({ req, workspace, session }) => {
- const { selectedDomains, selectedGroupTags } = await req.json();
+ const { selectedDomains, selectedGroupTags, folderId } = await req.json();
const domains = await prisma.domain.findMany({
where: { projectId: workspace.id },
@@ -92,6 +92,7 @@ export const POST = withWorkspace(async ({ req, workspace, session }) => {
url: "",
userId: session?.user?.id,
projectId: workspace.id,
+ folderId,
})),
});
}
@@ -124,6 +125,7 @@ export const POST = withWorkspace(async ({ req, workspace, session }) => {
bitlyGroup,
domains,
importTags,
+ folderId,
},
}),
),
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/page-client.tsx
index dfd8d4953d..ed42a1dab5 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/page-client.tsx
@@ -24,6 +24,7 @@ import {
Popover,
Tooltip,
TooltipContent,
+ useRouterStuff,
} from "@dub/ui";
import { Download, Globe, TableIcon, Tag } from "@dub/ui/icons";
import { useSession } from "next-auth/react";
@@ -199,7 +200,7 @@ function WorkspaceLinks() {
}
const MoreLinkOptions = () => {
- const router = useRouter();
+ const { queryParams } = useRouterStuff();
const { slug } = useWorkspace();
const [openPopover, setOpenPopover] = useState(false);
const [_state, setState] = useState<"default" | "import">("default");
@@ -222,7 +223,11 @@ const MoreLinkOptions = () => {
<ImportOption
onClick={() => {
setOpenPopover(false);
- router.push(`/${slug}?import=bitly`);
+ queryParams({
+ set: {
+ import: "bitly",
+ },
+ });
}}
setOpenPopover={setOpenPopover}
>
@@ -240,7 +245,11 @@ const MoreLinkOptions = () => {
<ImportOption
onClick={() => {
setOpenPopover(false);
- router.push(`/${slug}?import=rebrandly`);
+ queryParams({
+ set: {
+ import: "rebrandly",
+ },
+ });
}}
setOpenPopover={setOpenPopover}
>
@@ -258,7 +267,11 @@ const MoreLinkOptions = () => {
<ImportOption
onClick={() => {
setOpenPopover(false);
- router.push(`/${slug}?import=short`);
+ queryParams({
+ set: {
+ import: "short",
+ },
+ });
}}
setOpenPopover={setOpenPopover}
>
@@ -276,7 +289,11 @@ const MoreLinkOptions = () => {
<ImportOption
onClick={() => {
setOpenPopover(false);
- router.push(`/${slug}?import=csv`);
+ queryParams({
+ set: {
+ import: "csv",
+ },
+ });
}}
setOpenPopover={setOpenPopover}
>
diff --git a/apps/web/ui/modals/import-bitly-modal.tsx b/apps/web/ui/modals/import-bitly-modal.tsx
index 67ecbb999a..0a50bf6268 100644
--- a/apps/web/ui/modals/import-bitly-modal.tsx
+++ b/apps/web/ui/modals/import-bitly-modal.tsx
@@ -129,11 +129,14 @@ function ImportBitlyModal({
body: JSON.stringify({
selectedDomains,
selectedGroupTags,
+ folderId: searchParams.get("folderId"),
}),
}).then(async (res) => {
if (res.ok) {
await mutate();
- router.push(`/${slug}`);
+ queryParams({
+ del: "import",
+ });
} else {
setImporting(false);
throw new Error();