mirror of
https://github.com/hanzoai/sign.git
synced 2026-10-21 01:21:51 +00:00
The product is branded Hanzo eSign (esign.hanzo.ai, repo hanzoai/esign). Rename all workspace packages @hanzo/sign-* → @hanzo/esign-* (1052 files) and align docker/Dockerfile turbo --scope/--filter to the actual remix package name @hanzo/esign (was a pre-existing mismatch with -remix that broke the build). Protected deps (@hanzo/insights, @hanzo/logo) untouched.
59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import type {
|
|
TShareDocumentRequest,
|
|
TShareDocumentResponse,
|
|
} from '@hanzo/esign-trpc/server/document-router/share-document.types';
|
|
import { useZapMutation } from '@hanzo/esign-trpc/zap/react';
|
|
|
|
import { useCopyToClipboard } from './use-copy-to-clipboard';
|
|
|
|
export type UseCopyShareLinkOptions = {
|
|
onSuccess?: () => void;
|
|
onError?: () => void;
|
|
};
|
|
|
|
export function useCopyShareLink({ onSuccess, onError }: UseCopyShareLinkOptions) {
|
|
const [, copyToClipboard] = useCopyToClipboard();
|
|
|
|
const { mutateAsync: createOrGetShareLink, isPending: isCreatingShareLink } = useZapMutation<
|
|
TShareDocumentResponse,
|
|
TShareDocumentRequest
|
|
>('document.share');
|
|
|
|
/**
|
|
* Copy a newly created, or pre-existing share link to the user's clipboard.
|
|
*
|
|
* @param payload The payload to create or get a share link.
|
|
*/
|
|
const createAndCopyShareLink = async (payload: TShareDocumentRequest) => {
|
|
const valueToCopy = createOrGetShareLink(payload).then(
|
|
(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 {
|
|
const isCopySuccess = await copyToClipboard(shareLink);
|
|
if (!isCopySuccess) {
|
|
throw new Error('Copy to clipboard failed');
|
|
}
|
|
|
|
onSuccess?.();
|
|
} catch (e) {
|
|
onError?.();
|
|
}
|
|
};
|
|
|
|
return {
|
|
createAndCopyShareLink,
|
|
copyShareLink,
|
|
isCopyingShareLink: isCreatingShareLink,
|
|
};
|
|
}
|