refactor: 提取 useShare hook,统一分享和剪贴板逻辑
新增 useShare(Web Share API + clipboard fallback), 消除 QrInviteModal、BlindboxRoomPage、ShareCardModal 三处重复的分享代码。
This commit is contained in:
@@ -7,6 +7,7 @@ import { QRCodeSVG } from "qrcode.react";
|
||||
import { toPng } from "html-to-image";
|
||||
import type { Restaurant, MatchType, SceneType } from "@/types";
|
||||
import { useToast } from "@/hooks/useToast";
|
||||
import { useShare } from "@/hooks/useShare";
|
||||
|
||||
type ShareCardData =
|
||||
| {
|
||||
@@ -832,6 +833,8 @@ export default function ShareCardModal({
|
||||
toast.show("图片已保存");
|
||||
}, [handleGenerate, toast, data]);
|
||||
|
||||
const { share: nativeShare } = useShare();
|
||||
|
||||
const handleShare = useCallback(async () => {
|
||||
const png = await handleGenerate();
|
||||
if (!png) {
|
||||
@@ -840,20 +843,12 @@ export default function ShareCardModal({
|
||||
}
|
||||
|
||||
const file = dataUrlToFile(png, "NoWhatever.png");
|
||||
|
||||
const shareData: ShareData = { files: [file] };
|
||||
try {
|
||||
if (navigator.canShare?.(shareData)) {
|
||||
await navigator.share(shareData);
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof Error && e.name === "AbortError") return;
|
||||
const shared = await nativeShare({ files: [file] });
|
||||
if (!shared) {
|
||||
downloadDataUrl(png, "NoWhatever.png");
|
||||
toast.show("图片已保存,快去分享吧!");
|
||||
}
|
||||
|
||||
downloadDataUrl(png, "NoWhatever.png");
|
||||
toast.show("图片已保存,快去分享吧!");
|
||||
}, [handleGenerate, toast]);
|
||||
}, [handleGenerate, toast, nativeShare]);
|
||||
|
||||
const handleBackdropClick = (e: React.MouseEvent) => {
|
||||
if (e.target === backdropRef.current) onClose();
|
||||
|
||||
Reference in New Issue
Block a user