a45c5c6647
Add a dedicated Share button using the Web Share API with clipboard fallback across three locations: - NoteCard action bar (between Zap and More) - PostDetailPage action bars (both compact and expanded views) - ProfilePage header (between More and Zap buttons) On mobile and supported desktop browsers, tapping Share opens the native OS share sheet with all installed apps (Twitter, WhatsApp, etc.). On unsupported browsers, falls back to copying the link to clipboard with a toast confirmation. Closes #39
26 lines
823 B
TypeScript
26 lines
823 B
TypeScript
/**
|
|
* Share a URL using the native Web Share API when available,
|
|
* falling back to copying to clipboard.
|
|
*
|
|
* @returns `'shared'` if the native share sheet was used,
|
|
* `'copied'` if the URL was copied to clipboard,
|
|
* `'cancelled'` if the user dismissed the share sheet.
|
|
*/
|
|
export async function shareOrCopy(url: string, title?: string): Promise<'shared' | 'copied' | 'cancelled'> {
|
|
if (navigator.share) {
|
|
try {
|
|
await navigator.share({ url, title });
|
|
return 'shared';
|
|
} catch (error) {
|
|
// User cancelled the share sheet — not an error
|
|
if (error instanceof Error && error.name === 'AbortError') {
|
|
return 'cancelled';
|
|
}
|
|
// Some other error — fall through to clipboard
|
|
}
|
|
}
|
|
|
|
await navigator.clipboard.writeText(url);
|
|
return 'copied';
|
|
}
|