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 PartnerLinkSelector

Steven Tey0310178fc9f6
diff --git a/apps/web/lib/swr/use-link.ts b/apps/web/lib/swr/use-link.ts
new file mode 100644
index 0000000000..9b2355b969
--- /dev/null
+++ b/apps/web/lib/swr/use-link.ts
@@ -0,0 +1,18 @@
+import { fetcher } from "@dub/utils";
+import useSWR from "swr";
+import { ExpandedLinkProps } from "../types";
+import useWorkspace from "./use-workspace";
+
+export default function useLink(linkId: string) {
+ const { id: workspaceId } = useWorkspace();
+
+ const { data: link, error } = useSWR<ExpandedLinkProps>(
+ workspaceId && linkId && `/api/links/${linkId}?workspaceId=${workspaceId}`,
+ fetcher,
+ );
+
+ return {
+ link,
+ loading: !link && !error,
+ };
+}
diff --git a/apps/web/ui/partners/partner-link-selector.tsx b/apps/web/ui/partners/partner-link-selector.tsx
index a96b3ceb2f..b90cced129 100644
--- a/apps/web/ui/partners/partner-link-selector.tsx
+++ b/apps/web/ui/partners/partner-link-selector.tsx
@@ -1,3 +1,4 @@
+import useLink from "@/lib/swr/use-link";
import useLinks from "@/lib/swr/use-links";
import { LinkProps } from "@/lib/types";
import { Combobox, LinkLogo } from "@dub/ui";
@@ -51,9 +52,7 @@ export function PartnerLinkSelector({
},
);
- const { links: selectedLinks } = useLinks({
- linkIds: [selectedLinkId ?? ""],
- });
+ const { link: selectedLink } = useLink(selectedLinkId ?? "");
const options = useMemo(
() => links?.map((link) => getLinkOption(link)),
@@ -61,13 +60,9 @@ export function PartnerLinkSelector({
);
const selectedOption = useMemo(() => {
- const link = [...(links || []), ...(selectedLinks || [])].find(
- ({ id }) => id === selectedLinkId,
- );
- return link ? getLinkOption(link) : null;
- }, [selectedLinkId, links, selectedLinks]);
-
- const selectedLink = links?.find((l) => l.id === selectedLinkId);
+ if (!selectedLink) return null;
+ return getLinkOption(selectedLink);
+ }, [selectedLink]);
return (
<>