Skip to content

Commit 288703d

Browse files
committed
render the selected folder in a recognizable way
1 parent 0022f6d commit 288703d

File tree

3 files changed

+85
-63
lines changed

3 files changed

+85
-63
lines changed

client/browser/FinderFileSelect.tsx

Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React, {
2-
forwardRef,
32
lazy,
3+
memo,
44
Suspense,
5+
useCallback,
56
useEffect,
6-
useImperativeHandle,
77
useMemo,
88
useRef,
99
useState,
@@ -14,6 +14,7 @@ import ArrowDownIcon from '../icons/arrow-down.svg';
1414
import ArrowRightIcon from '../icons/arrow-right.svg';
1515
import EmptyIcon from '../icons/empty.svg';
1616
import FolderIcon from '../icons/folder.svg';
17+
import FolderOpenIcon from '../icons/folder-open.svg';
1718
import RootIcon from '../icons/root.svg';
1819

1920

@@ -51,38 +52,17 @@ function Figure(props) {
5152
}
5253

5354

54-
const FolderList = forwardRef((props: any, forwardedRef) => {
55-
const [files, setFiles] = useState(props.files);
56-
const [isLoading, setLoading] = useState(false);
57-
const [searchQuery, setSearchQuery] = useState(() => {
58-
const params = new URLSearchParams(window.location.search);
59-
return params.get('q');
60-
});
61-
62-
useImperativeHandle(forwardedRef, () => ({fetchFiles}));
63-
64-
async function fetchFiles(folderId) {
65-
const params = new URLSearchParams({q: searchQuery});
66-
const listFilesUrl = `${props.baseUrl}list/${folderId}${searchQuery ? `?${params.toString()}` : ''}`;
67-
setLoading(true);
68-
const response = await fetch(listFilesUrl);
69-
if (response.ok) {
70-
const body = await response.json();
71-
setFiles(body.files);
72-
} else {
73-
console.error(response);
74-
}
75-
setLoading(false);
76-
}
55+
const FilesList = memo((props: any) => {
56+
const {files, isLoading} = props;
7757

7858
function selectFile(file) {
7959
console.log(file);
8060
}
8161

62+
console.log('FolderList', files);
8263
return (
8364
<ul className="files-list">{
84-
isLoading ?
85-
<li className="status">{gettext("Loading…")}</li> : files.length === 0 ?
65+
files.length === 0 ?
8666
<li className="status">{gettext("Empty folder")}</li> :
8767
files.map(file => (
8868
<li key={file.id} onClick={() => selectFile(file)}><Figure {...file} /></li>
@@ -93,25 +73,28 @@ const FolderList = forwardRef((props: any, forwardedRef) => {
9373

9474

9575
function FolderEntry(props) {
96-
const {folder, toggleOpen, listFolder} = props;
76+
const {folder, toggleOpen, fetchFiles, isCurrent} = props;
9777

9878
if (folder.is_root) {
99-
return (<span onClick={() => listFolder(folder.id)}><RootIcon/></span>);
79+
return (<span onClick={() => fetchFiles(folder.id)}><RootIcon/></span>);
10080
}
10181

10282
return (<>
10383
<i onClick={toggleOpen}>{
10484
folder.has_subfolders ? folder.is_open ? <ArrowDownIcon/> : <ArrowRightIcon/> : <EmptyIcon/>
10585
}</i>
106-
<span onClick={() => listFolder(folder.id)}>
86+
{isCurrent ?
87+
<strong><FolderOpenIcon/>{folder.name}</strong> :
88+
<span onClick={() => fetchFiles(folder.id)} role="button">
10789
<FolderIcon/>{folder.name}
10890
</span>
91+
}
10992
</>);
11093
}
11194

11295

11396
function FolderStructure(props) {
114-
const {baseUrl, folder, refreshStructure, folderListRef} = props;
97+
const {baseUrl, folder, lastFolderId, fetchFiles, refreshStructure} = props;
11598

11699
async function fetchChildren() {
117100
const response = await fetch(`${baseUrl}fetch/${folder.id}`);
@@ -139,23 +122,26 @@ function FolderStructure(props) {
139122
refreshStructure();
140123
}
141124

142-
async function listFolder(folderId) {
143-
await folderListRef.current.fetchFiles(folderId);
144-
}
145-
146125
return folder ? (
147126
<li>
148-
<FolderEntry folder={folder} toggleOpen={toggleOpen} listFolder={listFolder} />
149-
{folder.is_open && (<ul>
150-
{folder.children.map(child => (
151-
<FolderStructure
152-
key={child.id}
153-
folder={child}
154-
baseUrl={baseUrl}
155-
refreshStructure={refreshStructure}
156-
folderListRef={folderListRef}
157-
/>
158-
))}
127+
<FolderEntry
128+
folder={folder}
129+
toggleOpen={toggleOpen}
130+
fetchFiles={fetchFiles}
131+
isCurrent={lastFolderId === folder.id}
132+
/>
133+
{folder.is_open && (
134+
<ul>
135+
{folder.children.map(child => (
136+
<FolderStructure
137+
key={child.id}
138+
baseUrl={baseUrl}
139+
folder={child}
140+
lastFolderId={lastFolderId}
141+
fetchFiles={fetchFiles}
142+
refreshStructure={refreshStructure}
143+
/>
144+
))}
159145
</ul>)}
160146
</li>
161147
) : null;
@@ -164,7 +150,12 @@ function FolderStructure(props) {
164150

165151
export default function FinderFileSelect(props) {
166152
const baseUrl = props['base-url'];
167-
const [structure, setStructure] = useState({root_folder: null, files: []});
153+
const [structure, setStructure] = useState({root_folder: null, last_folder: null, files: []});
154+
const [isLoading, setLoading] = useState(false);
155+
const [searchQuery, setSearchQuery] = useState(() => {
156+
const params = new URLSearchParams(window.location.search);
157+
return params.get('q');
158+
});
168159
const folderListRef = useRef(null);
169160

170161
useEffect(() => {
@@ -180,18 +171,46 @@ export default function FinderFileSelect(props) {
180171
}
181172
}
182173

174+
async function fetchFiles(folderId){
175+
const params = new URLSearchParams({q: searchQuery});
176+
const listFilesUrl = `${baseUrl}list/${folderId}${searchQuery ? `?${params.toString()}` : ''}`;
177+
setLoading(true);
178+
const newStructure = {root_folder: structure.root_folder, last_folder: folderId, files: []};
179+
const response = await fetch(listFilesUrl);
180+
if (response.ok) {
181+
const body = await response.json();
182+
newStructure.files = body.files;
183+
} else {
184+
console.error(response);
185+
}
186+
setLoading(false);
187+
setStructure(newStructure);
188+
}
189+
190+
function refreshStructure() {
191+
console.log('refreshStructure');
192+
setStructure({...structure});
193+
}
194+
183195
function handleUpload(folderId) {
184196
folderListRef.current.fetchFiles
185197
}
186198

187199
return structure.root_folder && (<>
188200
<ul className="folder-structure">
189-
<FolderStructure baseUrl={baseUrl} folder={structure.root_folder} folderListRef={folderListRef} refreshStructure={() => {
190-
setStructure({...structure});
191-
}}/>
201+
<FolderStructure
202+
baseUrl={baseUrl}
203+
folder={structure.root_folder}
204+
lastFolderId={structure.last_folder}
205+
fetchFiles={fetchFiles}
206+
// folderListRef={folderListRef}
207+
refreshStructure={refreshStructure}
208+
/>
192209
</ul>
193-
<FileUploader folderId={structure.root_folder} handleUpload={handleUpload}>
194-
<FolderList baseUrl={baseUrl} files={structure.files} ref={folderListRef} />
195-
</FileUploader>
210+
<FileUploader folderId={structure.root_folder} handleUpload={handleUpload}>{
211+
isLoading ?
212+
<div className="status">{gettext("Loading…")}</div> :
213+
<FilesList files={structure.files} />
214+
}</FileUploader>
196215
</>);
197216
}

client/finder-browser.scss

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ finder-file-select {
3131
margin-right: 0.5em;
3232
}
3333

34-
span {
34+
span[role="button"] {
3535
cursor: pointer;
3636
&:hover {
3737
text-decoration: underline;
@@ -58,15 +58,6 @@ finder-file-select {
5858
min-height: 150px;
5959
width: 125px;
6060

61-
&.status {
62-
font-size: 18px;
63-
font-weight: bold;
64-
line-height: 50px;
65-
color: #808080;
66-
padding-left: 2em;
67-
width: auto;
68-
}
69-
7061
.figure {
7162
width: 100%;
7263
height: 100%;
@@ -115,6 +106,15 @@ finder-file-select {
115106
}
116107
}
117108

109+
.status {
110+
font-size: 18px;
111+
font-weight: bold;
112+
line-height: 50px;
113+
color: #808080;
114+
padding-left: 2em;
115+
width: auto;
116+
}
117+
118118
.file-uploader {
119119
width: 100%;
120120
}

client/icons/folder-open.svg

Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)