-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshare-button.tsx
More file actions
175 lines (162 loc) · 5.37 KB
/
Copy pathshare-button.tsx
File metadata and controls
175 lines (162 loc) · 5.37 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"use client";
import { Send, Copy } from "lucide-react";
import { usePathname } from "next/navigation";
import { useToast } from "@/components/ui/use-toast";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { useState, useEffect } from "react";
import Image from "next/image";
import Link from "next/link";
type RecipeType = {
title: string;
id: string;
likes: number;
image_url: string | null;
cuisine: string;
category: string;
prepTime: number;
ingredients: string;
procedure: string;
username: string | null;
submitted_by: string | null;
created_at: Date;
updated_at: Date;
};
export default function ShareButton({ recipe }: { recipe: RecipeType }) {
const pathname = usePathname();
const { toast } = useToast();
const [isMobile, setIsMobile] = useState(false);
const [isOpen, setIsOpen] = useState(false);
useEffect(() => {
// Check if device supports native sharing
// Only enable on mobile devices that actually have native sharing
const isMobileDevice =
/Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent
);
const hasNativeSharing =
typeof navigator !== "undefined" &&
"share" in navigator &&
navigator.canShare &&
navigator.canShare({ url: "https://dishcraft.vercel.app" });
setIsMobile(isMobileDevice && hasNativeSharing);
}, []);
const shareUrl = `https://dishcraft.vercel.app/${recipe.id.replace("_", "/")}`;
const shareTitle = `Check out ${recipe.title} by ${recipe.username} on DishCraft`;
const encodedShareTitle = encodeURIComponent(`${shareTitle}: ${shareUrl}`);
async function copyLink() {
try {
await navigator.clipboard.writeText(shareUrl);
toast({ title: "Link copied to clipboard!" });
setIsOpen(false);
} catch (error) {
toast({ title: "Failed to copy link", variant: "destructive" });
}
}
async function handleShare() {
if (isMobile) {
try {
await navigator.share({
title: shareTitle,
url: shareUrl,
});
} catch (error) {
// On mobile, don't show dialog - just silently fail
// This ensures only native sharing is used on mobile
console.log("Native sharing failed or was cancelled");
}
} else {
setIsOpen(true);
}
}
return (
<>
<Send
className="h-6 w-6 cursor-pointer"
onClick={handleShare}
aria-label="share recipe"
/>
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>Share Recipe</DialogTitle>
</DialogHeader>
<div className="flex flex-col items-center gap-6">
<div className="grid grid-cols-2 gap-4 w-full">
<Button
onClick={copyLink}
variant="outline"
className="flex flex-col items-center gap-2 h-20"
>
<Copy className="h-6 w-6" />
<span className="text-sm">Copy Link</span>
</Button>
<Button
variant="outline"
className="flex flex-col items-center gap-2 h-20 bg-green-50 hover:bg-green-100 border-green-200"
asChild
>
<Link
href={`https://wa.me/?text=${encodedShareTitle}`}
target="_blank"
>
<Image
src="https://img.icons8.com/color/48/whatsapp--v1.png"
alt="whatsapp"
width={40}
height={40}
unoptimized
/>
<span className="text-sm">WhatsApp</span>
</Link>
</Button>
<Button
variant="outline"
className="flex flex-col items-center gap-2 h-20 bg-black hover:bg-neutral-800 text-white border-black hover:text-white"
asChild
>
<Link
href={`https://twitter.com/intent/tweet?text=${encodedShareTitle}`}
target="_blank"
>
<Image
src="https://img.icons8.com/color/48/twitterx--v1.png"
alt="twitter"
width={40}
height={40}
unoptimized
/>
<span className="text-sm">X (Twitter)</span>
</Link>
</Button>
<Button
variant="outline"
className="flex flex-col items-center gap-2 h-20 bg-blue-50 hover:bg-blue-100 border-blue-200"
asChild
>
<Link
href={`https://www.facebook.com/sharer/sharer.php?u=${shareUrl}`}
target="_blank"
>
<Image
src="https://img.icons8.com/color/48/facebook-new.png"
alt="facebook"
width={40}
height={40}
unoptimized
/>
<span className="text-sm">Facebook</span>
</Link>
</Button>
</div>
</div>
</DialogContent>
</Dialog>
</>
);
}