Skip to content

Commit 6e77c83

Browse files
committed
Fix: Add updatedAt date to fileData
1 parent d9393c4 commit 6e77c83

File tree

9 files changed

+71
-17
lines changed

9 files changed

+71
-17
lines changed

src/components/pages/FileManagementPage.tsx

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from "react";
1+
import { useState, useEffect, useRef } from "react";
22
import { useTranslation } from "react-i18next";
33
import {
44
Upload,
@@ -33,9 +33,10 @@ export const FileManagementPage = () => {
3333
const [showRenameModal, setShowRenameModal] = useState(false);
3434
const [selectedFile, setSelectedFile] = useState<FileData | null>(null);
3535
const [newFileName, setNewFileName] = useState("");
36-
const [sortBy, setSortBy] = useState<"name" | "size" | "type">("name");
37-
const [sortDirection, setSortDirection] = useState<"asc" | "desc">("asc");
36+
const [sortBy, setSortBy] = useState<"name" | "size" | "type" | "updatedAt">("updatedAt");
37+
const [sortDirection, setSortDirection] = useState<"asc" | "desc">("desc");
3838
const [activeCategory, setActiveCategory] = useState<FileCategory>('all');
39+
const [isSortOptionsOpen, setIsSortOptionsOpen] = useState(false);
3940
const [confirmDialog, setConfirmDialog] = useState({
4041
isOpen: false,
4142
title: '',
@@ -44,6 +45,10 @@ export const FileManagementPage = () => {
4445
cancelText: '',
4546
confirmColor: 'red' as 'red' | 'blue' | 'green' | 'gray'
4647
});
48+
49+
// Add refs for sort dropdown and button
50+
const sortOptionsRef = useRef<HTMLDivElement>(null);
51+
const sortButtonRef = useRef<HTMLButtonElement>(null);
4752

4853
// Load files on mount
4954
useEffect(() => {
@@ -164,6 +169,9 @@ export const FileManagementPage = () => {
164169
comparison = a.size - b.size;
165170
} else if (sortBy === "type") {
166171
comparison = a.type.localeCompare(b.type);
172+
} else if (sortBy === "updatedAt") {
173+
// Sort by date - newest first by default
174+
comparison = new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime();
167175
}
168176
return sortDirection === "asc" ? comparison : -comparison;
169177
});
@@ -192,7 +200,8 @@ export const FileManagementPage = () => {
192200
const fileData = {
193201
name: file.name,
194202
type: file.type,
195-
size: file.size
203+
size: file.size,
204+
updatedAt: new Date()
196205
};
197206

198207
// Save to database
@@ -287,6 +296,11 @@ export const FileManagementPage = () => {
287296
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
288297
};
289298

299+
// Format date for display
300+
const formatDate = (date: Date): string => {
301+
return new Date(date).toLocaleString();
302+
};
303+
290304
// Get file icon based on type
291305
const getFileIcon = (file: FileData) => {
292306
const category = getFileCategory(file);
@@ -328,8 +342,30 @@ export const FileManagementPage = () => {
328342
}
329343
};
330344

331-
// Toggle sort direction
332-
const handleSortChange = (sortKey: "name" | "size" | "type") => {
345+
// Handle click outside to close sort options dropdown
346+
useEffect(() => {
347+
const handleClickOutside = (event: MouseEvent) => {
348+
if (
349+
sortOptionsRef.current &&
350+
!sortOptionsRef.current.contains(event.target as Node) &&
351+
sortButtonRef.current &&
352+
!sortButtonRef.current.contains(event.target as Node)
353+
) {
354+
setIsSortOptionsOpen(false);
355+
}
356+
};
357+
358+
document.addEventListener('mousedown', handleClickOutside);
359+
return () => {
360+
document.removeEventListener('mousedown', handleClickOutside);
361+
};
362+
}, []);
363+
364+
const toggleSortOptions = () => {
365+
setIsSortOptionsOpen(!isSortOptionsOpen);
366+
};
367+
368+
const handleSortChange = (sortKey: "name" | "size" | "type" | "updatedAt") => {
333369
if (sortBy === sortKey) {
334370
// Toggle direction if same key
335371
setSortDirection(sortDirection === "asc" ? "desc" : "asc");
@@ -338,6 +374,7 @@ export const FileManagementPage = () => {
338374
setSortBy(sortKey);
339375
setSortDirection("asc");
340376
}
377+
setIsSortOptionsOpen(false);
341378
};
342379

343380
const handleDeleteClick = (file: FileData) => {
@@ -475,19 +512,26 @@ export const FileManagementPage = () => {
475512
{/* Sort dropdown */}
476513
<div className="relative ml-4">
477514
<button
515+
ref={sortButtonRef}
478516
className="flex items-center px-3 py-2 rounded-md input-box"
479-
onClick={() => {
480-
const options = document.getElementById('sortOptions');
481-
options?.classList.toggle('hidden');
482-
}}
517+
onClick={toggleSortOptions}
483518
>
484519
<span className="mr-2">
485520
{t("fileManagement.sortBy")}
486521
</span>
487522
<ChevronDown size={16} />
488523
</button>
489-
<div id="sortOptions" className="absolute right-0 z-10 hidden mt-1 bg-white border rounded-md shadow-lg image-generation-popup">
524+
<div
525+
ref={sortOptionsRef}
526+
className={`absolute right-0 z-10 mt-1 bg-white border rounded-md shadow-lg image-generation-popup ${!isSortOptionsOpen ? 'hidden' : ''}`}
527+
>
490528
<div className="py-1">
529+
<button
530+
className={`flex items-center w-full px-4 py-2 ${sortBy === 'updatedAt' ? 'image-generation-provider-selected' : 'image-generation-provider-item'}`}
531+
onClick={() => handleSortChange('updatedAt')}
532+
>
533+
{t("fileManagement.updatedAt")} {sortBy === 'updatedAt' && (sortDirection === 'asc' ? '↑' : '↓')}
534+
</button>
491535
<button
492536
className={`flex items-center w-full px-4 py-2 ${sortBy === 'name' ? 'image-generation-provider-selected' : 'image-generation-provider-item'}`}
493537
onClick={() => handleSortChange('name')}
@@ -524,6 +568,7 @@ export const FileManagementPage = () => {
524568
<th className="px-4 py-3 text-left">{t("fileManagement.fileName")}</th>
525569
<th className="px-4 py-3 text-left">{t("fileManagement.fileType")}</th>
526570
<th className="px-4 py-3 text-left">{t("fileManagement.fileSize")}</th>
571+
<th className="px-4 py-3 text-left">{t("fileManagement.updatedAt")}</th>
527572
<th className="px-4 py-3 text-center">{t("fileManagement.actions")}</th>
528573
</tr>
529574
</thead>
@@ -541,6 +586,7 @@ export const FileManagementPage = () => {
541586
</td>
542587
<td className="px-4 py-3">{file.type.split("/").pop() || "Unknown"}</td>
543588
<td className="px-4 py-3">{formatFileSize(file.size)}</td>
589+
<td className="px-4 py-3">{formatDate(file.updatedAt)}</td>
544590
<td className="px-4 py-3">
545591
<div className="flex items-center justify-center space-x-2">
546592
<button

src/locales/en/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"fileName": "File Name",
6464
"fileType": "Type",
6565
"fileSize": "Size",
66+
"updatedAt": "Last Updated",
6667
"actions": "Actions",
6768
"rename": "Rename",
6869
"export": "Export",

src/locales/es/translation.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@
5656
},
5757
"fileManagement": {
5858
"title": "Gestión de Archivos",
59-
"noFiles": "Aún no hay archivos",
60-
"noFilesDescription": "Tus archivos subidos aparecerán aquí",
61-
"uploadButton": "Subir Archivo",
62-
"uploading": "Subiendo...",
59+
"noFiles": "No hay archivos todavía",
60+
"noFilesDescription": "Los archivos cargados aparecerán aquí",
61+
"uploadButton": "Cargar Archivo",
62+
"uploading": "Cargando...",
6363
"fileName": "Nombre del Archivo",
6464
"fileType": "Tipo",
6565
"fileSize": "Tamaño",
66+
"updatedAt": "Última Actualización",
6667
"actions": "Acciones",
6768
"rename": "Renombrar",
6869
"export": "Exportar",

src/locales/ja/translation.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@
5656
},
5757
"fileManagement": {
5858
"title": "ファイル管理",
59-
"noFiles": "ファイルがありません",
59+
"noFiles": "ファイルはまだありません",
6060
"noFilesDescription": "アップロードしたファイルがここに表示されます",
6161
"uploadButton": "ファイルをアップロード",
6262
"uploading": "アップロード中...",
6363
"fileName": "ファイル名",
64-
"fileType": "種類",
64+
"fileType": "タイプ",
6565
"fileSize": "サイズ",
66+
"updatedAt": "最終更新日",
6667
"actions": "操作",
6768
"rename": "名前の変更",
6869
"export": "エクスポート",

src/locales/ko/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"fileName": "파일 이름",
6464
"fileType": "유형",
6565
"fileSize": "크기",
66+
"updatedAt": "최종 수정일",
6667
"actions": "동작",
6768
"rename": "이름 변경",
6869
"export": "내보내기",

src/locales/zh-CN/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"fileName": "文件名称",
6464
"fileType": "类型",
6565
"fileSize": "大小",
66+
"updatedAt": "最后更新",
6667
"actions": "操作",
6768
"rename": "重命名",
6869
"export": "导出",

src/locales/zh-TW/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"fileName": "檔案名稱",
6464
"fileType": "類型",
6565
"fileSize": "大小",
66+
"updatedAt": "最後更新",
6667
"actions": "操作",
6768
"rename": "重新命名",
6869
"export": "匯出",

src/services/database-integration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ export class DatabaseIntegrationService {
354354
const fileId = uuidv4();
355355
const file: FileData = {
356356
fileId: fileId,
357+
updatedAt: new Date(),
357358
name: fileData.name,
358359
type: fileData.type,
359360
size: arrayBuffer.byteLength,

src/types/file.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface FileData {
22
fileId: string;
3+
updatedAt: Date;
34
name: string;
45
type: string;
56
size: number;

0 commit comments

Comments
 (0)