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.

Fix caching issue on integrations page

Kiran Ke40b2447811a
diff --git a/apps/web/app/api/integrations/route.ts b/apps/web/app/api/integrations/route.ts
new file mode 100644
index 0000000000..143f004b6a
--- /dev/null
+++ b/apps/web/app/api/integrations/route.ts
@@ -0,0 +1,32 @@
+import { withWorkspace } from "@/lib/auth";
+import { prisma } from "@/lib/prisma";
+import { NextResponse } from "next/server";
+
+// GET /api/integrations - get all active integrations
+export const GET = withWorkspace(
+ async ({ workspace }) => {
+ const integrations = await prisma.oAuthApp.findMany({
+ select: {
+ clientId: true,
+ name: true,
+ slug: true,
+ },
+ where: {
+ verified: true,
+ authorizedApps: {
+ some: {
+ project: {
+ slug: workspace.slug,
+ },
+ },
+ },
+ },
+ });
+
+ return NextResponse.json(integrations);
+ },
+ {
+ requiredPermissions: ["workspaces.read"],
+ featureFlag: "integrations",
+ },
+);
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/page.tsx
index d8799f90e9..58a590f6e5 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/page.tsx
@@ -5,6 +5,8 @@ import { Suspense } from "react";
import IntegrationsPageClient from "./page-client";
import PageHeader from "./page-header";
+export const revalidate = 300; // 5 minutes
+
export default async function IntegrationsPage({
params,
}: {
@@ -35,13 +37,6 @@ const Integrations = async ({ slug }: { slug: string }) => {
authorizedApps: true,
},
},
- authorizedApps: {
- where: {
- project: {
- slug,
- },
- },
- },
},
});
@@ -50,7 +45,6 @@ const Integrations = async ({ slug }: { slug: string }) => {
integrations={integrations.map((integration) => ({
...integration,
installations: integration._count.authorizedApps,
- installed: integration.authorizedApps.length > 0,
}))}
/>
);
diff --git a/apps/web/lib/swr/use-integrations.ts b/apps/web/lib/swr/use-integrations.ts
new file mode 100644
index 0000000000..0e39be6e9f
--- /dev/null
+++ b/apps/web/lib/swr/use-integrations.ts
@@ -0,0 +1,22 @@
+import { InstalledIntegrationProps } from "@/lib/types";
+import { fetcher } from "@dub/utils";
+import useSWR from "swr";
+import useWorkspace from "./use-workspace";
+
+export default function useIntegrations() {
+ const { id } = useWorkspace();
+
+ const { data: integrations, error } = useSWR<
+ Pick<InstalledIntegrationProps, "clientId" | "name" | "slug">[]
+ >(`/api/integrations?workspaceId=${id}`, fetcher, {
+ dedupingInterval: 20000,
+ revalidateOnFocus: false,
+ keepPreviousData: true,
+ });
+
+ return {
+ integrations,
+ error,
+ loading: !integrations && !error,
+ };
+}
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 8435b57727..13f2d6ac81 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -198,7 +198,7 @@ export type InstalledIntegrationProps = Pick<
"clientId" | "slug" | "logo" | "name" | "developer" | "description"
> & {
installations: number;
- installed: boolean;
+ installed?: boolean;
};
export type InstalledIntegrationInfoProps = Pick<
diff --git a/apps/web/ui/integrations/integration-card.tsx b/apps/web/ui/integrations/integration-card.tsx
index 021e6395a0..01bff3b2c1 100644
--- a/apps/web/ui/integrations/integration-card.tsx
+++ b/apps/web/ui/integrations/integration-card.tsx
@@ -1,3 +1,4 @@
+import useIntegrations from "@/lib/swr/use-integrations";
import useWorkspace from "@/lib/swr/use-workspace";
import { InstalledIntegrationProps } from "@/lib/types";
import { BlurImage, TokenAvatar } from "@dub/ui";
@@ -8,13 +9,18 @@ export default function IntegrationCard(
integration: InstalledIntegrationProps,
) {
const { slug } = useWorkspace();
+ const { integrations: activeIntegrations } = useIntegrations();
+
+ const installed = activeIntegrations?.some(
+ (i) => i.clientId === integration.clientId,
+ );
return (
<Link
href={`/${slug}/integrations/${integration.slug}`}
className="relative rounded-xl border border-gray-200 bg-white px-5 py-4 transition-[filter] hover:[filter:drop-shadow(0_8px_12px_#222A350d)_drop-shadow(0_32px_80px_#2f30370f)]"
>
- {integration.installed && (
+ {installed && (
<p className="absolute right-4 top-4 text-xs text-gray-500">
INSTALLED
</p>