-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathshare-button.tsx
More file actions
138 lines (126 loc) · 3.51 KB
/
Copy pathshare-button.tsx
File metadata and controls
138 lines (126 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use client';
import { clsx } from 'clsx';
import { useState } from 'react';
import { Stream, Streamable } from '@/vibes/soul/lib/streamable';
import { Button, ButtonProps } from '@/vibes/soul/primitives/button';
import * as Skeleton from '@/vibes/soul/primitives/skeleton';
import { toast } from '@/vibes/soul/primitives/toaster';
import { Tooltip } from '@/vibes/soul/primitives/tooltip';
import { Modal } from '~/components/modal';
import { getShareWishlistModal } from '../../app/[locale]/(default)/account/wishlists/modals';
import { ShareWishlistModal } from './modals/share';
interface Props {
wishlistName: string;
publicUrl: string;
label: string;
isMobileUser: Streamable<boolean>;
isPublic: boolean;
modalTitle: string;
successMessage: string;
copiedMessage: string;
disabledTooltip: string;
closeLabel: string;
copyLabel: string;
size?: ButtonProps['size'];
}
export const WishlistShareButton = ({
wishlistName,
publicUrl,
label,
isMobileUser: streamableIsMobileUser,
isPublic,
modalTitle,
successMessage,
copiedMessage,
disabledTooltip,
closeLabel,
copyLabel,
size = 'small',
}: Props) => {
const [open, setOpen] = useState(false);
const [tooltipOpen, setTooltipOpen] = useState(false);
const getShareUrl = () => String(new URL(publicUrl, window.location.origin));
const nativeShare = async () => {
try {
await navigator.share({ url: getShareUrl(), title: wishlistName });
toast.success(successMessage);
} catch {
// noop
}
};
const copyToClipboard = async () => {
try {
await navigator.clipboard.writeText(getShareUrl());
toast.success(copiedMessage);
setOpen(false);
} catch {
// noop
}
};
return (
<Stream fallback={<WishlistShareButtonSkeleton size={size} />} value={streamableIsMobileUser}>
{(isMobileUser) => {
const ShareButton = (
<Button
disabled={!isPublic}
onClick={isMobileUser ? nativeShare : () => null}
onTouchStart={() => (!isPublic && isMobileUser ? setTooltipOpen(!tooltipOpen) : null)}
size={size}
variant="secondary"
>
{label}
</Button>
);
if (!isPublic) {
return (
<Tooltip
className="max-w-52 pl-4 text-sm"
delayDuration={0}
open={tooltipOpen}
setOpen={setTooltipOpen}
side="bottom"
trigger={ShareButton}
>
{disabledTooltip}
</Tooltip>
);
}
if (!isMobileUser) {
return (
<Modal
className="min-w-64 max-w-lg @lg:min-w-96"
isOpen={open}
setOpen={setOpen}
trigger={ShareButton}
{...getShareWishlistModal(
modalTitle,
copyLabel,
closeLabel,
publicUrl,
copyToClipboard,
)}
>
<ShareWishlistModal publicUrl={publicUrl} />
</Modal>
);
}
return ShareButton;
}}
</Stream>
);
};
export function WishlistShareButtonSkeleton({ size = 'small' }: { size?: Props['size'] }) {
return (
<Skeleton.Box
className={clsx(
'rounded-full',
{
'x-small': 'min-h-8 min-w-[7ch]',
small: 'min-h-10 min-w-[7ch]',
medium: 'min-h-12 min-w-[8ch]',
large: 'min-h-14 min-w-[9ch]',
}[size],
)}
/>
);
}