refactor: 企业级目录重构与播放/关于页体验升级
- 按 app / shared / entities / features 分层,路由懒加载并抽出布局组件 - 请求层改为可插拔拦截器,统一错误类型、超时取消与端点拆分 - 抽取播放器、骨架屏、网格等复用组件,点赞收藏接入统一交互反馈 - 关于页重做为无边框极光风格,补充 iOS / Google Play 下载入口 - 修复播放页滚轮切集吞事件、页面误滚动、后台持续播放与声音记忆问题 - 语言按本地偏好与 IP 识别,国旗改为内联 SVG - 清理未使用的旧站点文件,补充 ESLint 配置
This commit is contained in:
1 parent
8dd5283892
commit
ecfbd3b8b5
90 files changed
+4535
-886
No files matched your search
@@ -0,0 +1,56 @@
|
||||
// Author: fruit.com.cn
|
||||
import { useState } from 'react';
|
||||
import { engagementApi } from '../api/engagement';
|
||||
import { useMutation } from './useMutation';
|
||||
|
||||
/**
|
||||
* 点赞 / 收藏状态与交互,Shorts 与播放页共用。
|
||||
* likeTargetType: 'EPISODE' | 'DRAMA'
|
||||
*/
|
||||
export function useEngagement({ likeTargetType, likeTargetId, dramaId, initialLikeCount = 0, initialSaveCount = 0 }) {
|
||||
const [liked, setLiked] = useState(false);
|
||||
const [likeCount, setLikeCount] = useState(Number(initialLikeCount) || 0);
|
||||
const [saved, setSaved] = useState(false);
|
||||
const [saveCount, setSaveCount] = useState(Number(initialSaveCount) || 0);
|
||||
const likeMutation = useMutation(() => engagementApi.toggleLike(likeTargetType, likeTargetId));
|
||||
const saveMutation = useMutation(() => engagementApi.toggleFavorite(dramaId));
|
||||
|
||||
const toggleLike = async () => {
|
||||
try {
|
||||
const result = await likeMutation.run();
|
||||
setLiked(Boolean(result?.liked));
|
||||
setLikeCount(Number(result?.count) || 0);
|
||||
} catch {
|
||||
// 交互失败时保持原状态,避免误导用户
|
||||
}
|
||||
};
|
||||
|
||||
const toggleSave = async () => {
|
||||
try {
|
||||
const next = await saveMutation.run();
|
||||
setSaved(Boolean(next));
|
||||
setSaveCount((count) => Math.max(0, count + (next ? 1 : -1)));
|
||||
} catch {
|
||||
// 同上
|
||||
}
|
||||
};
|
||||
|
||||
const syncStats = (stats) => {
|
||||
if (!stats) return;
|
||||
setLiked(Boolean(stats.liked));
|
||||
setLikeCount(Number(stats.likeCount) || 0);
|
||||
setSaved(Boolean(stats.favorited));
|
||||
setSaveCount(Number(stats.favoriteCount) || 0);
|
||||
};
|
||||
|
||||
return {
|
||||
liked,
|
||||
likeCount,
|
||||
saved,
|
||||
saveCount,
|
||||
toggleLike,
|
||||
toggleSave,
|
||||
syncStats,
|
||||
pending: likeMutation.pending || saveMutation.pending,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Author: fruit.com.cn
|
||||
import { useState } from 'react';
|
||||
|
||||
const FOLLOW_KEY = 'ymb.shorts.following';
|
||||
|
||||
function readFollowedChannels() {
|
||||
try {
|
||||
const stored = JSON.parse(window.localStorage.getItem(FOLLOW_KEY) || '[]');
|
||||
return Array.isArray(stored) ? stored : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export function useFollowedChannels() {
|
||||
const [channels, setChannels] = useState(readFollowedChannels);
|
||||
const toggle = (channel) => {
|
||||
setChannels((prev) => {
|
||||
const next = prev.includes(channel) ? prev.filter((item) => item !== channel) : [...prev, channel];
|
||||
try {
|
||||
window.localStorage.setItem(FOLLOW_KEY, JSON.stringify(next));
|
||||
} catch (error) {
|
||||
console.error('YMB FOLLOW STORAGE FAILED', error);
|
||||
}
|
||||
return next;
|
||||
});
|
||||
};
|
||||
return { channels, toggle };
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Author: fruit.com.cn
|
||||
import { useCallback, useRef, useState } from 'react';
|
||||
|
||||
/** 统一写操作 hook:暴露 pending / error,run 失败会抛错由调用方决定提示方式。 */
|
||||
export function useMutation(mutator) {
|
||||
const [state, setState] = useState({ pending: false, error: null });
|
||||
const mutatorRef = useRef(mutator);
|
||||
mutatorRef.current = mutator;
|
||||
|
||||
const run = useCallback(async (...args) => {
|
||||
setState({ pending: true, error: null });
|
||||
try {
|
||||
const data = await mutatorRef.current(...args);
|
||||
setState({ pending: false, error: null });
|
||||
return data;
|
||||
} catch (error) {
|
||||
setState({ pending: false, error });
|
||||
throw error;
|
||||
}
|
||||
}, []);
|
||||
|
||||
return { ...state, run };
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// Author: fruit.com.cn
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { isCanceled } from '../api';
|
||||
|
||||
/**
|
||||
* 统一查询 hook:支持取消、错误对象、重试(refetch)与禁用。
|
||||
* fetcher 接收 { signal },应把 signal 透传给 api 方法。
|
||||
*/
|
||||
export function useRequest(fetcher, deps = [], { enabled = true, initialData = null } = {}) {
|
||||
const [reloadKey, setReloadKey] = useState(0);
|
||||
const [state, setState] = useState({ data: initialData, loading: enabled, error: null });
|
||||
const fetcherRef = useRef(fetcher);
|
||||
fetcherRef.current = fetcher;
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled) {
|
||||
setState({ data: initialData, loading: false, error: null });
|
||||
return undefined;
|
||||
}
|
||||
const controller = new AbortController();
|
||||
let active = true;
|
||||
setState((prev) => ({ ...prev, loading: true, error: null }));
|
||||
Promise.resolve()
|
||||
.then(() => fetcherRef.current({ signal: controller.signal }))
|
||||
.then((data) => {
|
||||
if (active) setState({ data, loading: false, error: null });
|
||||
})
|
||||
.catch((error) => {
|
||||
if (!active || isCanceled(error)) return;
|
||||
setState({ data: initialData, loading: false, error });
|
||||
});
|
||||
return () => {
|
||||
active = false;
|
||||
controller.abort();
|
||||
};
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [...deps, enabled, reloadKey]);
|
||||
|
||||
const refetch = useCallback(() => setReloadKey((key) => key + 1), []);
|
||||
return { ...state, refetch };
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Author: fruit.com.cn
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
|
||||
export function useToast(duration = 2000) {
|
||||
const [message, setMessage] = useState('');
|
||||
const timerRef = useRef(null);
|
||||
|
||||
const show = useCallback((text) => {
|
||||
setMessage(text);
|
||||
if (timerRef.current) window.clearTimeout(timerRef.current);
|
||||
timerRef.current = window.setTimeout(() => setMessage(''), duration);
|
||||
}, [duration]);
|
||||
|
||||
const hide = useCallback(() => {
|
||||
if (timerRef.current) window.clearTimeout(timerRef.current);
|
||||
setMessage('');
|
||||
}, []);
|
||||
|
||||
useEffect(() => () => {
|
||||
if (timerRef.current) window.clearTimeout(timerRef.current);
|
||||
}, []);
|
||||
|
||||
return { message, show, hide };
|
||||
}
|
||||
Reference in new issue
Block a user