김재형

Implement folder browsing

1 -import React, { useEffect } from "react"; 1 +import React from "react";
2 -import { Switch, Route, Redirect, useHistory } from "react-router-dom"; 2 +import { Switch, Route, Redirect } from "react-router-dom";
3 3
4 import { Login } from "auth/Login"; 4 import { Login } from "auth/Login";
5 import { useAuth, TokenContext } from "auth/useAuth"; 5 import { useAuth, TokenContext } from "auth/useAuth";
...@@ -10,13 +10,6 @@ import { FileList } from "file/FileList"; ...@@ -10,13 +10,6 @@ import { FileList } from "file/FileList";
10 export function App() { 10 export function App() {
11 const { token, login } = useAuth(); 11 const { token, login } = useAuth();
12 const root = token?.user.rootFolder; 12 const root = token?.user.rootFolder;
13 - const history = useHistory();
14 -
15 - useEffect(() => {
16 - if (root) {
17 - history.push(`/folder/${root}`);
18 - }
19 - }, [history, root]);
20 13
21 return ( 14 return (
22 <Switch> 15 <Switch>
...@@ -31,6 +24,9 @@ export function App() { ...@@ -31,6 +24,9 @@ export function App() {
31 <Route path="/folder/:id"> 24 <Route path="/folder/:id">
32 <FileList /> 25 <FileList />
33 </Route> 26 </Route>
27 + <Route>
28 + <Redirect to={`/folder/${root}`} />
29 + </Route>
34 </Switch> 30 </Switch>
35 </Page> 31 </Page>
36 </TokenContext.Provider> 32 </TokenContext.Provider>
......
...@@ -42,8 +42,16 @@ export function FileList() { ...@@ -42,8 +42,16 @@ export function FileList() {
42 const id = useParams<{ id: string }>().id; 42 const id = useParams<{ id: string }>().id;
43 const { data } = useFileList(id); 43 const { data } = useFileList(id);
44 44
45 - if (data && data.parent !== null) { 45 + if (!data) {
46 - data.list.unshift(({ 46 + return null;
47 + }
48 +
49 + const list = [...data.list].sort((itemA, itemB) =>
50 + itemA.is_folder === itemB.is_folder ? 0 : itemA.is_folder ? -1 : 1
51 + );
52 +
53 + if (data.parent !== null) {
54 + list.unshift(({
47 id: data.parent, 55 id: data.parent,
48 is_folder: true, 56 is_folder: true,
49 name: "..", 57 name: "..",
...@@ -51,5 +59,10 @@ export function FileList() { ...@@ -51,5 +59,10 @@ export function FileList() {
51 } as unknown) as FileItem); 59 } as unknown) as FileItem);
52 } 60 }
53 61
54 - return data && <Table columns={columns} dataSource={data.list} />; 62 + return (
63 + <div>
64 + {data.parent !== null && <h3>{data.name}</h3>}
65 + <Table columns={columns} dataSource={list} pagination={false} />
66 + </div>
67 + );
55 } 68 }
......
1 import React, { Fragment } from "react"; 1 import React, { Fragment } from "react";
2 import { FileItem } from "./useFileList"; 2 import { FileItem } from "./useFileList";
3 +import { Link } from "react-router-dom";
3 4
4 export function FileListItem({ item }: { item: FileItem }) { 5 export function FileListItem({ item }: { item: FileItem }) {
5 - return <Fragment>{item.name}</Fragment>; 6 + return (
7 + <Fragment>
8 + {item.is_folder ? (
9 + <Link to={`/folder/${item.id}`}>{item.name}</Link>
10 + ) : (
11 + item.name
12 + )}
13 + </Fragment>
14 + );
6 } 15 }
......