@@ -2,20 +2,16 @@ import React, {
22 lazy ,
33 memo ,
44 Suspense ,
5- useCallback ,
65 useEffect ,
76 useMemo ,
87 useRef ,
98 useState ,
109} from 'react' ;
10+ import { Tooltip } from 'react-tooltip' ;
1111import FigureLabels from '../finder/FigureLabels' ;
1212import FileUploader from '../finder/FileUploader' ;
13- import ArrowDownIcon from '../icons/arrow-down.svg' ;
14- import ArrowRightIcon from '../icons/arrow-right.svg' ;
15- import EmptyIcon from '../icons/empty.svg' ;
16- import FolderIcon from '../icons/folder.svg' ;
17- import FolderOpenIcon from '../icons/folder-open.svg' ;
18- import RootIcon from '../icons/root.svg' ;
13+ import FolderStructure from './FolderStructure' ;
14+ import Menu from './Menu' ;
1915
2016
2117function StaticFigure ( props ) {
@@ -53,13 +49,14 @@ function Figure(props) {
5349
5450
5551const FilesList = memo ( ( props : any ) => {
56- const { files, isLoading } = props ;
52+ const { files} = props ;
5753
5854 function selectFile ( file ) {
5955 console . log ( file ) ;
6056 }
6157
6258 console . log ( 'FolderList' , files ) ;
59+
6360 return (
6461 < ul className = "files-list" > {
6562 files . length === 0 ?
@@ -72,91 +69,11 @@ const FilesList = memo((props: any) => {
7269} ) ;
7370
7471
75- function FolderEntry ( props ) {
76- const { folder, toggleOpen, fetchFiles, isCurrent} = props ;
77-
78- if ( folder . is_root ) {
79- return ( < span onClick = { ( ) => fetchFiles ( folder . id ) } > < RootIcon /> </ span > ) ;
80- }
81-
82- return ( < >
83- < i onClick = { toggleOpen } > {
84- folder . has_subfolders ? folder . is_open ? < ArrowDownIcon /> : < ArrowRightIcon /> : < EmptyIcon />
85- } </ i >
86- { isCurrent ?
87- < strong > < FolderOpenIcon /> { folder . name } </ strong > :
88- < span onClick = { ( ) => fetchFiles ( folder . id ) } role = "button" >
89- < FolderIcon /> { folder . name }
90- </ span >
91- }
92- </ > ) ;
93- }
94-
95-
96- function FolderStructure ( props ) {
97- const { baseUrl, folder, lastFolderId, fetchFiles, refreshStructure} = props ;
98-
99- async function fetchChildren ( ) {
100- const response = await fetch ( `${ baseUrl } fetch/${ folder . id } ` ) ;
101- if ( response . ok ) {
102- const reply = await response . json ( ) ;
103- folder . name = reply . name ;
104- folder . has_subfolders = reply . has_subfolders ;
105- folder . children = reply . children ;
106- } else {
107- console . error ( response ) ;
108- }
109- }
110-
111- async function toggleOpen ( ) {
112- folder . is_open = ! folder . is_open ;
113- if ( folder . is_open ) {
114- if ( folder . children === null ) {
115- await fetchChildren ( ) ;
116- } else {
117- await fetch ( `${ baseUrl } open/${ folder . id } ` ) ;
118- }
119- } else {
120- await fetch ( `${ baseUrl } close/${ folder . id } ` ) ;
121- }
122- refreshStructure ( ) ;
123- }
124-
125- return folder ? (
126- < li >
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- ) ) }
145- </ ul > ) }
146- </ li >
147- ) : null ;
148- }
149-
150-
15172export default function FinderFileSelect ( props ) {
15273 const baseUrl = props [ 'base-url' ] ;
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- } ) ;
74+ const [ structure , setStructure ] = useState ( { root_folder : null , last_folder : null , files : null } ) ;
15975 const folderListRef = useRef ( null ) ;
76+ const uploaderRef = useRef ( null ) ;
16077
16178 useEffect ( ( ) => {
16279 getStructure ( ) ;
@@ -171,19 +88,22 @@ export default function FinderFileSelect(props) {
17188 }
17289 }
17390
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 ) ;
91+ async function fetchFiles ( folderId : string , searchQuery = '' ) {
92+ const fetchUrl = ( ( ) => {
93+ if ( searchQuery ) {
94+ const params = new URLSearchParams ( { q : searchQuery } ) ;
95+ return `${ baseUrl } ${ folderId } /search?${ params . toString ( ) } ` ;
96+ }
97+ return `${ baseUrl } ${ folderId } /list` ;
98+ } ) ( ) ;
99+ const newStructure = { root_folder : structure . root_folder , last_folder : folderId , files : null } ;
100+ const response = await fetch ( fetchUrl ) ;
180101 if ( response . ok ) {
181102 const body = await response . json ( ) ;
182103 newStructure . files = body . files ;
183104 } else {
184105 console . error ( response ) ;
185106 }
186- setLoading ( false ) ;
187107 setStructure ( newStructure ) ;
188108 }
189109
@@ -193,24 +113,38 @@ export default function FinderFileSelect(props) {
193113 }
194114
195115 function handleUpload ( folderId ) {
196- folderListRef . current . fetchFiles
116+ fetchFiles ( folderId ) ;
197117 }
198118
199119 return structure . root_folder && ( < >
200- < ul className = "folder-structure" >
201- < FolderStructure
202- baseUrl = { baseUrl }
203- folder = { structure . root_folder }
120+ < nav className = "folder-structure" >
121+ < ul >
122+ < FolderStructure
123+ baseUrl = { baseUrl }
124+ folder = { structure . root_folder }
125+ lastFolderId = { structure . last_folder }
126+ fetchFiles = { fetchFiles }
127+ refreshStructure = { refreshStructure }
128+ />
129+ </ ul >
130+ </ nav >
131+ < div className = "file-browser" >
132+ < Menu
204133 lastFolderId = { structure . last_folder }
205134 fetchFiles = { fetchFiles }
206- // folderListRef={folderListRef}
207- refreshStructure = { refreshStructure }
135+ openUploader = { ( ) => uploaderRef . current . openUploader ( ) }
208136 />
209- </ ul >
210- < FileUploader folderId = { structure . root_folder } handleUpload = { handleUpload } > {
211- isLoading ?
212- < div className = "status" > { gettext ( "Loading…" ) } </ div > :
213- < FilesList files = { structure . files } />
214- } </ FileUploader >
137+ < FileUploader
138+ folderId = { structure . last_folder }
139+ handleUpload = { handleUpload }
140+ ref = { uploaderRef }
141+ settings = { { csrf_token : props [ 'csrf-token' ] , base_url : props [ 'base-url' ] } }
142+ > {
143+ structure . files === null ?
144+ < div className = "status" > { gettext ( "Loading files…" ) } </ div > :
145+ < FilesList files = { structure . files } />
146+ } </ FileUploader >
147+ </ div >
148+ < Tooltip id = "django-finder-tooltip" place = "bottom-start" />
215149 </ > ) ;
216150}
0 commit comments