mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 00:32:43 +10:00
refactor: restructure logic
This commit is contained in:
@ -20,7 +20,7 @@ export type DataTableActionButtonProps = {
|
|||||||
export const DataTableActionButton = ({ row }: DataTableActionButtonProps) => {
|
export const DataTableActionButton = ({ row }: DataTableActionButtonProps) => {
|
||||||
const { data: session } = useSession();
|
const { data: session } = useSession();
|
||||||
|
|
||||||
const { copyShareLink, isCopyingShareLink } = useCopyShareLink();
|
const { createAndCopyShareLink, isCopyingShareLink } = useCopyShareLink();
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
return null;
|
return null;
|
||||||
@ -63,12 +63,12 @@ export const DataTableActionButton = ({ row }: DataTableActionButtonProps) => {
|
|||||||
<Button
|
<Button
|
||||||
className="w-24"
|
className="w-24"
|
||||||
loading={isCopyingShareLink}
|
loading={isCopyingShareLink}
|
||||||
onClick={() => {
|
onClick={async () =>
|
||||||
void copyShareLink({
|
createAndCopyShareLink({
|
||||||
token: recipient?.token,
|
token: recipient?.token,
|
||||||
documentId: row.id,
|
documentId: row.id,
|
||||||
});
|
})
|
||||||
}}
|
}
|
||||||
>
|
>
|
||||||
{!isCopyingShareLink && <Share className="-ml-1 mr-2 h-4 w-4" />}
|
{!isCopyingShareLink && <Share className="-ml-1 mr-2 h-4 w-4" />}
|
||||||
Share
|
Share
|
||||||
|
|||||||
@ -39,7 +39,7 @@ export type DataTableActionDropdownProps = {
|
|||||||
export const DataTableActionDropdown = ({ row }: DataTableActionDropdownProps) => {
|
export const DataTableActionDropdown = ({ row }: DataTableActionDropdownProps) => {
|
||||||
const { data: session } = useSession();
|
const { data: session } = useSession();
|
||||||
|
|
||||||
const { copyShareLink, isCopyingShareLink } = useCopyShareLink();
|
const { createAndCopyShareLink, isCopyingShareLink } = useCopyShareLink();
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
return null;
|
return null;
|
||||||
@ -140,12 +140,12 @@ export const DataTableActionDropdown = ({ row }: DataTableActionDropdownProps) =
|
|||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
|
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
onClick={() => {
|
onClick={async () =>
|
||||||
void copyShareLink({
|
createAndCopyShareLink({
|
||||||
token: recipient?.token,
|
token: recipient?.token,
|
||||||
documentId: row.id,
|
documentId: row.id,
|
||||||
});
|
})
|
||||||
}}
|
}
|
||||||
>
|
>
|
||||||
{isCopyingShareLink ? (
|
{isCopyingShareLink ? (
|
||||||
<Loader className="mr-2 h-4 w-4" />
|
<Loader className="mr-2 h-4 w-4" />
|
||||||
|
|||||||
@ -24,7 +24,7 @@ export type ShareButtonProps = HTMLAttributes<HTMLButtonElement> & {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const ShareButton = ({ token, documentId }: ShareButtonProps) => {
|
export const ShareButton = ({ token, documentId }: ShareButtonProps) => {
|
||||||
const { copyShareLink, isCopyingShareLink } = useCopyShareLink();
|
const { copyShareLink, createAndCopyShareLink, isCopyingShareLink } = useCopyShareLink();
|
||||||
|
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
|
|
||||||
@ -46,14 +46,14 @@ export const ShareButton = ({ token, documentId }: ShareButtonProps) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onCopyClick = async () => {
|
const onCopyClick = async () => {
|
||||||
const copyToClipboardValue = shareLink
|
if (shareLink) {
|
||||||
? `${window.location.origin}/share/${shareLink.slug}`
|
await copyShareLink(`${window.location.origin}/share/${shareLink.slug}`);
|
||||||
: {
|
} else {
|
||||||
|
await createAndCopyShareLink({
|
||||||
token,
|
token,
|
||||||
documentId,
|
documentId,
|
||||||
};
|
});
|
||||||
|
}
|
||||||
await copyShareLink(copyToClipboardValue);
|
|
||||||
|
|
||||||
setIsOpen(false);
|
setIsOpen(false);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -13,22 +13,26 @@ export function useCopyShareLink() {
|
|||||||
trpc.shareLink.createOrGetShareLink.useMutation();
|
trpc.shareLink.createOrGetShareLink.useMutation();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copy a share link to the user's clipboard.
|
* Copy a newly created, or pre-existing share link to the user's clipboard.
|
||||||
*
|
*
|
||||||
* Will create or get a share link if one is not provided.
|
* @param payload The payload to create or get a share link.
|
||||||
*
|
|
||||||
* @param payload Either the share link itself or the input to create a new share link.
|
|
||||||
*/
|
*/
|
||||||
const copyShareLink = async (payload: TCreateOrGetShareLinkMutationSchema | string) => {
|
const createAndCopyShareLink = async (payload: TCreateOrGetShareLinkMutationSchema) => {
|
||||||
const valueToCopy =
|
const valueToCopy = createOrGetShareLink(payload).then(
|
||||||
typeof payload === 'string'
|
|
||||||
? payload
|
|
||||||
: createOrGetShareLink(payload).then(
|
|
||||||
(result) => `${window.location.origin}/share/${result.slug}`,
|
(result) => `${window.location.origin}/share/${result.slug}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await copyShareLink(valueToCopy);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy a share link to the user's clipboard.
|
||||||
|
*
|
||||||
|
* @param shareLink Either the share link itself or a promise that returns a shared link.
|
||||||
|
*/
|
||||||
|
const copyShareLink = async (shareLink: Promise<string> | string) => {
|
||||||
try {
|
try {
|
||||||
const isCopySuccess = await copyToClipboard(valueToCopy);
|
const isCopySuccess = await copyToClipboard(shareLink);
|
||||||
if (!isCopySuccess) {
|
if (!isCopySuccess) {
|
||||||
throw new Error('Copy to clipboard failed');
|
throw new Error('Copy to clipboard failed');
|
||||||
}
|
}
|
||||||
@ -48,7 +52,8 @@ export function useCopyShareLink() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
isCopyingShareLink: isCreatingShareLink,
|
createAndCopyShareLink,
|
||||||
copyShareLink,
|
copyShareLink,
|
||||||
|
isCopyingShareLink: isCreatingShareLink,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user