add viewport calculate for fileName cut

This commit is contained in:
smanylov
2026-03-05 16:44:33 +07:00
parent 6d6d0e102b
commit b79d8b8879
4 changed files with 78 additions and 27 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "no-copy-frontend", "name": "no-copy-frontend",
"version": "0.51.0", "version": "0.52.0",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "next dev -p 2999", "dev": "next dev -p 2999",
@@ -26,6 +26,7 @@ import { FileTypeIcon } from '@/app/components/FileTypeIcon';
import { fetchReferralUserStats } from '@/app/actions/referralsActions'; import { fetchReferralUserStats } from '@/app/actions/referralsActions';
import { MonitoringDropDown } from '@/app/components/tanstak-table/MonitoringDropDown'; import { MonitoringDropDown } from '@/app/components/tanstak-table/MonitoringDropDown';
import { formatDate, formatDateTime } from '@/app/lib/formatDate'; import { formatDate, formatDateTime } from '@/app/lib/formatDate';
import { useViewport } from '@/app/hooks/useViewport';
type FileType = 'image' | 'video' | 'audio'; type FileType = 'image' | 'video' | 'audio';
@@ -59,30 +60,6 @@ type ApiResponse = {
files?: ApiFile[]; files?: ApiFile[];
}; };
const cutFileName = (fileName: string) => {
const MAX_FILE_NAME_LENGTH = 26;
const lastDotIndex = fileName.lastIndexOf('.');
if (lastDotIndex <= 0) {
return fileName.length > MAX_FILE_NAME_LENGTH ? fileName.substring(0, MAX_FILE_NAME_LENGTH - 3) + '...' : fileName;
}
const nameWithoutExtension = fileName.substring(0, lastDotIndex);
const extension = fileName.substring(lastDotIndex);
if (fileName.length <= MAX_FILE_NAME_LENGTH) {
return fileName;
}
const maxNameLength = MAX_FILE_NAME_LENGTH - extension.length - 3;
if (maxNameLength <= 0) {
return '...' /* + extension */;
}
return nameWithoutExtension.substring(0, maxNameLength) + '...' /* + extension */;
}
const cutFileExtension = (fileName: string) => { const cutFileExtension = (fileName: string) => {
const lastDotIndex = fileName.lastIndexOf('.'); const lastDotIndex = fileName.lastIndexOf('.');
const extension = fileName.substring(lastDotIndex + 1); const extension = fileName.substring(lastDotIndex + 1);
@@ -156,6 +133,38 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
const t = useTranslations("Global"); const t = useTranslations("Global");
const locale = useLocale(); const locale = useLocale();
const viewport = useViewport();
const getMaxLengthByWidth = (): number => {
if (viewport.device === 'MOBILE') return 26;
if (viewport.device === 'TABLET') return 32;
if (viewport.device === 'DESKTOP') return 64;
return 100;
};
const cutFileName = (fileName: string) => {
const MAX_FILE_NAME_LENGTH = getMaxLengthByWidth();
const lastDotIndex = fileName.lastIndexOf('.');
if (lastDotIndex <= 0) {
return fileName.length > MAX_FILE_NAME_LENGTH ? fileName.substring(0, MAX_FILE_NAME_LENGTH - 3) + '...' : fileName;
}
const nameWithoutExtension = fileName.substring(0, lastDotIndex);
const extension = fileName.substring(lastDotIndex);
if (fileName.length <= MAX_FILE_NAME_LENGTH) {
return fileName;
}
const maxNameLength = MAX_FILE_NAME_LENGTH - extension.length - 3;
if (maxNameLength <= 0) {
return '...' /* + extension */;
}
return nameWithoutExtension.substring(0, maxNameLength) + '...' /* + extension */;
}
// Определение колонок // Определение колонок
const columns = useMemo<ColumnDef<FileItem>[]>( const columns = useMemo<ColumnDef<FileItem>[]>(
@@ -223,7 +232,7 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
</div> </div>
), ),
cell: ({ row }) => ( cell: ({ row }) => (
<div className="flex items-center space-x-2 table-item"> <div className="flex items-center space-x-2 table-item table-item-file-name">
<FileTypeIcon type={row.original.fileType} /> <FileTypeIcon type={row.original.fileType} />
<div> <div>
<div className="font-medium w-full truncate" title={row.original.fileName}> <div className="font-medium w-full truncate" title={row.original.fileName}>
@@ -452,7 +461,7 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
enableColumnFilter: false, enableColumnFilter: false,
}, },
], ],
[] [viewport.device]
); );
const handleView = async (file: FileItem) => { const handleView = async (file: FileItem) => {
+36
View File
@@ -0,0 +1,36 @@
'use client'
import { useState, useEffect } from 'react'
export const MOBILE = 'MOBILE'
export const TABLET = 'TABLET'
export const DESKTOP = 'DESKTOP'
const getDevice = (width: number) => {
if (width < 768) return MOBILE
else if (width < 992) return TABLET
else return DESKTOP
}
export function useViewport() {
const [viewport, setViewport] = useState({
width: 0,
device: 'DESKTOP'
});
useEffect(() => {
const handleResize = () => {
setViewport({
width: window.innerWidth,
device: getDevice(window.innerWidth)
});
};
handleResize();
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, []);
return viewport;
}
+6
View File
@@ -803,6 +803,11 @@
&:has(.table-item-id) { &:has(.table-item-id) {
width: 20px; width: 20px;
} }
&:has(.table-item-file-name) {
max-width: 300px;
overflow: hidden;
}
} }
.table-item { .table-item {
@@ -3570,6 +3575,7 @@
padding: 0; padding: 0;
overflow: hidden; overflow: hidden;
min-width: 400px; min-width: 400px;
width: 100%;
@media (max-width: 470px) { @media (max-width: 470px) {
min-width: auto; min-width: auto;