mirror of
https://github.com/hanzoai/dataroom.git
synced 2026-10-30 18:41:07 +00:00
94 lines
1.8 KiB
TypeScript
94 lines
1.8 KiB
TypeScript
import { useRouter } from "next/router";
|
|
|
|
import { useTeam } from "@/context/team-context";
|
|
import { View } from "@prisma/client";
|
|
import useSWR from "swr";
|
|
|
|
import { fetcher } from "@/lib/utils";
|
|
|
|
import { LinkWithDocument } from "../types";
|
|
|
|
export function useLink() {
|
|
const router = useRouter();
|
|
|
|
const { linkId } = router.query as {
|
|
linkId: string;
|
|
};
|
|
|
|
// only fetch data once when linkId is present
|
|
const { data: link, error } = useSWR<LinkWithDocument>(
|
|
linkId && `/api/links/${encodeURIComponent(linkId)}`,
|
|
fetcher,
|
|
{
|
|
dedupingInterval: 10000,
|
|
},
|
|
);
|
|
|
|
return {
|
|
link,
|
|
loading: !error && !link,
|
|
error,
|
|
};
|
|
}
|
|
|
|
export function useDomainLink() {
|
|
const router = useRouter();
|
|
|
|
const { domain, slug } = router.query as {
|
|
domain: string;
|
|
slug: string;
|
|
};
|
|
|
|
const { data: link, error } = useSWR<LinkWithDocument>(
|
|
domain &&
|
|
slug &&
|
|
`/api/links/domains/${encodeURIComponent(domain)}/${encodeURIComponent(
|
|
slug,
|
|
)}`,
|
|
fetcher,
|
|
{
|
|
dedupingInterval: 10000,
|
|
},
|
|
);
|
|
|
|
return {
|
|
link,
|
|
loading: !error && !link,
|
|
error,
|
|
};
|
|
}
|
|
|
|
interface ViewWithDuration extends View {
|
|
duration: {
|
|
data: { pageNumber: string; sum_duration: number }[];
|
|
};
|
|
totalDuration: number;
|
|
completionRate: number;
|
|
}
|
|
|
|
interface LinkVisitsResponse {
|
|
views: ViewWithDuration[];
|
|
hiddenFromPause: number;
|
|
}
|
|
|
|
export function useLinkVisits(linkId: string) {
|
|
const teamInfo = useTeam();
|
|
const teamId = teamInfo?.currentTeam?.id;
|
|
const { data, error } = useSWR<LinkVisitsResponse>(
|
|
linkId &&
|
|
teamId &&
|
|
`/api/teams/${teamId}/links/${encodeURIComponent(linkId)}/visits`,
|
|
fetcher,
|
|
{
|
|
dedupingInterval: 10000,
|
|
},
|
|
);
|
|
|
|
return {
|
|
views: data?.views,
|
|
hiddenFromPause: data?.hiddenFromPause ?? 0,
|
|
loading: !error && !data,
|
|
error,
|
|
};
|
|
}
|