import type { Route } from "./+types/info"; import { treaty } from "@elysiajs/eden"; import type { App } from "@elysia/src"; import { useEffect, useState } from "react"; import { Skeleton } from "@/components/ui/skeleton"; import { TriangleAlert } from "lucide-react"; const app = treaty(import.meta.env.VITE_API_URL!); type SongInfo = Awaited["info"]["get"]>>["data"]; type SongInfoError = Awaited["info"]["get"]>>["error"]; export async function clientLoader({ params }: Route.LoaderArgs) { return { id: params.id }; } export default function SongInfo({ loaderData }: Route.ComponentProps) { const [data, setData] = useState(null); const [error, setError] = useState(null); useEffect(() => { (async () => { const { data, error } = await app.song({ id: loaderData.id }).info.get(); if (error) { console.log(error); setError(error); return; } setData(data); })(); }, []); if (!data && !error) { return (
); } if (error) { return (

出错了

状态码:{error.status}

{error.value.message && (

错误信息
{error.value.message}

)}
); } const formatDuration = (duration: number) => { const minutes = Math.floor(duration / 60); const seconds = duration % 60; return `${minutes}:${seconds}`; }; const songNameOnChange = async (e: React.FocusEvent) => { const name = e.target.textContent; await app.song({ id: loaderData.id }).info.patch({ name: name || undefined }); }; return (
{data!.cover && ( )}

{data!.name ? data!.name : "未知歌曲名"}

{data!.duration ? formatDuration(data!.duration) : "未知时长"} {data!.producer ? data!.producer : "未知P主"}
); }