реактивное отображение скелетонов вместо копирования

This commit is contained in:
Nastya 2025-06-10 23:04:01 +03:00
parent c396752ce6
commit a8d6db9f2d

@ -5,8 +5,8 @@ import { useCurrentQuiz } from "@/stores/quizes/hooks";
import { InfoPopover } from "@/ui_kit/InfoPopover";
import TooltipClickInfo from "@/ui_kit/Toolbars/TooltipClickInfo";
import { useDomainDefine } from "@/utils/hooks/useDomainDefine";
import { IconButton, ListItem, Typography, useTheme } from "@mui/material";
import { useMemo } from "react";
import { IconButton, ListItem, Skeleton, Typography, useTheme } from "@mui/material";
import { useEffect, useMemo, useState } from "react";
interface AuditoryLinkProps {
item: AuditoryItem;
@ -20,16 +20,66 @@ export const AuditoryLink = ({ utmParams, item, index, onDelete }: AuditoryLinkP
const quiz = useCurrentQuiz();
const { isTestServer } = useDomainDefine();
const canCopy = useMemo(() => {
if (!item.created_at) return true;
const getCreatedTime = (timestamp: number) => {
// Если timestamp в секундах (10 цифр)
if (timestamp.toString().length === 10) {
return new Date(timestamp * 1000).getTime();
}
// Если timestamp в миллисекундах (13 цифр)
return new Date(timestamp).getTime();
};
const [isLoading, setIsLoading] = useState(() => {
if (!item.created_at) return false;
const now = new Date().getTime();
const created = new Date(item.created_at).getTime();
const created = getCreatedTime(item.created_at);
const diffInMinutes = (now - created) / (1000 * 60);
return diffInMinutes >= 5;
console.log('Initial state:', {
created_at: item.created_at,
format: item.created_at.toString().length === 10 ? 'seconds' : 'milliseconds',
now,
created,
diffInMinutes,
isLoading: diffInMinutes < 2
});
return diffInMinutes < 2;
});
useEffect(() => {
if (!item.created_at) return;
const now = new Date().getTime();
const created = getCreatedTime(item.created_at);
const diffInMinutes = (now - created) / (1000 * 60);
console.log('Effect check:', {
created_at: item.created_at,
format: item.created_at.toString().length === 10 ? 'seconds' : 'milliseconds',
now,
created,
diffInMinutes,
timeDiff: now - created
});
if (now - created < 1000) {
console.log('Setting loading to true for new link');
setIsLoading(true);
}
if (diffInMinutes < 2) {
const timeLeft = Math.ceil((2 - diffInMinutes) * 60 * 1000);
console.log('Setting timer for:', timeLeft, 'ms');
const timer = setTimeout(() => {
console.log('Timer finished, setting loading to false');
setIsLoading(false);
}, timeLeft);
return () => clearTimeout(timer);
}
}, [item.created_at]);
const handleCopy = (text: string) => {
if (!canCopy) return;
if (isLoading) return;
navigator.clipboard.writeText(text);
};
@ -77,21 +127,32 @@ export const AuditoryLink = ({ utmParams, item, index, onDelete }: AuditoryLinkP
<IconButton edge="end" aria-label="info" sx={{ color: theme.palette.brightPurple.main, p: 0, width: 18, height: 18 }}>
<TooltipClickInfo title={`Пол: ${item.sex === 0 ? "женский" : item.sex === 1 ? "мужской" : "оба"} \n Возраст: ${item.age}`} />
</IconButton>
<IconButton
edge="end"
aria-label="copy"
disabled={!canCopy}
sx={{
color: canCopy ? theme.palette.brightPurple.main : theme.palette.grey[400],
p: 0,
width: 18,
height: 18,
marginRight: "-2px"
}}
onClick={() => handleCopy(linkText)}
>
<CopyIcon color={canCopy ? theme.palette.brightPurple.main : theme.palette.grey[400]} />
</IconButton>
{isLoading ? (
<Skeleton
variant="circular"
width={18}
height={18}
sx={{
bgcolor: theme.palette.grey[400],
marginRight: "-2px"
}}
/>
) : (
<IconButton
edge="end"
aria-label="copy"
sx={{
color: theme.palette.brightPurple.main,
p: 0,
width: 18,
height: 18,
marginRight: "-2px"
}}
onClick={() => handleCopy(linkText)}
>
<CopyIcon color={theme.palette.brightPurple.main} />
</IconButton>
)}
</>
}
>