MCPcopy Create free account
hub / github.com/dailydotdev/apps / useGetShortUrl

Function useGetShortUrl

packages/shared/src/hooks/utils/useGetShortUrl.ts:43–101  ·  view source on GitHub ↗
({
  query,
}: UseGetShortUrl = {})

Source from the content-addressed store, hash-verified

41};
42
43export const useGetShortUrl = ({
44 query,
45}: UseGetShortUrl = {}): UseGetShortUrlResult => {
46 const { user, isAuthReady } = useAuthContext();
47 const queryClient = useQueryClient();
48 const getProps = useCallback(
49 (url: string, cid?: ReferralCampaignKey) =>
50 getShortLinkProps(url, cid, user),
51 [user],
52 );
53
54 const queryShortUrl = async (url: string) => {
55 const res = await gqlClient.request(GET_SHORT_URL_QUERY, { url });
56 return res.getShortUrl;
57 };
58
59 const getShortUrl = useCallback(
60 async (url: string, cid?: ReferralCampaignKey) => {
61 if (!url || !isAuthReady || !user) {
62 return url;
63 }
64
65 const { trackedUrl, queryKey } = getProps(url, cid);
66
67 try {
68 return queryClient.fetchQuery({
69 queryKey,
70 queryFn: () => queryShortUrl(trackedUrl),
71 staleTime: Infinity,
72 });
73 } catch (err) {
74 return trackedUrl;
75 }
76 },
77 [isAuthReady, user, getProps, queryClient],
78 );
79
80 const getTrackedUrl = useCallback(
81 (url: string, cid?: ReferralCampaignKey) => {
82 const { trackedUrl } = getProps(url, cid);
83 return trackedUrl;
84 },
85 [getProps],
86 );
87
88 const isEnabled = query?.enabled ?? true;
89 const { queryKey, trackedUrl } = query
90 ? getProps(query.url, query.cid)
91 : { queryKey: [], trackedUrl: '' };
92 const { data: shareLink, isPending } = useQuery({
93 queryKey,
94 queryFn: () => queryShortUrl(trackedUrl),
95 ...disabledRefetch,
96 staleTime: Infinity,
97 enabled: !!query?.url && isEnabled && !!user,
98 });
99
100 return { getShortUrl, getTrackedUrl, shareLink, isLoading: isPending };

Callers 13

ShareBarFunction · 0.90
SocialShareFunction · 0.90
SocialShareListFunction · 0.90
MobilePostFloatingBarFunction · 0.90
MobilePostFloatingBarV1Function · 0.90
PostContentShareFunction · 0.90
DiscussionShareRowFunction · 0.90
usePostContentFunction · 0.90
useSharePostFunction · 0.90
useShareCommentFunction · 0.90
useCopyLinkFunction · 0.90
useShareOrCopyLinkFunction · 0.90

Calls 3

useAuthContextFunction · 0.90
getShortLinkPropsFunction · 0.85
queryShortUrlFunction · 0.85

Tested by

no test coverage detected