2
2
import styles from "./app.module.css" ;
3
3
import sharedStyles from "../shared.module.css"
4
4
5
- import { AuthType , createClient , FileStat , ResponseDataDetailed } from "webdav" ;
5
+ import { AuthType , createClient , FileStat , ResponseDataDetailed , WebDAVClient } from "webdav" ;
6
6
import React from "react" ;
7
7
import ProgressBar from "components/progress_bar" ;
8
8
import Settings , { SettingsObject } from "./settings" ;
9
9
10
- const client = createClient ( "http://localhost:7086/webdav" , {
11
- authType : AuthType . Password ,
12
- username : "flydav" ,
13
- password : "pass-for-testing" // sha256: 8c024a1ccc39abc26d05bacc4ab64b78ad4c4378e67df31975d5303ca2258a22
14
- } ) ;
10
+
15
11
16
12
const dirname = ( path : String ) => {
17
13
let ret = path . replace ( / \\ / g, '/' ) . replace ( / \/ [ ^ / ] * $ / , '' ) ;
@@ -36,33 +32,55 @@ const App = (): JSX.Element => {
36
32
const [ path , setPath ] = React . useState < string > ( "/" ) ;
37
33
const [ pathUncommitted , setPathUncommitted ] = React . useState < string > ( "/" ) ;
38
34
const [ files , setFiles ] = React . useState < FileStatExtended [ ] > ( [ ] ) ;
39
- const [ loading , setLoading ] = React . useState < boolean > ( false ) ;
35
+ const [ loading , setLoading ] = React . useState < string > ( "Loading" ) ;
40
36
const [ downloadProgress , setDownloadProgress ] = React . useState ( 0 ) ;
41
37
const [ showSettingsModal , setShowSettingsModal ] = React . useState ( false ) ;
38
+ const [ refreshFlag , setRefreshFlag ] = React . useState ( false ) ;
42
39
43
40
const [ settingsData , setSettingsData ] = React . useState < SettingsObject > ( {
44
- url : "http://localhost:7086/webdav " ,
45
- username : "flydav " ,
46
- password : "pass-for-testing "
41
+ url : "" ,
42
+ username : "" ,
43
+ password : ""
47
44
} )
48
45
46
+ const [ client , setClient ] = React . useState < WebDAVClient | null > ( null )
47
+
49
48
// try load settings from localStorage
50
49
React . useEffect ( ( ) => {
51
- const settings = localStorage . getItem ( "settings" )
52
- if ( settings ) {
53
- setSettingsData ( JSON . parse ( settings ) )
50
+ const settingsStr = localStorage . getItem ( "settings" )
51
+ if ( settingsStr ) {
52
+ let settings = JSON . parse ( settingsStr )
53
+ setSettingsData ( settings )
54
+ console . log ( "loaded settings from localStorage: " , settingsData )
55
+ } else {
56
+ console . log ( "no settings in localStorage" )
57
+ setShowSettingsModal ( true )
54
58
}
55
59
} , [ ] )
56
60
57
61
React . useEffect ( ( ) => {
58
- setLoading ( true ) ;
62
+ if ( settingsData . url == "" ) return ;
63
+ setClient ( createClient ( settingsData . url , {
64
+ username : settingsData . username ,
65
+ password : settingsData . password ,
66
+ authType : AuthType . Password
67
+ } ) )
68
+ } , [ settingsData ] )
69
+
70
+ React . useEffect ( ( ) => {
71
+ setRefreshFlag ( false ) ;
72
+ setLoading ( "Loading" ) ;
59
73
setFiles ( [ ] )
60
74
pathUncommitted != path && setPathUncommitted ( path ) ;
61
- client . getDirectoryContents ( path ) . then ( ( files : FileStat [ ] | ResponseDataDetailed < FileStat [ ] > ) => {
75
+ console . log ( "client: " , client ) ;
76
+
77
+ client ?. getDirectoryContents ( path ) . then ( ( files : FileStat [ ] | ResponseDataDetailed < FileStat [ ] > ) => {
62
78
// if is ResponseDataDetailed, unwrap
63
79
if ( ! Array . isArray ( files ) ) {
64
80
alert ( "Error: unexpected response type" )
65
81
}
82
+ console . log ( "files: " , files ) ;
83
+
66
84
let filesUnwrapped = files as FileStatExtended [ ] ;
67
85
68
86
filesUnwrapped . map ( f => {
@@ -72,17 +90,17 @@ const App = (): JSX.Element => {
72
90
setFiles ( filesUnwrapped ) ;
73
91
} ) . catch ( r => {
74
92
console . log ( r ) ;
75
- if ( r . status == 404 ) {
76
- alert ( "not found" ) ;
93
+ if ( r . status == 404 ) {
77
94
setPath ( "/" )
95
+ setLoading ( "Not found. Check settings." ) ;
78
96
}
79
97
} ) . finally ( ( ) => {
80
- setLoading ( false ) ;
98
+ setLoading ( "" ) ;
81
99
} ) ;
82
- } , [ path ] ) ;
100
+ } , [ path , client , refreshFlag ] ) ;
83
101
84
102
async function handleClickFile ( file : FileStat ) {
85
- const buff : Buffer = await client . getFileContents ( file . filename , {
103
+ const buff : Buffer = await client ? .getFileContents ( file . filename , {
86
104
format : "binary" ,
87
105
onDownloadProgress : e => {
88
106
setDownloadProgress ( e . loaded / e . total * 100 )
@@ -142,6 +160,9 @@ const App = (): JSX.Element => {
142
160
< button className = { sharedStyles . buttonPrimary } onClick = { ( ) => setPath (
143
161
pathUncommitted
144
162
) } > Go</ button >
163
+ < span className = "mr-2" > </ span >
164
+ < button className = { sharedStyles . buttonDefault } onClick = { ( ) => { setRefreshFlag ( true ) } } > Refresh</ button >
165
+
145
166
</ div >
146
167
</ section >
147
168
< section className = "p-4" >
@@ -157,7 +178,7 @@ const App = (): JSX.Element => {
157
178
{
158
179
files . filter ( file => file . type == "directory" ) . map ( ( file : FileStatExtended , idx , arr ) => {
159
180
return (
160
- < div className = { styles . fileListItem } >
181
+ < div className = { styles . fileListItem } key = { file . filename } >
161
182
< div className = { styles . fileListCell } >
162
183
< svg className = { styles . fileListIcon } xmlns = "http://www.w3.org/2000/svg" width = "16" height = "16" fill = "#e79f2d" viewBox = "0 0 16 16" >
163
184
< path d = "M9.828 3h3.982a2 2 0 0 1 1.992 2.181l-.637 7A2 2 0 0 1 13.174 14H2.825a2 2 0 0 1-1.991-1.819l-.637-7a1.99 1.99 0 0 1 .342-1.31L.5 3a2 2 0 0 1 2-2h3.672a2 2 0 0 1 1.414.586l.828.828A2 2 0 0 0 9.828 3zm-8.322.12C1.72 3.042 1.95 3 2.19 3h5.396l-.707-.707A1 1 0 0 0 6.172 2H2.5a1 1 0 0 0-1 .981l.006.139z" />
@@ -174,7 +195,7 @@ const App = (): JSX.Element => {
174
195
{
175
196
files . filter ( file => file . type == "file" ) . map ( ( file : FileStat , idx , arr ) => {
176
197
return (
177
- < div className = { styles . fileListItem } >
198
+ < div className = { styles . fileListItem } key = { file . filename } >
178
199
< div className = { styles . fileListCell } >
179
200
< svg className = { styles . fileListIcon } xmlns = "http://www.w3.org/2000/svg" width = "16" height = "16" fill = "currentColor" viewBox = "0 0 16 16" >
180
201
< path d = "M4 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H4zm0 1h8a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1z" />
0 commit comments