"use client"; import { useRef, useEffect } from "react"; import { useTranslations } from "next-intl"; import { toast } from "sonner"; import { PageLayout } from "@/components/ui/PageLayout"; import { LightButton } from "@/design-system/base/button"; import { HStack } from "@/design-system/layout/stack"; import { Video, FileText, ChevronLeft, ChevronRight, RotateCcw, Pause, Play } from "lucide-react"; import { useVideoSync } from "./hooks/useVideoSync"; import { useSubtitleSync } from "./hooks/useSubtitleSync"; import { useSrtPlayerShortcuts } from "./hooks/useKeyboardShortcuts"; import { loadSubtitle } from "./utils/subtitleParser"; import { useSrtPlayerStore } from "./stores/srtPlayerStore"; import { useFileUpload } from "./hooks/useFileUpload"; import { setVideoRef } from "./stores/srtPlayerStore"; import Link from "next/link"; export default function SrtPlayerPage() { const t = useTranslations("home"); const srtT = useTranslations("srt_player"); const videoRef = useRef(null); const { uploadVideo, uploadSubtitle } = useFileUpload(); const subtitleUrl = useSrtPlayerStore((state) => state.subtitle.url); const setSubtitleData = useSrtPlayerStore((state) => state.setSubtitleData); const setSubtitleUrl = useSrtPlayerStore((state) => state.setSubtitleUrl); const setVideoUrl = useSrtPlayerStore((state) => state.setVideoUrl); const videoUrl = useSrtPlayerStore((state) => state.video.url); const subtitleData = useSrtPlayerStore((state) => state.subtitle.data); const currentIndex = useSrtPlayerStore((state) => state.subtitle.currentIndex); const isPlaying = useSrtPlayerStore((state) => state.video.isPlaying); const playbackRate = useSrtPlayerStore((state) => state.video.playbackRate); const autoPause = useSrtPlayerStore((state) => state.controls.autoPause); const togglePlayPause = useSrtPlayerStore((state) => state.togglePlayPause); const nextSubtitle = useSrtPlayerStore((state) => state.nextSubtitle); const previousSubtitle = useSrtPlayerStore((state) => state.previousSubtitle); const restartSubtitle = useSrtPlayerStore((state) => state.restartSubtitle); const setPlaybackRate = useSrtPlayerStore((state) => state.setPlaybackRate); const toggleAutoPause = useSrtPlayerStore((state) => state.toggleAutoPause); const seek = useSrtPlayerStore((state) => state.seek); useVideoSync(videoRef); useSubtitleSync(); useSrtPlayerShortcuts(); useEffect(() => { setVideoRef(videoRef); }, [videoRef]); const canPlay = !!videoUrl && !!subtitleUrl && subtitleData.length > 0; useEffect(() => { if (subtitleUrl) { loadSubtitle(subtitleUrl) .then((subtitleData) => { setSubtitleData(subtitleData); toast.success(srtT("subtitleLoadSuccess")); }) .catch((error) => { toast.error(srtT("subtitleLoadFailed") + ": " + error.message); }); } }, [srtT, subtitleUrl, setSubtitleData]); const handleVideoUpload = () => { uploadVideo((url) => { setVideoUrl(url); }, (error) => { toast.error(srtT('videoUploadFailed') + ': ' + error.message); }); }; const handleSubtitleUpload = () => { uploadSubtitle((url) => { setSubtitleUrl(url); }, (error) => { toast.error(srtT('subtitleUploadFailed') + ': ' + error.message); }); }; const handlePlaybackRateChange = () => { const rates = [0.5, 0.75, 1.0, 1.25, 1.5, 2.0]; const currentIndexRate = rates.indexOf(playbackRate); const nextIndexRate = (currentIndexRate + 1) % rates.length; setPlaybackRate(rates[nextIndexRate]); }; const currentSubtitle = currentIndex !== null ? subtitleData[currentIndex] : null; return (

{t("srtPlayer.name")}

{t("srtPlayer.description")}

); }