'use client';
import { useState } from 'react';
import { ShareModal } from './ShareModal';
import type { ShareType, CreateShareResponse } from '@/types/api-types';
interface ShareButtonProps {
type: ShareType;
targetId: string;
title: string;
className?: string;
}
export function ShareButton({ type, targetId, title, className = '' }: ShareButtonProps) {
const [isModalOpen, setIsModalOpen] = useState(false);
const [shareUrl, setShareUrl] = useState('');
const [isLoading, setIsLoading] = useState(false);
const handleShare = async () => {
if (shareUrl) {
setIsModalOpen(true);
return;
}
setIsLoading(true);
try {
const endpoint = type === 'SONG'
? `/api/share/song/${targetId}`
: type === 'PLAYLIST'
? `/api/share/playlist/${targetId}`
: `/api/share/album/${targetId}`;
const res = await fetch(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
});
if (!res.ok) throw new Error('Failed to create share link');
const data: CreateShareResponse = await res.json();
setShareUrl(data.shareUrl);
setIsModalOpen(true);
} catch (err) {
console.error('Share error:', err);
} finally {
setIsLoading(false);
}
};
return (
<>
setIsModalOpen(false)}
shareUrl={shareUrl}
title={title}
type={type}
/>
>
);
}