File size: 4,685 Bytes
3c56493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<script lang="ts">
	import { Share2, Link, Copy, Check, Code } from 'lucide-svelte';
	import { fade, scale } from 'svelte/transition';
	import { onMount } from 'svelte';
	import { page } from '$app/stores';

	interface Props {
		type: 'track' | 'album' | 'artist' | 'playlist';
		id: string | number;
		title?: string;
		size?: number;
		iconOnly?: boolean;
		variant?: 'ghost' | 'primary' | 'secondary';
	}

	let { 
		type, 
		id, 
		title = 'Share', 
		size = 20, 
		iconOnly = false,
		variant = 'ghost'
	}: Props = $props();

	let showMenu = $state(false);
	let copied = $state(false);
	let menuRef: HTMLDivElement | null = null;
	let buttonRef: HTMLButtonElement | null = null;

	function getLongLink() {
		return `${$page.url.protocol}://${$page.url.host}/${type}/${id}`;
	}

	function getEmbedUrl() {
		return `${$page.url.protocol}://${$page.url.host}/embed/${type}/${id}`;
	}

	function getShortLink() {
		const prefixMap = {
			track: 't',
			album: 'al',
			artist: 'ar',
			playlist: 'p'
		};
		return `https://okiw.me/${prefixMap[type]}/${id}`;
	}

	function getEmbedCode() {
        if (type === "track") return `<iframe src="${getEmbedUrl()}" width="100%" height="150" style="border:none; overflow:hidden; border-radius: 0.5em;" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture"></iframe>`;
		return `<iframe src="${getEmbedUrl()}" width="100%" height="450" style="border:none; overflow:hidden; border-radius: 0.5em;" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture"></iframe>`;
	}

	async function copyToClipboard(text: string) {
		try {
			if (navigator.clipboard && navigator.clipboard.writeText) {
				await navigator.clipboard.writeText(text);
			} else {
				// Fallback for non-secure contexts
				const textArea = document.createElement('textarea');
				textArea.value = text;
				textArea.style.position = 'fixed';
				textArea.style.left = '-9999px';
				textArea.style.top = '0';
				document.body.appendChild(textArea);
				textArea.focus();
				textArea.select();
				try {
					document.execCommand('copy');
				} catch (err) {
					console.error('Fallback: Oops, unable to copy', err);
					throw err;
				}
				document.body.removeChild(textArea);
			}
			copied = true;
			showMenu = false;
			setTimeout(() => {
				copied = false;
			}, 2000);
		} catch (err) {
			console.error('Failed to copy:', err);
		}
	}

	function handleClickOutside(event: MouseEvent) {
		if (showMenu && 
			menuRef && 
			!menuRef.contains(event.target as Node) && 
			buttonRef && 
			!buttonRef.contains(event.target as Node)) {
			showMenu = false;
		}
	}

	onMount(() => {
		document.addEventListener('click', handleClickOutside);
		return () => {
			document.removeEventListener('click', handleClickOutside);
		};
	});

	const variantClasses = {
		ghost: 'text-gray-400 hover:text-white hover:bg-white/10',
		primary: 'bg-blue-600 text-white hover:bg-blue-700',
		secondary: 'bg-gray-800 text-white hover:bg-gray-700'
	};
</script>

<div class="relative inline-block">
	<button
		bind:this={buttonRef}
		class="flex items-center gap-2 rounded-full transition-colors {variantClasses[variant]} {iconOnly ? 'p-2' : 'px-4 py-2'}"
		onclick={(e) => {
			e.stopPropagation();
			showMenu = !showMenu;
		}}
		{title}
		aria-label={title}
		aria-haspopup="true"
		aria-expanded={showMenu}
	>
		{#if copied && iconOnly}
			<Check size={size} class="text-green-500" />
		{:else}
			<Share2 size={size} />
		{/if}
		{#if !iconOnly}
			<span>{copied ? 'Copied!' : 'Share'}</span>
		{/if}
	</button>

	{#if showMenu}
		<div
			bind:this={menuRef}
			transition:scale={{ duration: 100, start: 0.95 }}
			class="absolute right-0 top-full z-50 mt-2 w-48 origin-top-right rounded-lg border border-white/10 bg-gray-900 p-1 shadow-xl backdrop-blur-xl"
		>
			<button
				class="flex w-full items-center gap-2 rounded px-3 py-2 text-sm text-gray-300 hover:bg-white/10 hover:text-white"
				onclick={(e) => {
					e.stopPropagation();
					copyToClipboard(getLongLink());
				}}
			>
				<Link size={16} />
				Copy Link
			</button>
			<button
				class="flex w-full items-center gap-2 rounded px-3 py-2 text-sm text-gray-300 hover:bg-white/10 hover:text-white"
				onclick={(e) => {
					e.stopPropagation();
					copyToClipboard(getShortLink());
				}}
			>
				<Copy size={16} />
				Copy Short Link
			</button>
			<button
				class="flex w-full items-center gap-2 rounded px-3 py-2 text-sm text-gray-300 hover:bg-white/10 hover:text-white"
				onclick={(e) => {
					e.stopPropagation();
					copyToClipboard(getEmbedCode());
				}}
			>
				<Code size={16} />
				Copy Embed Code
			</button>
		</div>
	{/if}
</div>