增加弹幕缓存大小统计
This commit is contained in:
@@ -34,23 +34,23 @@ import {
|
|||||||
X,
|
X,
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { useEffect, useState } from 'react';
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
import { createPortal } from 'react-dom';
|
import { createPortal } from 'react-dom';
|
||||||
|
|
||||||
import { getAuthInfoFromBrowserCookie } from '@/lib/auth';
|
import { getAuthInfoFromBrowserCookie } from '@/lib/auth';
|
||||||
import { clearAllDanmakuCache } from '@/lib/danmaku/api';
|
import { clearAllDanmakuCache, getDanmakuCacheStats } from '@/lib/danmaku/api';
|
||||||
import { CURRENT_VERSION } from '@/lib/version';
|
import { CURRENT_VERSION } from '@/lib/version';
|
||||||
import { UpdateStatus } from '@/lib/version_check';
|
import { UpdateStatus } from '@/lib/version_check';
|
||||||
|
|
||||||
|
import { DeviceManagementPanel } from './DeviceManagementPanel';
|
||||||
|
import { DownloadManagementPanel } from './DownloadManagementPanel';
|
||||||
|
import { EmailSettingsPanel } from './EmailSettingsPanel';
|
||||||
import { FavoritesPanel } from './FavoritesPanel';
|
import { FavoritesPanel } from './FavoritesPanel';
|
||||||
import { NotificationPanel } from './NotificationPanel';
|
import { NotificationPanel } from './NotificationPanel';
|
||||||
import { OfflineDownloadPanel } from './OfflineDownloadPanel';
|
import { OfflineDownloadPanel } from './OfflineDownloadPanel';
|
||||||
import { DeviceManagementPanel } from './DeviceManagementPanel';
|
|
||||||
import { EmailSettingsPanel } from './EmailSettingsPanel';
|
|
||||||
import { PersonalCenterPanel } from './PersonalCenterPanel';
|
import { PersonalCenterPanel } from './PersonalCenterPanel';
|
||||||
import { useVersionCheck } from './VersionCheckProvider';
|
import { useVersionCheck } from './VersionCheckProvider';
|
||||||
import { VersionPanel } from './VersionPanel';
|
import { VersionPanel } from './VersionPanel';
|
||||||
import { DownloadManagementPanel } from './DownloadManagementPanel';
|
|
||||||
|
|
||||||
interface AuthInfo {
|
interface AuthInfo {
|
||||||
username?: string;
|
username?: string;
|
||||||
@@ -250,6 +250,7 @@ export const UserMenu: React.FC = () => {
|
|||||||
// 清除弹幕缓存相关状态
|
// 清除弹幕缓存相关状态
|
||||||
const [isClearingCache, setIsClearingCache] = useState(false);
|
const [isClearingCache, setIsClearingCache] = useState(false);
|
||||||
const [clearCacheMessage, setClearCacheMessage] = useState<string | null>(null);
|
const [clearCacheMessage, setClearCacheMessage] = useState<string | null>(null);
|
||||||
|
const [danmakuCacheUsage, setDanmakuCacheUsage] = useState('计算中...');
|
||||||
|
|
||||||
// 确保组件已挂载
|
// 确保组件已挂载
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -274,6 +275,22 @@ export const UserMenu: React.FC = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const formatCacheSize = useCallback((size: number) => {
|
||||||
|
if (size < 1024) return `${size} B`;
|
||||||
|
if (size < 1024 * 1024) return `${(size / 1024).toFixed(2)} KB`;
|
||||||
|
return `${(size / 1024 / 1024).toFixed(2)} MB`;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const loadDanmakuCacheUsage = useCallback(async () => {
|
||||||
|
try {
|
||||||
|
const stats = await getDanmakuCacheStats();
|
||||||
|
setDanmakuCacheUsage(formatCacheSize(stats.totalSize));
|
||||||
|
} catch (error) {
|
||||||
|
console.error('获取弹幕缓存占用失败:', error);
|
||||||
|
setDanmakuCacheUsage('获取失败');
|
||||||
|
}
|
||||||
|
}, [formatCacheSize]);
|
||||||
|
|
||||||
// 首次加载时检查未读通知数量(使用全局标记避免多个实例重复请求)
|
// 首次加载时检查未读通知数量(使用全局标记避免多个实例重复请求)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (typeof window === 'undefined') return;
|
if (typeof window === 'undefined') return;
|
||||||
@@ -304,6 +321,13 @@ export const UserMenu: React.FC = () => {
|
|||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!mounted || !isSettingsOpen || !isDanmakuSectionOpen) return;
|
||||||
|
void (async () => {
|
||||||
|
await loadDanmakuCacheUsage();
|
||||||
|
})();
|
||||||
|
}, [loadDanmakuCacheUsage, mounted, isSettingsOpen, isDanmakuSectionOpen]);
|
||||||
|
|
||||||
// 监听通知更新事件
|
// 监听通知更新事件
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleNotificationsUpdated = () => {
|
const handleNotificationsUpdated = () => {
|
||||||
@@ -1398,6 +1422,7 @@ export const UserMenu: React.FC = () => {
|
|||||||
try {
|
try {
|
||||||
await clearAllDanmakuCache();
|
await clearAllDanmakuCache();
|
||||||
setClearCacheMessage('弹幕缓存已清除成功!');
|
setClearCacheMessage('弹幕缓存已清除成功!');
|
||||||
|
setDanmakuCacheUsage('0 B');
|
||||||
console.log('弹幕缓存已清除');
|
console.log('弹幕缓存已清除');
|
||||||
|
|
||||||
// 3秒后自动清除提示
|
// 3秒后自动清除提示
|
||||||
@@ -2856,6 +2881,9 @@ export const UserMenu: React.FC = () => {
|
|||||||
<h4 className='text-sm font-medium text-gray-700 dark:text-gray-300'>
|
<h4 className='text-sm font-medium text-gray-700 dark:text-gray-300'>
|
||||||
弹幕缓存管理
|
弹幕缓存管理
|
||||||
</h4>
|
</h4>
|
||||||
|
<p className='text-xs text-gray-500 dark:text-gray-400 mt-1'>
|
||||||
|
弹幕缓存空间占用:{danmakuCacheUsage}
|
||||||
|
</p>
|
||||||
<p className='text-xs text-gray-500 dark:text-gray-400 mt-1'>
|
<p className='text-xs text-gray-500 dark:text-gray-400 mt-1'>
|
||||||
清除所有已缓存的弹幕数据
|
清除所有已缓存的弹幕数据
|
||||||
</p>
|
</p>
|
||||||
|
|||||||
@@ -428,7 +428,7 @@ export async function getDanmakuCacheStats(): Promise<{
|
|||||||
if (cursor) {
|
if (cursor) {
|
||||||
const data = cursor.value as DanmakuCacheData;
|
const data = cursor.value as DanmakuCacheData;
|
||||||
total++;
|
total++;
|
||||||
totalSize += data.comments.length;
|
totalSize += new Blob([JSON.stringify(data)]).size;
|
||||||
|
|
||||||
if (data.timestamp < expireThreshold) {
|
if (data.timestamp < expireThreshold) {
|
||||||
expired++;
|
expired++;
|
||||||
|
|||||||
Reference in New Issue
Block a user