fix: upload page

This commit is contained in:
Oleg Proskurin 2025-10-26 19:13:48 +07:00
parent 349abc2071
commit a397de80e9
2 changed files with 16 additions and 8 deletions

View File

@ -1,6 +1,6 @@
'use client';
import { useState, useEffect, useRef, DragEvent, ChangeEvent } from 'react';
import { useState, useEffect, useRef, useCallback, DragEvent, ChangeEvent } from 'react';
import { useApiKey } from '@/components/shared/ApiKeyWidget/apikey-context';
import { Section } from '@/components/shared/Section';
import { CodeExamplesWidget } from '@/components/demo/CodeExamplesWidget';
@ -235,13 +235,17 @@ export default function DemoUploadPage() {
}
};
const handleDownloadMeasured = (itemId: string, downloadMs: number) => {
const handleDownloadMeasured = useCallback((itemId: string, downloadMs: number) => {
setUploadHistory((prev) =>
prev.map((item) =>
item.id === itemId ? { ...item, downloadMs } : item
)
prev.map((item) => {
// Only update if this item doesn't have downloadMs yet (prevent re-measuring)
if (item.id === itemId && item.downloadMs === undefined) {
return { ...item, downloadMs };
}
return item;
})
);
};
}, []);
const generateUploadCodeExamples = (item: UploadHistoryItem, key: string, baseUrl: string) => {
const fileName = item.originalName;

View File

@ -1,6 +1,6 @@
'use client';
import { useEffect } from 'react';
import { useEffect, useRef } from 'react';
import { ImageMetadataBar } from '../ImageMetadataBar';
import { useImageDownloadTime } from './useImageDownloadTime';
import { usePageContext } from '@/contexts/page-context';
@ -36,9 +36,13 @@ export const ImageCard = ({
measureDownloadTime ? imageUrl : null
);
// Track if we've already called onDownloadMeasured to prevent duplicate calls
const hasCalledCallback = useRef(false);
useEffect(() => {
if (downloadTime !== null && onDownloadMeasured) {
if (downloadTime !== null && onDownloadMeasured && !hasCalledCallback.current) {
onDownloadMeasured(downloadTime);
hasCalledCallback.current = true;
}
}, [downloadTime, onDownloadMeasured]);