useFileList.ts
860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { useState, useCallback, useEffect } from "react";
import ky from "ky";
interface FileListData extends FileItem {
list: FileItem[];
}
interface FileListResponse {
data: FileListData;
}
export interface FileItem {
is_folder: boolean;
name: string;
file_type: "folder" | "file";
path: string;
parent: number;
user_id: number;
size: number;
is_deleted: boolean;
created_time: string | null;
updated_time: string;
status: boolean;
id: number;
}
export function useFileList(id: string | number) {
const [data, setData] = useState<FileListData | null>(null);
const reload = useCallback(async () => {
const response = await ky
.get(`/items/${id}/children/`)
.json<FileListResponse>();
setData(response.data);
}, [id]);
useEffect(() => {
reload();
}, [reload]);
return { data, reload };
}