"use client"; import { useState, useEffect, useRef } from "react"; import { Calendar, Eye } from "lucide-react"; import { Button } from "@/components/ui/button"; import Link from "next/link"; import { getDetail } from "@/service/landing/landing"; import { BarWave } from "react-cssfx-loading"; import WaveSurfer from "wavesurfer.js"; import { getArticleDetail } from "@/service/content/content"; export default function AudioDetail({ id }: { id: number }) { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [audioLoaded, setAudioLoaded] = useState(false); const [playing, setPlaying] = useState(false); const [volume, setVolume] = useState(0.5); const waveformRef = useRef(null); const wavesurfer = useRef(null); const [selectedAudio, setSelectedAudio] = useState(0); // Ambil data audio dari API lama useEffect(() => { const fetchData = async () => { setLoading(true); const res = await getArticleDetail(id); const detail = res?.data?.data; if (detail) setData(detail); setLoading(false); }; fetchData(); }, [id]); // Inisialisasi WaveSurfer useEffect(() => { if (!data?.files?.[selectedAudio]?.url) return; // Hancurkan instance sebelumnya jika ada if (wavesurfer.current) wavesurfer.current.destroy(); const url = data.files[selectedAudio].url; const options = { container: waveformRef.current!, waveColor: "#E0E0E0", progressColor: "#FFC831", cursorColor: "#000", barWidth: 2, barRadius: 2, responsive: true, height: 80, normalize: true, partialRender: true, }; const ws = WaveSurfer.create(options); wavesurfer.current = ws; ws.load(url); ws.on("ready", () => { setAudioLoaded(true); ws.setVolume(volume); }); ws.on("finish", () => { setPlaying(false); }); return () => { ws.destroy(); }; }, [data?.files, selectedAudio]); const handlePlayPause = () => { if (!wavesurfer.current) return; wavesurfer.current.playPause(); setPlaying(!playing); }; const handleVolumeChange = (e: any) => { const vol = parseFloat(e.target.value); setVolume(vol); wavesurfer.current?.setVolume(vol); }; if (loading) return

Memuat audio...

; if (!data) return

Data tidak ditemukan

; return (
{/* 🎵 Player */}
{/* Loading sebelum waveform siap */} {!audioLoaded && ( )} {/* Waveform */}
{/* Volume control */} {audioLoaded && (
🔊
)}
{/* 🔸 Daftar File Audio */}
{data?.files?.map((file: any, index: number) => ( ))}
{/* 🗓️ Informasi Artikel */}
by {data?.uploadedBy?.userLevel?.name || "MABES POLRI"} {new Date(data.createdAt) .toLocaleString("id-ID", { day: "2-digit", month: "short", year: "numeric", hour: "2-digit", minute: "2-digit", hour12: false, timeZone: "Asia/Jakarta", }) .replace(".", ":")}{" "} WIB {data.clickCount || 0} Creator: {data.creatorName}
{/* 📝 Deskripsi */}

{data.title}

); } // "use client"; // import Image from "next/image"; // import { Calendar, Clock, Eye } from "lucide-react"; // import { Button } from "@/components/ui/button"; // import Link from "next/link"; // import { useState, useEffect } from "react"; // import { // FaFacebookF, // FaTiktok, // FaYoutube, // FaWhatsapp, // FaInstagram, // FaTwitter, // FaCheck, // FaLink, // FaShareAlt, // } from "react-icons/fa"; // import { getDetail, getArticleDetail } from "@/service/landing/landing"; // export default function AudioDetail({ id }: { id: string }) { // const [copied, setCopied] = useState(false); // const [showShareMenu, setShowShareMenu] = useState(false); // const [data, setData] = useState(null); // const [loading, setLoading] = useState(true); // const [selectedDoc, setSelectedDoc] = useState(0); // const [isLoading, setIsLoading] = useState(true); // useEffect(() => { // const timer = setTimeout(() => { // setIsLoading(false); // }, 3000); // return () => clearTimeout(timer); // }, []); // const handleCopyLink = async () => { // try { // await navigator.clipboard.writeText(window.location.href); // setCopied(true); // setTimeout(() => setCopied(false), 2000); // } catch (err) { // console.error("Failed to copy: ", err); // } // }; // const SocialItem = ({ // icon, // label, // }: { // icon: React.ReactNode; // label: string; // }) => ( //
//
{icon}
// {label} //
// ); // useEffect(() => { // const fetchDetail = async () => { // try { // setLoading(true); // // Try new Articles API first // const response = await getArticleDetail(id); // console.log("Article Detail API response:", response); // if (response?.error) { // console.error("Articles API failed, falling back to old API"); // // Fallback to old API // const fallbackResponse = await getDetail(id); // setData(fallbackResponse?.data?.data); // console.log( // "doc", // fallbackResponse?.data?.data.files[selectedDoc]?.secondaryUrl // ); // return; // } // // Handle new API response structure // const articleData = response?.data?.data; // if (articleData) { // // Transform article data to match old structure for backward compatibility // const transformedData = { // id: articleData.id, // title: articleData.title, // description: articleData.description, // createdAt: articleData.createdAt, // clickCount: articleData.viewCount, // creatorGroupLevelName: articleData.createdByName || "Unknown", // uploadedBy: { // publisher: articleData.createdByName || "MABES POLRI" // }, // files: articleData.files?.map((file: any) => ({ // id: file.id, // url: file.file_url, // fileName: file.file_name, // filePath: file.file_path, // fileThumbnail: file.file_thumbnail, // fileAlt: file.file_alt, // widthPixel: file.width_pixel, // heightPixel: file.height_pixel, // size: file.size, // downloadCount: file.download_count, // createdAt: file.created_at, // updatedAt: file.updated_at, // secondaryUrl: file.file_url, // For audio files, use same URL // ...file // })) || [], // ...articleData // }; // setData(transformedData); // console.log( // "doc", // transformedData.files[selectedDoc]?.secondaryUrl // ); // } // } catch (error) { // console.error("Error fetching detail:", error); // // Try fallback to old API if new API fails // try { // const fallbackResponse = await getDetail(id); // setData(fallbackResponse?.data?.data); // console.log( // "doc", // fallbackResponse?.data?.data.files[selectedDoc]?.secondaryUrl // ); // } catch (fallbackError) { // console.error("Fallback API also failed:", fallbackError); // } // } finally { // setLoading(false); // } // }; // if (id) fetchDetail(); // }, [id]); // if (loading) { // return ( //
//

Loading...

//
// ); // } // if (!data) { // return ( //
//

Data tidak ditemukan

//
// ); // } // return ( //
//
// //
//
// {data?.files?.map((file: any, index: number) => ( // // ))} //
//
//
// // by {data?.uploadedBy?.publisher || "MABES POLRI"} // // // // {new Date(data.createdAt) // .toLocaleString("id-ID", { // day: "2-digit", // month: "short", // year: "numeric", // hour: "2-digit", // minute: "2-digit", // hour12: false, // timeZone: "Asia/Jakarta", // }) // .replace(".", ":")}{" "} // WIB // // {/* // // {data.time || "-"} // */} // // // {data.clickCount || 0} // // // {" "} // Creator: {data.creatorGroupLevelName} // //
//
//
// {/* Sidebar actions */} //
//
// // COPY LINK //
//
// // SHARE // {showShareMenu && ( //
// } label="Facebook" /> // } label="TikTok" /> // } label="YouTube" /> // } label="WhatsApp" /> // } label="Instagram" /> // } label="Twitter" /> //
// )} //
//
// // // COMMENT // //
//
// {/* Content */} //
//

{data.title}

//
//

{data.description}

//
// {/* Actions bawah */} //
//
// // COPY LINK //
//
// // SHARE //
//
// // // COMMENT // //
//
//
//
//
// ); // }