FileList.tsx
3.56 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import React, { useCallback } from "react";
import { Table, message, Button } from "antd";
import { ColumnsType } from "antd/lib/table";
import filesize from "filesize";
import { useParams } from "react-router-dom";
import { useFileList, FileItem } from "./useFileList";
import { useApi } from "util/useApi";
import { FileListItem } from "./FileListItem";
import { FileItemActions } from "./FileItemActions";
import styles from "./FileList.module.scss";
export function FileList() {
const id = useParams<{ id: string }>().id;
const { data, reload } = useFileList(id);
const api = useApi();
const handleMove = useCallback(
async (id: number, to: number) => {
try {
const body = new URLSearchParams();
body.set("parent", to.toString(10));
await api.post(`/items/${id}/move/`, { body });
await reload();
message.info("이동되었습니다");
} catch {
message.error("파일 이동에 실패했습니다");
}
},
[api, reload]
);
const handleCopy = useCallback(
async (id: number, to: number) => {
try {
const body = new URLSearchParams();
body.set("parent", to.toString(10));
await api.post(`/items/${id}/copy/`, { body });
await reload();
message.info("복사되었습니다");
} catch {
message.error("파일 복사에 실패했습니다");
}
},
[api, reload]
);
const handleDelete = useCallback(
async (id: number) => {
try {
await api.delete(`/items/${id}/`);
await reload();
message.info("삭제되었습니다");
} catch {
message.error("파일 삭제에 실패했습니다");
}
},
[api, reload]
);
if (!data) {
return null;
}
const list = [...data.list].sort((itemA, itemB) =>
itemA.is_folder === itemB.is_folder ? 0 : itemA.is_folder ? -1 : 1
);
if (data.parent !== null) {
list.unshift(({
id: data.parent,
is_folder: true,
name: "..",
file_type: "folder",
} as unknown) as FileItem);
}
return (
<div>
<div className={styles.header}>
<div>{data.parent !== null && <h3>{data.name}</h3>}</div>
<div>
<Button type="link" size="small">
파일 업로드
</Button>
<Button type="link" size="small">
새 폴더
</Button>
</div>
</div>
<Table
rowKey="id"
columns={getColumns({
handleMove,
handleCopy,
handleDelete,
})}
dataSource={list}
pagination={false}
/>
</div>
);
}
type GetColumnsParams = {
handleMove: (id: number, to: number) => void;
handleCopy: (id: number, to: number) => void;
handleDelete: (id: number) => void;
};
function getColumns({
handleMove,
handleCopy,
handleDelete,
}: GetColumnsParams): ColumnsType<FileItem> {
return [
{
title: "파일명",
key: "name",
dataIndex: "name",
render: (_name: string, item) => <FileListItem item={item} />,
},
{
title: "크기",
key: "size",
dataIndex: "size",
width: 120,
render: (bytes: number, item) =>
item.is_folder ? "" : filesize(bytes, { round: 0 }),
},
{
title: "",
key: "action",
dataIndex: "",
width: 300,
render: (__: any, item) =>
item.is_folder ? null : (
<FileItemActions
item={item}
onMove={handleMove}
onCopy={handleCopy}
onDelete={handleDelete}
/>
),
},
];
}