mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-09 13:15:51 +08:00
49 lines
1005 B
TypeScript
49 lines
1005 B
TypeScript
import { useAFFiNEI18N } from '@affine/i18n/hooks';
|
|
import { useCallback, useMemo } from 'react';
|
|
|
|
import { toast } from '../../ui/toast';
|
|
|
|
type UrlType = 'share' | 'workspace';
|
|
|
|
type UseSharingUrl = {
|
|
workspaceId: string;
|
|
pageId: string;
|
|
urlType: UrlType;
|
|
};
|
|
|
|
export const generateUrl = ({
|
|
workspaceId,
|
|
pageId,
|
|
urlType,
|
|
}: UseSharingUrl) => {
|
|
return `${runtimeConfig.serverUrlPrefix}/${urlType}/${workspaceId}/${pageId}`;
|
|
};
|
|
|
|
export const useSharingUrl = ({
|
|
workspaceId,
|
|
pageId,
|
|
urlType,
|
|
}: UseSharingUrl) => {
|
|
const t = useAFFiNEI18N();
|
|
const sharingUrl = useMemo(
|
|
() => generateUrl({ workspaceId, pageId, urlType }),
|
|
[urlType, workspaceId, pageId]
|
|
);
|
|
|
|
const onClickCopyLink = useCallback(() => {
|
|
navigator.clipboard
|
|
.writeText(sharingUrl)
|
|
.then(() => {
|
|
toast(t['Copied link to clipboard']());
|
|
})
|
|
.catch(err => {
|
|
console.error(err);
|
|
});
|
|
}, [sharingUrl, t]);
|
|
|
|
return {
|
|
sharingUrl,
|
|
onClickCopyLink,
|
|
};
|
|
};
|