Skip to content

Commit

Permalink
Remove lastEls, clean up Files inline JSX funcs.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Sep 24, 2021
1 parent e115a53 commit 6ab677b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 106 deletions.
160 changes: 63 additions & 97 deletions imports/dashboard/files/files.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,52 @@ const Files = (props: {
setMessage('Uploaded all files successfully!')
fetchFiles()
}
// Single file logic.
const handleDeleteMenuButton = async () => {
setMenuOpen('')
setFetching(true)
const a = await request(
ip,
`/server/${server}/file?path=${euc(path + menuOpen)}`,
{ method: 'DELETE' }
).then(async e => await e.json())
if (a.error) setMessage(a.error)
setFetching(false)
setMenuOpen('')
fetchFiles()
}
const handleDownloadMenuButton = async () => {
setMenuOpen('')
const ticket = await fetch(ip + '/ott', {
headers: { authorization: localStorage.getItem('token') || '' }
})
const ott = encodeURIComponent((await ticket.json()).ticket)
window.location.href = `${ip}/server/${server}/file?ticket=${ott}&path=${path}${menuOpen}`
}
const handleDecompressMenuButton = async () => {
setMenuOpen('')
setFetching(true)
const a = await request(
ip,
`/server/${server}/decompress?path=${euc(path + menuOpen)}`,
{ method: 'POST' }
).then(async e => await e.json())
if (a.error) setMessage(a.error)
setFetching(false)
setMenuOpen('')
fetchFiles()
}
const handleDownloadButton = async () => {
setDownload('')
// document.cookie = `X-Authentication=${localStorage.getItem('token')}`
const ticket = await fetch(ip + '/ott', {
headers: { authorization: localStorage.getItem('token') || '' }
})
const ott = encodeURIComponent((await ticket.json()).ticket)
window.location.href = download.replace('?path', `?ticket=${ott}&path`)
}

const selectedFile = menuOpen && files && files.find(e => e.name === menuOpen)
return (
<>
{!files ? <ConnectionFailure loading={fetching} /> : (
Expand Down Expand Up @@ -258,27 +304,21 @@ const Files = (props: {
</>
)}
<Tooltip title='Reload'>
<span>
<IconButton disabled={fetching} onClick={fetchFiles}>
<Replay />
</IconButton>
</span>
<IconButton disabled={fetching} onClick={fetchFiles}>
<Replay />
</IconButton>
</Tooltip>
<div style={{ paddingRight: 5 }} />
<Tooltip title='Create Folder'>
<span>
<IconButton disabled={fetching} onClick={() => setFolderPromptOpen(true)}>
<CreateNewFolder />
</IconButton>
</span>
<IconButton disabled={fetching} onClick={() => setFolderPromptOpen(true)}>
<CreateNewFolder />
</IconButton>
</Tooltip>
<div style={{ paddingRight: 5 }} />
<Tooltip title='Create File'>
<span>
<IconButton disabled={fetching} onClick={() => setFile({ name: '', content: '' })}>
<Add />
</IconButton>
</span>
<IconButton disabled={fetching} onClick={() => setFile({ name: '', content: '' })}>
<Add />
</IconButton>
</Tooltip>
<div style={{ paddingRight: 5 }} />
<UploadButton
Expand Down Expand Up @@ -318,20 +358,7 @@ const Files = (props: {
onClose={() => setDownload('')}
message={`Do you want to download '${download.replace(/%2F/g, '/').split('/').filter(e => e).pop()}'?`}
action={[
<Button
key='download'
size='small'
color='primary'
onClick={async () => {
setDownload('')
// document.cookie = `X-Authentication=${localStorage.getItem('token')}`
const ticket = await fetch(ip + '/ott', {
headers: { authorization: localStorage.getItem('token') || '' }
})
const ott = encodeURIComponent((await ticket.json()).ticket)
window.location.href = download.replace('?path', `?ticket=${ott}&path`)
}}
>
<Button key='download' size='small' color='primary' onClick={handleDownloadButton}>
Download
</Button>,
<Button key='close' size='small' aria-label='close' color='inherit' onClick={() => setDownload('')}>
Expand Down Expand Up @@ -372,83 +399,22 @@ const Files = (props: {
/>
)}
{massActionMenuOpen && (
<Menu
keepMounted
anchorEl={massActionMenuOpen}
onClose={() => setMassActionMenuOpen(null)}
open
>
<Menu open keepMounted anchorEl={massActionMenuOpen} onClose={() => setMassActionMenuOpen(null)}>
<MenuItem onClick={() => setMassActionDialogOpen('move')}>Move</MenuItem>
<MenuItem onClick={() => setMassActionDialogOpen('copy')}>Copy</MenuItem>
<MenuItem onClick={async () => await handleFilesDelete()}>Delete</MenuItem>
<MenuItem onClick={() => setMassActionDialogOpen('compress')}>Compress</MenuItem>
</Menu>
)}
{menuOpen && (
<Menu
keepMounted
anchorEl={anchorEl}
onClose={() => setMenuOpen('')}
open
>
{selectedFile && (
<Menu open keepMounted anchorEl={anchorEl} onClose={() => setMenuOpen('')}>
<MenuItem onClick={() => setModifyFileDialogOpen('rename')}>Rename</MenuItem>
<MenuItem onClick={() => setModifyFileDialogOpen('move')}>Move</MenuItem>
<MenuItem onClick={() => setModifyFileDialogOpen('copy')}>Copy</MenuItem>
<MenuItem
onClick={async () => {
setMenuOpen('')
setFetching(true)
const a = await request(
ip,
`/server/${server}/file?path=${euc(path + menuOpen)}`,
{ method: 'DELETE' }
).then(async e => await e.json())
if (a.error) setMessage(a.error)
setFetching(false)
setMenuOpen('')
fetchFiles()
}}
>
Delete
</MenuItem>
{!(() => {
const file = files && files.find(e => e.name === menuOpen)
return file && file.folder
})() && (
<MenuItem
onClick={async () => {
setMenuOpen('')
const ticket = await fetch(ip + '/ott', {
headers: { authorization: localStorage.getItem('token') || '' }
})
const ott = encodeURIComponent((await ticket.json()).ticket)
window.location.href = `${ip}/server/${server}/file?ticket=${ott}&path=${path}${menuOpen}`
}}
>
Download
</MenuItem>
)}
{(() => {
const file = files && files.find(e => e.name === menuOpen)
return file && !file.folder && file.name.endsWith('.zip')
})() && (
<MenuItem
onClick={async () => {
setMenuOpen('')
setFetching(true)
const a = await request(
ip,
`/server/${server}/decompress?path=${euc(path + menuOpen)}`,
{ method: 'POST' }
).then(async e => await e.json())
if (a.error) setMessage(a.error)
setFetching(false)
setMenuOpen('')
fetchFiles()
}}
>
Decompress
</MenuItem>
<MenuItem onClick={handleDeleteMenuButton}>Delete</MenuItem>
{!selectedFile.folder && <MenuItem onClick={handleDownloadMenuButton}>Download</MenuItem>}
{!selectedFile.folder && selectedFile.name.endsWith('.zip') && (
<MenuItem onClick={handleDecompressMenuButton}>Decompress</MenuItem>
)}
</Menu>
)}
Expand Down
20 changes: 11 additions & 9 deletions pages/dashboard/[server]/console.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ import ConsoleView from '../../../imports/dashboard/console/consoleView'
import ConsoleButtons from '../../../imports/dashboard/console/consoleButtons'
import ConnectionFailure from '../../../imports/errors/connectionFailure'

const lastEls = (array: any[], size: number) => {
const length = array.length
if (length > size) return array.slice(length - (size - 1))
else return array
}

// https://overreacted.io/making-setinterval-declarative-with-react-hooks/
const useInterval = (callback: (...args: any[]) => void, delay: number) => {
const savedCallback = useRef<() => void>()
Expand Down Expand Up @@ -92,7 +86,16 @@ const Console = ({ setAuthenticated }: {
if (buffer.current.length === 0) return
const oldBuffer = buffer.current
buffer.current = []
setConsole(lastEls(consoleText.concat(oldBuffer), 650))
if (oldBuffer.length >= 650) setConsole(oldBuffer.slice(oldBuffer.length - 650))
else if (consoleText.length + oldBuffer.length >= 650) {
const consoleSlice = consoleText.slice(consoleText.length - (650 - oldBuffer.length))
consoleSlice.push(...oldBuffer)
setConsole(consoleSlice)
} else {
const dupe = consoleText.slice(0)
dupe.push(...oldBuffer)
setConsole(dupe)
}
}, 50)

// Check if the user is authenticated.
Expand All @@ -117,8 +120,7 @@ const Console = ({ setAuthenticated }: {
// This listener needs to be loaded ASAP.
// Limit the amount of lines in memory to prevent out of memory site crashes :v
ws.onmessage = (event) => {
buffer.current = buffer.current
.concat(event.data.split('\n').map((line: string) => ({ id: ++id, text: line })))
buffer.current.push(...event.data.split('\n').map((text: string) => ({ id: ++id, text })))
}
setWs(ws)
// Register listeners.
Expand Down

0 comments on commit 6ab677b

Please sign in to comment.