Files
no-copy-frontend/src/app/ui/marking-page/protection-summary.tsx
T

101 lines
2.5 KiB
TypeScript
Raw Normal View History

2025-12-16 15:03:32 +07:00
'use client'
import { useTranslations } from 'next-intl';
2025-12-29 16:08:38 +07:00
import { useQuery } from '@tanstack/react-query';
import { getUserFilesData } from '@/app/actions/fileEntity';
2026-01-07 13:03:50 +07:00
import { getUserFilesInfo } from '@/app/actions/action';
import { convertBytes } from '@/app/lib/convertBytes';
2025-12-29 16:08:38 +07:00
type FilesInfo = {
2026-01-07 13:03:50 +07:00
totalSize: number;
2025-12-29 16:08:38 +07:00
totalCount: number;
};
2025-12-16 15:03:32 +07:00
2026-01-07 13:03:50 +07:00
export default function ProtectionSummary({ fileType }: { fileType: string }) {
2025-12-16 15:03:32 +07:00
const t = useTranslations('Global');
2026-01-07 13:03:50 +07:00
console.log(fileType);
const getFileTypeFields = (fileType: string): { totalSize: string, totalCount: string } => {
switch (fileType) {
case 'image':
return {
totalSize: 'images_size',
totalCount: 'images_quantity'
};
case 'video':
return {
totalSize: 'videos_size',
totalCount: 'videos_quantity'
};
case 'audio':
return {
totalSize: 'audios_size',
totalCount: 'audios_quantity'
};
default:
return {
totalSize: 'all_files_size',
totalCount: 'all_files_quantity'
};
}
};
2025-12-16 15:03:32 +07:00
2025-12-29 16:08:38 +07:00
const {
data: filesInfo,
isLoading,
isError,
error,
} = useQuery<{
2026-01-07 13:03:50 +07:00
all_files_size: number,
all_files_quantity: number
2025-12-29 16:08:38 +07:00
}, Error, FilesInfo>({
2026-01-07 13:03:50 +07:00
queryKey: ['userFilesInfo'],
queryFn: getUserFilesInfo,
2025-12-29 16:08:38 +07:00
select: (data): FilesInfo => {
if (!data) {
return {
2026-01-07 13:03:50 +07:00
totalSize: 0,
2025-12-29 16:08:38 +07:00
totalCount: 0
}
}
2026-01-07 13:03:50 +07:00
const fields = getFileTypeFields(fileType);
2025-12-29 16:08:38 +07:00
return {
2026-01-07 13:03:50 +07:00
totalSize: data[fields.totalSize as keyof typeof data],
totalCount: data[fields.totalCount as keyof typeof data]
2025-12-29 16:08:38 +07:00
}
2026-01-07 13:03:50 +07:00
}
2025-12-29 16:08:38 +07:00
});
2025-12-05 16:19:13 +07:00
return (
<div className="protection-overview grow">
2025-12-16 15:03:32 +07:00
<h3>{t('protecting-your-content')}</h3>
<p>{t('current-status-of')}</p>
<div className="protection-stats">
2025-12-16 15:03:32 +07:00
<div className="protection-stat total-files">
2025-12-29 16:08:38 +07:00
<div className="protection-stat-value">{filesInfo?.totalCount ? filesInfo?.totalCount : 0}</div>
2026-01-07 14:44:31 +07:00
<div className="protection-stat-label">
{t('total-files')}
<br />
({t(`${fileType}s`)})
</div>
2025-12-05 16:19:13 +07:00
</div>
2025-12-16 15:03:32 +07:00
<div className="protection-stat total-checks">
<div className="protection-stat-value">0</div>
<div className="protection-stat-label">{t('check')}</div>
<div className="protection-stat-value">0</div>
<div className="protection-stat-label">{t('violations')}</div>
2025-12-05 16:19:13 +07:00
</div>
<div className="protection-stat total-usage">
2026-01-07 13:03:50 +07:00
<div className="protection-stat-value">{filesInfo?.totalSize ? convertBytes(filesInfo?.totalSize) : 0} / 0</div>
<div className="protection-stat-label">
{t('disk-space-used')}
2025-12-16 15:03:32 +07:00
</div>
</div>
2025-12-05 16:19:13 +07:00
</div>
</div>
)
}