JavaScript · 1127 bytes Raw Blame History
1 import React, { useState } from 'react';
2
3 const ShareButton = ({ roast, location }) => {
4 const [copied, setCopied] = useState(false);
5
6 const getLocationString = () => {
7 if (!location) return 'somewhere';
8 return location.city || location.state || location.country || 'somewhere';
9 };
10
11 const shareRoast = async () => {
12 const text = `I just got roasted by LocalRoast! 🔥\n\n"${roast}"\n\nFrom: ${getLocationString()}\n\nGet roasted at localroast.lol`;
13
14 if (navigator.share) {
15 try {
16 await navigator.share({
17 title: 'LocalRoast',
18 text: text
19 });
20 } catch (err) {
21 // User cancelled share
22 }
23 } else {
24 // Fallback to clipboard
25 try {
26 await navigator.clipboard.writeText(text);
27 setCopied(true);
28 setTimeout(() => setCopied(false), 2000);
29 } catch (err) {
30 console.error('Failed to copy:', err);
31 }
32 }
33 };
34
35 return (
36 <button onClick={shareRoast} className="share-button">
37 {copied ? 'Copied! 📋' : 'Share this burn 📤'}
38 </button>
39 );
40 };
41
42 export default ShareButton;