-
Notifications
You must be signed in to change notification settings - Fork 527
Expand file tree
/
Copy pathFilesPage.js
More file actions
474 lines (426 loc) · 15.7 KB
/
FilesPage.js
File metadata and controls
474 lines (426 loc) · 15.7 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { findDOMNode } from 'react-dom'
import { Helmet } from 'react-helmet'
import { connect } from 'redux-bundler-react'
import { withTranslation, Trans } from 'react-i18next'
import ReactJoyride from 'react-joyride'
// Lib
import { filesTour } from '../lib/tours.js'
import { readSetting, writeSetting } from '../lib/local-storage.js'
// Components
import ContextMenu from './context-menu/ContextMenu.js'
import withTour from '../components/tour/withTour.js'
import InfoBoxes from './info-boxes/InfoBoxes.js'
import FilePreview from './file-preview/FilePreview.js'
import FilesList from './files-list/FilesList.js'
import FilesGrid from './files-grid/files-grid.js'
import { ViewList, ViewModule } from '../icons/stroke-icons.js'
import FileNotFound from './file-not-found/index.tsx'
import { getJoyrideLocales } from '../helpers/i8n.js'
import SortDropdown from './sort-dropdown/SortDropdown.js'
// Icons
import Modals, { DELETE, NEW_FOLDER, SHARE, ADD_BY_CAR, RENAME, ADD_BY_PATH, BULK_CID_IMPORT, SHORTCUTS, CLI_TUTOR_MODE, PINNING, PUBLISH } from './modals/Modals.js'
import Header from './header/Header.js'
import FileImportStatus from './file-import-status/FileImportStatus.js'
import { useExplore } from 'ipld-explorer-components/providers'
import SelectedActions from './selected-actions/SelectedActions.js'
import Checkbox from '../components/checkbox/Checkbox.js'
const FilesPage = ({
doFetchPinningServices, doFilesFetch, doPinsFetch, doFilesSizeGet, doFilesDownloadLink, doFilesDownloadCarLink, doFilesWrite, doAddCarFile, doFilesBulkCidImport, doFilesAddPath, doUpdateHash,
doFilesUpdateSorting, doFilesNavigateTo, doFilesMove, doSetCliOptions, doFetchRemotePins, remotePins, pendingPins, failedPins,
ipfsProvider, ipfsConnected, doFilesMakeDir, doFilesShareLink, doFilesCidProvide, doFilesDelete, doSetPinning, onRemotePinClick, doPublishIpnsKey,
files, filesPathInfo, filesIsFetching, pinningServices, toursEnabled, handleJoyrideCallback, isCliTutorModeEnabled, cliOptions, filesSorting, t
}) => {
const { doExploreUserProvidedPath } = useExplore()
const contextMenuRef = useRef()
const [modals, setModals] = useState({ show: null, files: null })
const [contextMenu, setContextMenu] = useState({
isOpen: false,
translateX: 0,
translateY: 0,
file: null
})
const [viewMode, setViewMode] = useState(() => readSetting('files.viewMode') || 'list')
const [selected, setSelected] = useState([])
const toggleViewMode = () => {
const newMode = viewMode === 'list' ? 'grid' : 'list'
setViewMode(newMode)
}
useEffect(() => {
doFetchPinningServices()
doFilesFetch()
doPinsFetch()
doFilesSizeGet()
}, [doFetchPinningServices, doFilesFetch, doPinsFetch, doFilesSizeGet])
useEffect(() => {
if (ipfsConnected || filesPathInfo.path) {
doFilesFetch()
}
}, [ipfsConnected, filesPathInfo, doFilesFetch])
useEffect(() => {
const handleKeyDown = (e) => {
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
return
}
if (e.key === '?' && e.shiftKey) {
e.preventDefault()
showModal(SHORTCUTS)
}
}
document.addEventListener('keydown', handleKeyDown)
return () => document.removeEventListener('keydown', handleKeyDown)
}, [])
// Persist view mode changes to localStorage
useEffect(() => {
writeSetting('files.viewMode', viewMode)
}, [viewMode])
/* TODO: uncomment below if we ever want automatic remote pin check
* (it was disabled for now due to https://github.com/ipfs/ipfs-desktop/issues/1954)
useEffect(() => {
files && files.content && doFetchRemotePins(files.content)
}, [files, pinningServices, doFetchRemotePins])
*/
const onDownload = async (files) => {
const url = await doFilesDownloadLink(files)
window.location.href = url
}
const onDownloadCar = async (files) => {
const url = await doFilesDownloadCarLink(files)
window.location.href = url
}
const onAddFiles = (raw, root = '') => {
if (root === '') root = files.path
doFilesWrite(raw, root)
}
const onBulkCidImport = (raw, root = '') => {
if (root === '') root = files.path
doFilesBulkCidImport(raw, root)
}
const onClosePreview = useCallback(() => {
// if the parentPath is / or null then we are at the root of the files page, (preview close button shouldn't even be visible in this case)
if (files?.parentPath == null || files?.parentPath === '/') return
doUpdateHash(files?.parentPath)
}, [files?.parentPath, doUpdateHash])
const onAddByPath = (path, name) => doFilesAddPath(files.path, path, name)
/**
*
* @param {File} file
* @param {string} name
*/
const onAddByCar = (file, name) => {
doAddCarFile(files.path, file, name)
}
const onInspect = (cid) => doUpdateHash(`/explore/${cid}`)
const onCheckRetrieval = (cid) => {
doFilesCidProvide(cid) // Trigger background provide
doUpdateHash(`/diagnostics/retrieval-check/${cid}`)
}
const showModal = (modal, files = null) => setModals({ show: modal, files })
const hideModal = () => setModals({})
/**
* @param {React.MouseEvent} ev
* @param {string} clickType
* @param {ContextMenuFile} file
* @param {Pick<DOMRect, 'y' | 'right' | 'bottom'>} [pos]
*/
const handleContextMenu = (ev, clickType, file, pos) => {
// This is needed to disable the native OS right-click menu
// and deal with the clicking on the ContextMenu options
if (ev !== undefined && typeof ev !== 'string') {
ev.preventDefault()
ev.persist()
}
const ctxMenu = findDOMNode(contextMenuRef.current)
const ctxMenuPosition = ctxMenu.getBoundingClientRect()
let translateX = 0
let translateY = 0
switch (clickType) {
case 'RIGHT': {
const rightPadding = window.innerWidth - ctxMenu.parentNode.getBoundingClientRect().right
translateX = (window.innerWidth - ev.clientX) - rightPadding - 20
translateY = (ctxMenuPosition.y + ctxMenuPosition.height / 2) - ev.clientY - 10
break
}
case 'TOP': {
const pagePositions = ctxMenu.parentNode.getBoundingClientRect()
translateX = pagePositions.right - pos.right
translateY = -(pos.bottom - pagePositions.top + 11)
break
}
default: {
translateX = 1
translateY = (ctxMenuPosition.y + ctxMenuPosition.height / 2) - (pos && pos.y) - 30
}
}
setContextMenu({
isOpen: !contextMenu.isOpen,
translateX,
translateY,
file
})
}
const MainView = ({ t, files, remotePins, pendingPins, failedPins, doExploreUserProvidedPath }) => {
const selectedFiles = useMemo(() =>
selected
.map(name => files?.content?.find(el => el.name === name))
.filter(n => n)
.map(file => ({
...file,
pinned: files?.pins?.map(p => p.toString())?.includes(file.cid.toString())
}))
/* eslint-disable-next-line react-hooks/exhaustive-deps */
, [files?.content, files?.pins, selected])
if (!files || files.type === 'file') return null
// Don't render stale content during navigation
if (files.path && filesPathInfo.path && files.path !== filesPathInfo.path) {
return null
}
if (files.type === 'unknown') {
const path = files.path
return (
<div>
<Trans i18nKey='cidNotFileNorDir' t={t}>
The current link isn't a file, nor a directory. Try to <button className='link blue pointer' onClick={() => doExploreUserProvidedPath(path)}>inspect</button> it instead.
</Trans>
</div>
)
}
if (files.type === 'not-found') {
return <FileNotFound path={files.path} />
}
const commonProps = {
updateSorting: doFilesUpdateSorting,
files: files.content || [],
pins: files.pins || [],
remotePins: remotePins || [],
pendingPins: pendingPins || [],
failedPins: failedPins || [],
filesPathInfo,
selected,
modalOpen: modals.show !== null,
onSelect: (name, isSelected) => {
if (Array.isArray(name)) {
if (isSelected) {
setSelected(name)
} else {
setSelected([])
}
} else {
if (isSelected) {
setSelected(prev => [...prev, name])
} else {
setSelected(prev => prev.filter(n => n !== name))
}
}
},
onShare: (files) => showModal(SHARE, files),
onRename: (files) => showModal(RENAME, files),
onRemove: (files) => showModal(DELETE, files),
onSetPinning: (files) => showModal(PINNING, files),
onInspect,
onRemotePinClick,
onDownload,
onAddFiles,
onNavigate: doFilesNavigateTo,
onMove: doFilesMove,
handleContextMenuClick: handleContextMenu,
// TODO: Implement this
onDismissFailedPin: () => {}
}
return <>
{viewMode === 'list'
? <FilesList {...commonProps} />
: <FilesGrid {...commonProps} />}
{selectedFiles.length !== 0 && <SelectedActions
className={'fixed bottom-0 right-0'}
style={{
zIndex: 20
}}
animateOnStart={selectedFiles.length === 1}
unselect={() => setSelected([])}
remove={() => showModal(DELETE, selectedFiles)}
rename={() => showModal(RENAME, selectedFiles)}
share={() => showModal(SHARE, selectedFiles)}
setPinning={() => showModal(PINNING, selectedFiles)}
download={() => onDownload(selectedFiles)}
inspect={() => onInspect(selectedFiles[0].cid)}
count={selectedFiles.length}
isMfs={filesPathInfo.isMfs}
size={selectedFiles.reduce((a, b) => a + (b.size || 0), 0)} />
}
</>
}
const getTitle = (filesPathInfo, t) => {
const parts = []
if (filesPathInfo) {
parts.push(filesPathInfo.realPath)
}
if (filesPathInfo.isMfs) {
parts.push(t('app:terms.files'))
}
parts.push('IPFS')
return parts.join(' | ')
}
return (
<div data-id='FilesPage' className='mw9 center'>
<Helmet>
<title>{getTitle(filesPathInfo, t)}</title>
</Helmet>
<ContextMenu
autofocus
ref={contextMenuRef}
isOpen={contextMenu.isOpen}
translateX={contextMenu.translateX}
translateY={contextMenu.translateY}
handleClick={handleContextMenu}
isMfs={filesPathInfo ? filesPathInfo.isMfs : false}
isUnknown={!!(contextMenu.file && contextMenu.file.type === 'unknown')}
pinned={contextMenu.file && contextMenu.file.pinned}
cid={contextMenu.file && contextMenu.file.cid}
onShare={() => showModal(SHARE, [contextMenu.file])}
onRemove={() => showModal(DELETE, [contextMenu.file])}
onRename={() => showModal(RENAME, [contextMenu.file])}
onInspect={() => onInspect(contextMenu.file.cid)}
onCheckRetrieval={() => onCheckRetrieval(contextMenu.file.cid)}
onDownload={() => onDownload([contextMenu.file])}
onDownloadCar={() => onDownloadCar([contextMenu.file])}
onPinning={() => showModal(PINNING, [contextMenu.file])}
onPublish={() => showModal(PUBLISH, [contextMenu.file])}
onCopyCid={(cid) => doFilesCidProvide(cid)}
isCliTutorModeEnabled={isCliTutorModeEnabled}
onCliTutorMode={() => showModal(CLI_TUTOR_MODE, [contextMenu.file])}
doSetCliOptions={doSetCliOptions}
/>
<Header
files={files}
onNavigate={doFilesNavigateTo}
onAddFiles={onAddFiles}
onMove={doFilesMove}
onAddByPath={(files) => showModal(ADD_BY_PATH, files)}
onAddByCar={(files) => showModal(ADD_BY_CAR, files)}
onBulkCidImport={(files) => showModal(BULK_CID_IMPORT, files)}
onNewFolder={(files) => showModal(NEW_FOLDER, files)}
onCliTutorMode={() => showModal(CLI_TUTOR_MODE)}
handleContextMenu={(...args) => handleContextMenu(...args, true)}
>
<div className="flex items-center justify-end">
<div className="mr3">
<SortDropdown
currentSort={filesSorting || { by: 'name', asc: true }}
onSortChange={doFilesUpdateSorting}
t={t}
/>
</div>
<button
className="pointer selected-item"
onClick={toggleViewMode}
title={viewMode === 'list' ? t('switchToGridView') : t('switchToListView')}
aria-label={viewMode === 'list' ? t('switchToGridView') : t('switchToListView')}
style={{
height: '24px'
}}
>
{viewMode === 'list' ? <ViewList width="24" height="24" /> : <ViewModule width="24" height="24" />}
</button>
</div>
</Header>
{(files && files.type !== 'file') && <div className="flex items-center justify-between">
<div>
{viewMode === 'grid' && files?.content?.length > 0
? (
<Checkbox
className='pv3 pl3 pr1 bg-white flex-none'
onChange={(checked) => {
if (checked) {
setSelected(files.content.map(f => f.name))
} else {
setSelected([])
}
}}
checked={files?.content?.length > 0 && selected.length === files.content.length}
label={<span className='fw5 f6'>{t('selectAllEntries')}</span>}
/>
)
: null
}
</div>
</div>}
<MainView t={t} files={files} remotePins={remotePins} pendingPins={pendingPins} failedPins={failedPins} doExploreUserProvidedPath={doExploreUserProvidedPath}/>
<Preview files={files} onDownload={() => onDownload([files])} onClose={onClosePreview} />
<InfoBoxes isRoot={filesPathInfo.isMfs && filesPathInfo.isRoot}
isCompanion={false}
isFetching={filesIsFetching}
filesExist={!!(files && files.content && files.content.length)} />
<Modals
done={hideModal}
root={files ? files.path : null}
onMove={doFilesMove}
onMakeDir={doFilesMakeDir}
onShareLink={doFilesShareLink}
onRemove={doFilesDelete}
onAddByPath={onAddByPath}
onAddByCar={onAddByCar}
onBulkCidImport={onBulkCidImport}
onPinningSet={doSetPinning}
onPublish={doPublishIpnsKey}
cliOptions={cliOptions}
{ ...modals } />
<FileImportStatus />
<ReactJoyride
run={toursEnabled}
steps={filesTour.getSteps({ t, Trans })}
styles={filesTour.styles}
callback={handleJoyrideCallback}
continuous
scrollToFirstStep
locale={getJoyrideLocales(t)}
showProgress />
</div>
)
}
const Preview = ({ files, onDownload, onClose }) => {
if (files && files.type === 'file') {
return (<FilePreview {...files} onDownload={onDownload} onClose={onClose} />)
}
return null
}
export default connect(
'selectIpfsProvider',
'selectIpfsConnected',
'selectFiles',
'selectFilesIsFetching',
'selectRemotePins',
'selectPendingPins',
'selectFailedPins',
'selectFilesPathInfo',
'doUpdateHash',
'doPinsFetch',
'doFetchPinningServices',
'selectPinningServices',
'doFetchRemotePins',
'doFilesFetch',
'doFilesMove',
'doFilesMakeDir',
'doFilesShareLink',
'doFilesCidProvide',
'doFilesDelete',
'doFilesAddPath',
'doAddCarFile',
'doFilesNavigateTo',
'doFilesUpdateSorting',
'selectFilesSorting',
'selectToursEnabled',
'doFilesWrite',
'doFilesBulkCidImport',
'doFilesDownloadLink',
'doFilesDownloadCarLink',
'doFilesSizeGet',
'selectIsCliTutorModeEnabled',
'selectIsCliTutorModalOpen',
'doOpenCliTutorModal',
'doSetCliOptions',
'selectCliOptions',
'doSetPinning',
'doPublishIpnsKey',
withTour(withTranslation('files')(FilesPage))
)