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

Function useCopyLink

packages/shared/src/hooks/useCopy.ts:30–86  ·  view source on GitHub ↗
(
  getLink?: () => string,
  shorten = false,
)

Source from the content-addressed store, hash-verified

28 | ((props?: CopyNotifyFunctionProps) => Promise<void>);
29
30export function useCopyLink(
31 getLink?: () => string,
32 shorten = false,
33): [boolean, CopyNotifyFunction] {
34 const [copying, setCopying] = useState(false);
35 const { displayToast } = useToastNotification();
36 const { getShortUrl } = useGetShortUrl();
37
38 const copy: CopyNotifyFunction = async (props = {}) => {
39 // getLink is optional: useCopyPostLink omits it when the link is only
40 // known at press time, and those callers pass it in props instead.
41 const link = props.link || getLink?.();
42 const shortenLink = props.shorten || shorten;
43
44 if (link) {
45 try {
46 // write the link to clipboard
47 await navigator.clipboard.writeText(link);
48 } catch {
49 displayToast(blockedMessage, { variant: ToastType.Error });
50
51 return;
52 }
53
54 // try with a shortened link as well, if requested
55 if (shortenLink) {
56 try {
57 const clipBoardItem = new ClipboardItem({
58 // A promise, not an awaited value: awaiting the shortener first
59 // would end the task that handled the gesture, and Safari refuses
60 // the write after that.
61 'text/plain': getShortUrl(link, props.cid).then((shortenedLink) => {
62 return new Blob([shortenedLink], { type: 'text/plain' });
63 }),
64 });
65 await navigator.clipboard.write([clipBoardItem]);
66 } catch (e) {
67 // eslint-disable-next-line no-console
68 console.warn('Error copying to clipboard', e);
69 }
70 }
71
72 if (!props.disableToast) {
73 displayToast(props.message || defaultLinkMessage, props);
74 }
75 } else {
76 displayToast(noLinkErrorMessage, { variant: ToastType.Error });
77 }
78
79 setCopying(true);
80 setTimeout(() => {
81 setCopying(false);
82 }, 1000);
83 };
84
85 return [copying, copy];
86}
87

Callers 14

BrowserPreviewFrameFunction · 0.90
SharedBookmarksModalFunction · 0.90
InviteLinkInputFunction · 0.90
SocialShareFunction · 0.90
AgentCodeBlockFunction · 0.90
HackathonSignupButtonFunction · 0.90
InviteMemberModalFunction · 0.90
useCopyPostLinkFunction · 0.90
useShareOrCopyLinkFunction · 0.90
useSquadInvitationFunction · 0.90
DevCardStep2Function · 0.90
RecruiterPaymentPageFunction · 0.90

Calls 2

useToastNotificationFunction · 0.90
useGetShortUrlFunction · 0.90

Tested by

no test coverage detected