File size: 6,387 Bytes
ebde876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client"

import {
    Popover,
    PopoverContent,
    PopoverTrigger,
} from "@/components/ui/popover"
import { Button } from "@/components/ui/button"
import { Share2, Copy, Check, Twitter, Facebook, Linkedin, Link as LinkIcon, MessageSquare, MessageCircle, Send, Gamepad2 } from "lucide-react"
import { useState } from "react"
import { cn } from "@/lib/utils"

interface ShareMenuProps {
    artworkId: string
    prompt: string
    trigger?: React.ReactNode
    className?: string
}

export function ShareMenu({ artworkId, prompt, trigger, className }: ShareMenuProps) {
    const [copied, setCopied] = useState(false)
    const [isOpen, setIsOpen] = useState(false)

    // Base URL needed for client-side composition
    // Fallback to window.location if available logic to prevent SSR issues
    const baseUrl = typeof window !== "undefined" ? window.location.origin : ""
    const shareUrl = `${baseUrl}/artwork/${artworkId}`
    const encodedUrl = encodeURIComponent(shareUrl)
    const encodedText = encodeURIComponent(`Check out this AI art: "${prompt}" created on Imyra!`)

    const handleCopy = async () => {
        try {
            await navigator.clipboard.writeText(shareUrl)
            setCopied(true)
            setTimeout(() => setCopied(false), 2000)
        } catch (err) {
            console.error("Failed to copy", err)
        }
    }

    const shareLinks = [
        {
            name: "Twitter / X",
            icon: Twitter,
            url: `https://twitter.com/intent/tweet?text=${encodedText}&url=${encodedUrl}`,
            color: "hover:text-blue-400"
        },
        {
            name: "Facebook",
            icon: Facebook,
            url: `https://www.facebook.com/sharer/sharer.php?u=${encodedUrl}`,
            color: "hover:text-blue-600"
        },
        {
            name: "WhatsApp",
            icon: MessageCircle,
            url: `https://wa.me/?text=${encodedText}%20${encodedUrl}`,
            color: "hover:text-green-500"
        },
        {
            name: "Telegram",
            icon: Send,
            url: `https://t.me/share/url?url=${encodedUrl}&text=${encodedText}`,
            color: "hover:text-blue-500"
        },
        {
            name: "Discord",
            icon: Gamepad2,
            url: "", // Discord doesn't have a share URL, handled via onClick
            action: true, // Special flag to trigger copy
            color: "hover:text-indigo-500"
        },
        {
            name: "Reddit",
            icon: MessageSquare,
            url: `https://www.reddit.com/submit?url=${encodedUrl}&title=${encodedText}`,
            color: "hover:text-orange-500"
        },
        {
            name: "LinkedIn",
            icon: Linkedin,
            url: `https://www.linkedin.com/sharing/share-offsite/?url=${encodedUrl}`,
            color: "hover:text-blue-700"
        }
    ]

    return (
        <Popover open={isOpen} onOpenChange={setIsOpen}>
            <PopoverTrigger asChild>
                {trigger || (
                    <Button variant="outline" className={cn("neo-brutalist", className)}>
                        <Share2 className="mr-2 h-4 w-4" /> Share
                    </Button>
                )}
            </PopoverTrigger>
            <PopoverContent align="end" className="w-80 glass-panel border-2 p-0 overflow-hidden">
                <div className="p-4 border-b border-border/50 bg-background/50">
                    <h4 className="font-bold text-sm mb-3">Share Artwork</h4>
                    <div className="flex items-center gap-2">
                        <div className="flex-1 h-9 bg-secondary/10 border border-input rounded-md px-3 text-xs flex items-center text-muted-foreground truncate font-mono">
                            {shareUrl}
                        </div>
                        <Button
                            size="icon"
                            variant="secondary"
                            className="h-9 w-9 shrink-0"
                            onClick={handleCopy}
                        >
                            {copied ? <Check className="h-4 w-4 text-green-500" /> : <Copy className="h-4 w-4" />}
                            <span className="sr-only">Copy link</span>
                        </Button>
                    </div>
                </div>
                <div className="p-2 grid grid-cols-2 gap-1 bg-background/80">
                    {shareLinks.map((platform) => {
                        if (platform.action) {
                            return (
                                <button
                                    key={platform.name}
                                    onClick={() => {
                                        handleCopy()
                                        // Optional: You could show a specialized toast here
                                    }}
                                    className={cn(
                                        "flex items-center gap-3 px-3 py-2.5 text-sm font-medium rounded-md transition-colors hover:bg-secondary/20 w-full text-left",
                                        platform.color
                                    )}
                                >
                                    <platform.icon className="h-4 w-4" />
                                    {platform.name}
                                </button>
                            )
                        }
                        return (
                            <a
                                key={platform.name}
                                href={platform.url}
                                target="_blank"
                                rel="noopener noreferrer"
                                className={cn(
                                    "flex items-center gap-3 px-3 py-2.5 text-sm font-medium rounded-md transition-colors hover:bg-secondary/20",
                                    platform.color
                                )}
                                onClick={() => setIsOpen(false)}
                            >
                                <platform.icon className="h-4 w-4" />
                                {platform.name}
                            </a>
                        )
                    })}
                </div>
            </PopoverContent>
        </Popover>
    )
}