feat: 加入塑料前端 (#262)
This commit is contained in:
56
web/src/lib/api.ts
Normal file
56
web/src/lib/api.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import type { VideoResponse, VideoInfo, VideosResponse, VideoSourcesResponse, ResetVideoResponse } from './types';
|
||||
|
||||
const BASE_URL = '/api';
|
||||
|
||||
export class ApiError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'ApiError';
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchWithAuth(url: string, options: RequestInit = {}) {
|
||||
const token = localStorage.getItem('auth_token');
|
||||
const headers = {
|
||||
...options.headers,
|
||||
'Authorization': token || ''
|
||||
};
|
||||
|
||||
const response = await fetch(url, { ...options, headers });
|
||||
if (!response.ok) {
|
||||
throw new ApiError(`API request failed: ${response.statusText}, body: ${await response.text()}`);
|
||||
}
|
||||
let { data } = await response.json();
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function getVideoSources(): Promise<VideoSourcesResponse> {
|
||||
return fetchWithAuth(`${BASE_URL}/video-sources`);
|
||||
}
|
||||
|
||||
export async function listVideos(params: {
|
||||
collection?: string;
|
||||
favorite?: string;
|
||||
submission?: string;
|
||||
watch_later?: string;
|
||||
query?: string;
|
||||
page?: number;
|
||||
page_size?: number;
|
||||
}): Promise<VideosResponse> {
|
||||
const searchParams = new URLSearchParams();
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== undefined) {
|
||||
searchParams.append(key, value.toString());
|
||||
}
|
||||
});
|
||||
return fetchWithAuth(`${BASE_URL}/videos?${searchParams.toString()}`);
|
||||
}
|
||||
|
||||
|
||||
export async function getVideo(id: number): Promise<VideoResponse> {
|
||||
return fetchWithAuth(`${BASE_URL}/videos/${id}`);
|
||||
}
|
||||
|
||||
export async function resetVideo(id: number): Promise<ResetVideoResponse> {
|
||||
return fetchWithAuth(`${BASE_URL}/videos/${id}/reset`, { method: 'POST' });
|
||||
}
|
||||
119
web/src/lib/components/VideoItem.svelte
Normal file
119
web/src/lib/components/VideoItem.svelte
Normal file
@@ -0,0 +1,119 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { Button } from '$lib/components/ui/button';
|
||||
import { Badge } from '$lib/components/ui/badge';
|
||||
import { getVideo, resetVideo } from '$lib/api';
|
||||
import { toast } from 'svelte-sonner';
|
||||
import type { VideoResponse, VideoInfo, ResetVideoResponse } from '$lib/types';
|
||||
|
||||
export let video: VideoInfo;
|
||||
export let collapseSignal: boolean = false;
|
||||
|
||||
let showDetail = false;
|
||||
let detail: VideoResponse | null = null;
|
||||
let loading = false;
|
||||
|
||||
// 定义视频和页面各状态的名称映射
|
||||
const videoStatusLabels = ['视频封面', '视频信息', 'Up 主头像', 'Up 主信息', '分 P 下载'];
|
||||
const pageStatusLabels = ['视频封面', '视频内容', '视频信息', '视频弹幕', '视频字幕'];
|
||||
|
||||
let prevCollapseSignal = collapseSignal;
|
||||
$: if (collapseSignal !== prevCollapseSignal) {
|
||||
showDetail = false;
|
||||
prevCollapseSignal = collapseSignal;
|
||||
}
|
||||
|
||||
function getVariant(status: number): 'warning' | 'success' | 'destructive' {
|
||||
if (status === 0) return 'warning';
|
||||
if (status === 7) return 'success';
|
||||
return 'destructive';
|
||||
}
|
||||
|
||||
async function toggleDetail() {
|
||||
showDetail = !showDetail;
|
||||
if (showDetail && (!detail || detail.video.id !== video.id)) {
|
||||
loading = true;
|
||||
detail = await getVideo(video.id);
|
||||
loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 修改重置函数:调用 resetVideo 后重新获取视频详情
|
||||
async function resetVideoItem() {
|
||||
loading = true;
|
||||
try {
|
||||
const res: ResetVideoResponse = await resetVideo(video.id);
|
||||
// 重置后重新加载视频详情,并更新视频信息
|
||||
const newDetail = await getVideo(video.id);
|
||||
detail = newDetail;
|
||||
video = newDetail.video;
|
||||
// 根据返回的 resetted 显示提示
|
||||
if (res.resetted) {
|
||||
toast.success('重置成功', {
|
||||
description: `已重置视频与视频的 ${res.pages.length} 条 page.`
|
||||
});
|
||||
} else {
|
||||
toast.info('重置无效', {
|
||||
description: '所有任务均成功,无需重置'
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
toast.error('重置失败', { description: `错误信息:${error}` });
|
||||
}
|
||||
loading = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="my-2 rounded border p-4">
|
||||
<div class="flex items-center justify-between">
|
||||
<div>
|
||||
<h3>{video.name}</h3>
|
||||
<div class="flex space-x-1">
|
||||
{#each video.download_status as status, i}
|
||||
<Badge variant={getVariant(status)}>
|
||||
{videoStatusLabels[i]}: {status === 0
|
||||
? '未开始'
|
||||
: status === 7
|
||||
? '已完成'
|
||||
: `失败 ${status} 次`}
|
||||
</Badge>
|
||||
{/each}
|
||||
</div>
|
||||
<p class="text-sm text-gray-500">{video.upper_name}</p>
|
||||
</div>
|
||||
<div class="flex space-x-2">
|
||||
<Button onclick={toggleDetail}>
|
||||
{showDetail ? '收起' : '展开'}
|
||||
</Button>
|
||||
<Button onclick={resetVideoItem}>重置</Button>
|
||||
</div>
|
||||
</div>
|
||||
{#if showDetail}
|
||||
{#if loading}
|
||||
<p>加载详情...</p>
|
||||
{:else if detail}
|
||||
<div class="mt-2">
|
||||
<h4 class="font-semibold">视频详情</h4>
|
||||
<div>
|
||||
{#each detail.pages as page}
|
||||
<div class="border-t py-1">
|
||||
<p>ID: {page.id} - 名称: {page.name}</p>
|
||||
<div class="flex space-x-1">
|
||||
{#each page.download_status as status, i}
|
||||
<Badge variant={getVariant(status)}>
|
||||
{pageStatusLabels[i]}: {status === 0
|
||||
? '未开始'
|
||||
: status === 7
|
||||
? '已完成'
|
||||
: `失败 ${status} 次`}
|
||||
</Badge>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
{/if}
|
||||
</div>
|
||||
18
web/src/lib/components/ui/badge/badge.svelte
Normal file
18
web/src/lib/components/ui/badge/badge.svelte
Normal file
@@ -0,0 +1,18 @@
|
||||
<script lang="ts">
|
||||
import { type Variant, badgeVariants } from "./index.js";
|
||||
import { cn } from "$lib/utils.js";
|
||||
|
||||
let className: string | undefined | null = undefined;
|
||||
export let href: string | undefined = undefined;
|
||||
export let variant: Variant = "default";
|
||||
export { className as class };
|
||||
</script>
|
||||
|
||||
<svelte:element
|
||||
this={href ? "a" : "span"}
|
||||
{href}
|
||||
class={cn(badgeVariants({ variant, className }))}
|
||||
{...$$restProps}
|
||||
>
|
||||
<slot />
|
||||
</svelte:element>
|
||||
23
web/src/lib/components/ui/badge/index.ts
Normal file
23
web/src/lib/components/ui/badge/index.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { type VariantProps, tv } from "tailwind-variants";
|
||||
export { default as Badge } from "./badge.svelte";
|
||||
|
||||
export const badgeVariants = tv({
|
||||
base: "focus:ring-ring inline-flex select-none items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-offset-2",
|
||||
variants: {
|
||||
variant: {
|
||||
default: "bg-primary text-primary-foreground hover:bg-primary/80 border-transparent",
|
||||
secondary:
|
||||
"bg-secondary text-secondary-foreground hover:bg-secondary/80 border-transparent",
|
||||
destructive:
|
||||
"bg-destructive text-destructive-foreground hover:bg-destructive/80 border-transparent",
|
||||
success: 'border-transparent bg-green-500 text-success-foreground',
|
||||
warning: 'border-transparent bg-yellow-500 text-warning-foreground',
|
||||
outline: "text-foreground",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
},
|
||||
});
|
||||
|
||||
export type Variant = VariantProps<typeof badgeVariants>["variant"];
|
||||
74
web/src/lib/components/ui/button/button.svelte
Normal file
74
web/src/lib/components/ui/button/button.svelte
Normal file
@@ -0,0 +1,74 @@
|
||||
<script lang="ts" module>
|
||||
import type { WithElementRef } from "bits-ui";
|
||||
import type { HTMLAnchorAttributes, HTMLButtonAttributes } from "svelte/elements";
|
||||
import { type VariantProps, tv } from "tailwind-variants";
|
||||
|
||||
export const buttonVariants = tv({
|
||||
base: "ring-offset-background focus-visible:ring-ring inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
|
||||
variants: {
|
||||
variant: {
|
||||
default: "bg-primary text-primary-foreground hover:bg-primary/90",
|
||||
destructive: "bg-destructive text-destructive-foreground hover:bg-destructive/90",
|
||||
outline:
|
||||
"border-input bg-background hover:bg-accent hover:text-accent-foreground border",
|
||||
secondary: "bg-secondary text-secondary-foreground hover:bg-secondary/80",
|
||||
ghost: "hover:bg-accent hover:text-accent-foreground",
|
||||
link: "text-primary underline-offset-4 hover:underline",
|
||||
},
|
||||
size: {
|
||||
default: "h-10 px-4 py-2",
|
||||
sm: "h-9 rounded-md px-3",
|
||||
lg: "h-11 rounded-md px-8",
|
||||
icon: "h-10 w-10",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
size: "default",
|
||||
},
|
||||
});
|
||||
|
||||
export type ButtonVariant = VariantProps<typeof buttonVariants>["variant"];
|
||||
export type ButtonSize = VariantProps<typeof buttonVariants>["size"];
|
||||
|
||||
export type ButtonProps = WithElementRef<HTMLButtonAttributes> &
|
||||
WithElementRef<HTMLAnchorAttributes> & {
|
||||
variant?: ButtonVariant;
|
||||
size?: ButtonSize;
|
||||
};
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import { cn } from "$lib/utils.js";
|
||||
|
||||
let {
|
||||
class: className,
|
||||
variant = "default",
|
||||
size = "default",
|
||||
ref = $bindable(null),
|
||||
href = undefined,
|
||||
type = "button",
|
||||
children,
|
||||
...restProps
|
||||
}: ButtonProps = $props();
|
||||
</script>
|
||||
|
||||
{#if href}
|
||||
<a
|
||||
bind:this={ref}
|
||||
class={cn(buttonVariants({ variant, size }), className)}
|
||||
{href}
|
||||
{...restProps}
|
||||
>
|
||||
{@render children?.()}
|
||||
</a>
|
||||
{:else}
|
||||
<button
|
||||
bind:this={ref}
|
||||
class={cn(buttonVariants({ variant, size }), className)}
|
||||
{type}
|
||||
{...restProps}
|
||||
>
|
||||
{@render children?.()}
|
||||
</button>
|
||||
{/if}
|
||||
17
web/src/lib/components/ui/button/index.ts
Normal file
17
web/src/lib/components/ui/button/index.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import Root, {
|
||||
type ButtonProps,
|
||||
type ButtonSize,
|
||||
type ButtonVariant,
|
||||
buttonVariants,
|
||||
} from "./button.svelte";
|
||||
|
||||
export {
|
||||
Root,
|
||||
type ButtonProps as Props,
|
||||
//
|
||||
Root as Button,
|
||||
buttonVariants,
|
||||
type ButtonProps,
|
||||
type ButtonSize,
|
||||
type ButtonVariant,
|
||||
};
|
||||
29
web/src/lib/components/ui/input/index.ts
Normal file
29
web/src/lib/components/ui/input/index.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import Root from "./input.svelte";
|
||||
|
||||
export type FormInputEvent<T extends Event = Event> = T & {
|
||||
currentTarget: EventTarget & HTMLInputElement;
|
||||
};
|
||||
export type InputEvents = {
|
||||
blur: FormInputEvent<FocusEvent>;
|
||||
change: FormInputEvent<Event>;
|
||||
click: FormInputEvent<MouseEvent>;
|
||||
focus: FormInputEvent<FocusEvent>;
|
||||
focusin: FormInputEvent<FocusEvent>;
|
||||
focusout: FormInputEvent<FocusEvent>;
|
||||
keydown: FormInputEvent<KeyboardEvent>;
|
||||
keypress: FormInputEvent<KeyboardEvent>;
|
||||
keyup: FormInputEvent<KeyboardEvent>;
|
||||
mouseover: FormInputEvent<MouseEvent>;
|
||||
mouseenter: FormInputEvent<MouseEvent>;
|
||||
mouseleave: FormInputEvent<MouseEvent>;
|
||||
mousemove: FormInputEvent<MouseEvent>;
|
||||
paste: FormInputEvent<ClipboardEvent>;
|
||||
input: FormInputEvent<InputEvent>;
|
||||
wheel: FormInputEvent<WheelEvent>;
|
||||
};
|
||||
|
||||
export {
|
||||
Root,
|
||||
//
|
||||
Root as Input,
|
||||
};
|
||||
42
web/src/lib/components/ui/input/input.svelte
Normal file
42
web/src/lib/components/ui/input/input.svelte
Normal file
@@ -0,0 +1,42 @@
|
||||
<script lang="ts">
|
||||
import type { HTMLInputAttributes } from "svelte/elements";
|
||||
import type { InputEvents } from "./index.js";
|
||||
import { cn } from "$lib/utils.js";
|
||||
|
||||
type $$Props = HTMLInputAttributes;
|
||||
type $$Events = InputEvents;
|
||||
|
||||
let className: $$Props["class"] = undefined;
|
||||
export let value: $$Props["value"] = undefined;
|
||||
export { className as class };
|
||||
|
||||
// Workaround for https://github.com/sveltejs/svelte/issues/9305
|
||||
// Fixed in Svelte 5, but not backported to 4.x.
|
||||
export let readonly: $$Props["readonly"] = undefined;
|
||||
</script>
|
||||
|
||||
<input
|
||||
class={cn(
|
||||
"border-input bg-background ring-offset-background placeholder:text-muted-foreground focus-visible:ring-ring flex h-10 w-full rounded-md border px-3 py-2 text-sm file:border-0 file:bg-transparent file:text-sm file:font-medium focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
|
||||
className
|
||||
)}
|
||||
bind:value
|
||||
{readonly}
|
||||
on:blur
|
||||
on:change
|
||||
on:click
|
||||
on:focus
|
||||
on:focusin
|
||||
on:focusout
|
||||
on:keydown
|
||||
on:keypress
|
||||
on:keyup
|
||||
on:mouseover
|
||||
on:mouseenter
|
||||
on:mouseleave
|
||||
on:mousemove
|
||||
on:paste
|
||||
on:input
|
||||
on:wheel|passive
|
||||
{...$$restProps}
|
||||
/>
|
||||
1
web/src/lib/components/ui/sonner/index.ts
Normal file
1
web/src/lib/components/ui/sonner/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { default as Toaster } from "./sonner.svelte";
|
||||
20
web/src/lib/components/ui/sonner/sonner.svelte
Normal file
20
web/src/lib/components/ui/sonner/sonner.svelte
Normal file
@@ -0,0 +1,20 @@
|
||||
<script lang="ts">
|
||||
import { Toaster as Sonner, type ToasterProps as SonnerProps } from "svelte-sonner";
|
||||
import { mode } from "mode-watcher";
|
||||
|
||||
type $$Props = SonnerProps;
|
||||
</script>
|
||||
|
||||
<Sonner
|
||||
theme={$mode}
|
||||
class="toaster group"
|
||||
toastOptions={{
|
||||
classes: {
|
||||
toast: "group toast group-[.toaster]:bg-background group-[.toaster]:text-foreground group-[.toaster]:border-border group-[.toaster]:shadow-lg",
|
||||
description: "group-[.toast]:text-muted-foreground",
|
||||
actionButton: "group-[.toast]:bg-primary group-[.toast]:text-primary-foreground",
|
||||
cancelButton: "group-[.toast]:bg-muted group-[.toast]:text-muted-foreground",
|
||||
},
|
||||
}}
|
||||
{...$$restProps}
|
||||
/>
|
||||
18
web/src/lib/components/ui/tabs/index.ts
Normal file
18
web/src/lib/components/ui/tabs/index.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Tabs as TabsPrimitive } from "bits-ui";
|
||||
import Content from "./tabs-content.svelte";
|
||||
import List from "./tabs-list.svelte";
|
||||
import Trigger from "./tabs-trigger.svelte";
|
||||
|
||||
const Root = TabsPrimitive.Root;
|
||||
|
||||
export {
|
||||
Root,
|
||||
Content,
|
||||
List,
|
||||
Trigger,
|
||||
//
|
||||
Root as Tabs,
|
||||
Content as TabsContent,
|
||||
List as TabsList,
|
||||
Trigger as TabsTrigger,
|
||||
};
|
||||
21
web/src/lib/components/ui/tabs/tabs-content.svelte
Normal file
21
web/src/lib/components/ui/tabs/tabs-content.svelte
Normal file
@@ -0,0 +1,21 @@
|
||||
<script lang="ts">
|
||||
import { Tabs as TabsPrimitive } from "bits-ui";
|
||||
import { cn } from "$lib/utils.js";
|
||||
|
||||
type $$Props = TabsPrimitive.ContentProps;
|
||||
|
||||
let className: $$Props["class"] = undefined;
|
||||
export let value: $$Props["value"];
|
||||
export { className as class };
|
||||
</script>
|
||||
|
||||
<TabsPrimitive.Content
|
||||
class={cn(
|
||||
"ring-offset-background focus-visible:ring-ring mt-2 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2",
|
||||
className
|
||||
)}
|
||||
{value}
|
||||
{...$$restProps}
|
||||
>
|
||||
<slot />
|
||||
</TabsPrimitive.Content>
|
||||
19
web/src/lib/components/ui/tabs/tabs-list.svelte
Normal file
19
web/src/lib/components/ui/tabs/tabs-list.svelte
Normal file
@@ -0,0 +1,19 @@
|
||||
<script lang="ts">
|
||||
import { Tabs as TabsPrimitive } from "bits-ui";
|
||||
import { cn } from "$lib/utils.js";
|
||||
|
||||
type $$Props = TabsPrimitive.ListProps;
|
||||
|
||||
let className: $$Props["class"] = undefined;
|
||||
export { className as class };
|
||||
</script>
|
||||
|
||||
<TabsPrimitive.List
|
||||
class={cn(
|
||||
"bg-muted text-muted-foreground inline-flex h-10 items-center justify-center rounded-md p-1",
|
||||
className
|
||||
)}
|
||||
{...$$restProps}
|
||||
>
|
||||
<slot />
|
||||
</TabsPrimitive.List>
|
||||
23
web/src/lib/components/ui/tabs/tabs-trigger.svelte
Normal file
23
web/src/lib/components/ui/tabs/tabs-trigger.svelte
Normal file
@@ -0,0 +1,23 @@
|
||||
<script lang="ts">
|
||||
import { Tabs as TabsPrimitive } from "bits-ui";
|
||||
import { cn } from "$lib/utils.js";
|
||||
|
||||
type $$Props = TabsPrimitive.TriggerProps;
|
||||
type $$Events = TabsPrimitive.TriggerEvents;
|
||||
|
||||
let className: $$Props["class"] = undefined;
|
||||
export let value: $$Props["value"];
|
||||
export { className as class };
|
||||
</script>
|
||||
|
||||
<TabsPrimitive.Trigger
|
||||
class={cn(
|
||||
"ring-offset-background focus-visible:ring-ring data-[state=active]:bg-background data-[state=active]:text-foreground inline-flex items-center justify-center whitespace-nowrap rounded-sm px-3 py-1.5 text-sm font-medium transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=active]:shadow-sm",
|
||||
className
|
||||
)}
|
||||
{value}
|
||||
{...$$restProps}
|
||||
on:click
|
||||
>
|
||||
<slot />
|
||||
</TabsPrimitive.Trigger>
|
||||
43
web/src/lib/types.ts
Normal file
43
web/src/lib/types.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
export interface VideoSource {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface VideoSourcesResponse {
|
||||
collection: VideoSource[];
|
||||
favorite: VideoSource[];
|
||||
submission: VideoSource[];
|
||||
watch_later: VideoSource[];
|
||||
}
|
||||
|
||||
export interface VideoInfo {
|
||||
id: number;
|
||||
name: string;
|
||||
upper_name: string;
|
||||
download_status: number[];
|
||||
}
|
||||
|
||||
export interface VideosResponse {
|
||||
videos: VideoInfo[];
|
||||
total_count: number;
|
||||
}
|
||||
|
||||
export interface PageInfo {
|
||||
id: number;
|
||||
pid: number;
|
||||
name: string;
|
||||
download_status: number[];
|
||||
}
|
||||
|
||||
export interface VideoResponse {
|
||||
video: VideoInfo;
|
||||
pages: PageInfo[];
|
||||
}
|
||||
|
||||
export interface ResetVideoResponse {
|
||||
resetted: boolean;
|
||||
video: number;
|
||||
pages: number[];
|
||||
}
|
||||
|
||||
export type VideoCategory = 'collection' | 'favorite' | 'submission' | 'watch_later';
|
||||
6
web/src/lib/utils.ts
Normal file
6
web/src/lib/utils.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { type ClassValue, clsx } from "clsx";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
Reference in New Issue
Block a user