Skip to content

Commit 3a77027

Browse files
committed
Fix useDomain()
Closes #1810
1 parent 98b6b6a commit 3a77027

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

apps/web/lib/swr/use-domain.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
import { fetcher } from "@dub/utils";
1+
import { fetcher, isDubDomain } from "@dub/utils";
22
import useSWR from "swr";
33
import { DomainProps } from "../types";
44
import useWorkspace from "./use-workspace";
55

6-
export default function useDomain(slug: string) {
6+
export default function useDomain({
7+
slug,
8+
enabled,
9+
}: {
10+
slug: string;
11+
enabled?: boolean;
12+
}) {
713
const { id: workspaceId } = useWorkspace();
814

915
const { data: domain, error } = useSWR<DomainProps>(
10-
workspaceId && slug && `/api/domains/${slug}?workspaceId=${workspaceId}`,
16+
enabled &&
17+
workspaceId &&
18+
slug &&
19+
!isDubDomain(slug) &&
20+
`/api/domains/${slug}?workspaceId=${workspaceId}`,
1121
fetcher,
22+
{ refreshInterval: 60000 },
1223
);
1324

1425
return {

apps/web/ui/links/link-title-column.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

3+
import useDomain from "@/lib/swr/use-domain";
34
import useWorkspace from "@/lib/swr/use-workspace";
4-
import { DomainProps } from "@/lib/types";
55
import {
66
ArrowTurnRight2,
77
Avatar,
@@ -27,7 +27,6 @@ import {
2727
} from "@dub/ui/icons";
2828
import {
2929
cn,
30-
fetcher,
3130
formatDateTime,
3231
getApexDomain,
3332
getPrettyUrl,
@@ -38,7 +37,6 @@ import {
3837
import * as HoverCard from "@radix-ui/react-hover-card";
3938
import { Mail } from "lucide-react";
4039
import { memo, PropsWithChildren, useContext, useRef, useState } from "react";
41-
import useSWR from "swr";
4240
import { useLinkBuilder } from "../modals/link-builder";
4341
import { ResponseLink } from "./links-container";
4442
import { LinksDisplayContext } from "./links-display-provider";
@@ -151,19 +149,12 @@ function UnverifiedTooltip({
151149
_key,
152150
children,
153151
}: PropsWithChildren<{ domain: string; _key: string }>) {
154-
const { id: workspaceId, slug } = useWorkspace();
152+
const { slug } = useWorkspace();
155153

156154
const ref = useRef<HTMLDivElement>(null);
157155
const isVisible = useInViewport(ref);
158156

159-
const { data: { verified } = {} } = useSWR<DomainProps>(
160-
workspaceId &&
161-
isVisible &&
162-
!isDubDomain(domain) &&
163-
`/api/domains/${domain}?workspaceId=${workspaceId}`,
164-
fetcher,
165-
{ refreshInterval: 60000 },
166-
);
157+
const { verified } = useDomain({ slug: domain, enabled: isVisible });
167158

168159
return (
169160
<div ref={ref}>

apps/web/ui/modals/link-builder/qr-code-preview.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import { QRCode } from "@/ui/shared/qr-code";
44
import {
55
Button,
66
ShimmerDots,
7+
useInViewport,
78
useKeyboardShortcut,
89
useLocalStorage,
910
useMediaQuery,
1011
} from "@dub/ui";
1112
import { Pen2, QRCode as QRCodeIcon } from "@dub/ui/icons";
1213
import { DUB_QR_LOGO, linkConstructor } from "@dub/utils";
1314
import { AnimatePresence, motion } from "framer-motion";
14-
import { useMemo } from "react";
15+
import { useMemo, useRef } from "react";
1516
import { useFormContext } from "react-hook-form";
1617
import { useDebounce } from "use-debounce";
1718
import { LinkFormData } from ".";
@@ -30,7 +31,13 @@ export function QRCodePreview() {
3031
const [key] = useDebounce(rawKey, 500);
3132
const [domain] = useDebounce(rawDomain, 500);
3233

33-
const { logo: domainLogo } = useDomain(rawDomain);
34+
const ref = useRef<HTMLDivElement>(null);
35+
const isVisible = useInViewport(ref);
36+
37+
const { logo: domainLogo } = useDomain({
38+
slug: rawDomain,
39+
enabled: isVisible,
40+
});
3441

3542
const [data, setData] = useLocalStorage<QRCodeDesign>(
3643
`qr-code-design-${workspaceId}`,
@@ -64,7 +71,7 @@ export function QRCodePreview() {
6471
});
6572

6673
return (
67-
<div>
74+
<div ref={ref}>
6875
<LinkQRModal />
6976
<div className="flex items-center justify-between">
7077
<h2 className="text-sm font-medium text-gray-700">QR Code</h2>

apps/web/ui/modals/link-qr-modal.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ function LinkQRModal(
7777
function LinkQRModalInner({
7878
props,
7979
onSave,
80+
showLinkQRModal,
8081
setShowLinkQRModal,
8182
}: {
8283
showLinkQRModal: boolean;
@@ -85,7 +86,10 @@ function LinkQRModalInner({
8586
const { id: workspaceId, slug, plan, logo: workspaceLogo } = useWorkspace();
8687
const id = useId();
8788
const { isMobile } = useMediaQuery();
88-
const { logo: domainLogo } = useDomain(props.domain);
89+
const { logo: domainLogo } = useDomain({
90+
slug: props.domain,
91+
enabled: showLinkQRModal,
92+
});
8993

9094
const url = useMemo(() => {
9195
return props.key && props.domain

0 commit comments

Comments
 (0)