Skip to content

Commit

Permalink
Prefix all localStorage keys with ecthelion:
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Dec 14, 2024
1 parent d1c503f commit 5b013f3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 62 deletions.
2 changes: 1 addition & 1 deletion imports/dashboard/files/fileManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ const FileManager = (props: {
if (r.status !== 200) {
setMessage(`Error deleting ${file}\n${(await r.json<{ error: string }>()).error}`)
} else setOverlay(`Deleting ${--total} out of ${filesSelected.length} files.`)
if (localStorage.getItem('logAsyncMassActions')) console.log('Deleted ' + file)
if (localStorage.getItem('ecthelion:logAsyncMassActions')) console.log('Deleted ' + file)
}).catch(e => setMessage(`Error deleting ${file}\n${e}`)))
}
Promise.allSettled(ops).then(() => {
Expand Down
107 changes: 58 additions & 49 deletions imports/dashboard/files/massActionDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,60 +24,60 @@ const MassActionDialog = ({
const moved = operation === 'move' ? 'Moved' : operation === 'compress' ? 'Compressed' : 'Copied'
const moving = operation === 'move' ? 'Moving' : operation === 'compress' ? 'Compressing ' : 'Copying'
const movingl = operation === 'move' ? 'moving' : operation === 'compress' ? 'compressing ' : 'copying'
const handleOperation = (): void => {
handleClose()
if (operation === 'compress') {
setOverlay(`Compressing ${files.length} files on the server.`)
const archiveTypeParam = archiveType.startsWith('tar') ? '&archiveType=tar&compress=' + (
archiveType === 'tar.gz' ? 'gzip'
: archiveType === 'tar.xz' ? 'xz'
: archiveType === 'tar.zst' ? 'zstd'
: 'false'
) : ''
ky.post(`${endpoint}/v2\

const handleCompressOperation = (): void => {
setOverlay(`Compressing ${files.length} files on the server.`)
const archiveTypeParam = archiveType.startsWith('tar') ? '&archiveType=tar&compress=' + (
archiveType === 'tar.gz' ? 'gzip'
: archiveType === 'tar.xz' ? 'xz'
: archiveType === 'tar.zst' ? 'zstd'
: 'false'
) : ''
ky.post(`${endpoint}/v2\
?async=true\
&path=${encodeURIComponent(path + newPath + '.' + archiveType)}${archiveTypeParam}\
&basePath=${encodeURIComponent(path)}`, { json: files }).then(res => {
if (res.ok) {
// Poll the token every second until the compression is finished.
res.json<{ token: string }>().then(async ({ token }) => {
while (true) {
const res = await ky.get(`${endpoint}/v2?token=${token}`).json<{ finished: boolean, error: string }>()
if (res.finished || res.error) {
reload()
setOverlay('')
setMessage(res.error ?? 'Compressed all files successfully!')
break
}
await new Promise(resolve => setTimeout(resolve, 1000))
}
}).catch(() => setMessage('Failed to compress the files!'))
} else if (res.status === 404 && archiveType !== 'zip') {
setOverlay('')
setMessage('Compressing `tar` archives requires Octyne v1.2 or newer!')
} else if (res.status === 404) {
// Fallback to v1 API without async compression and basePath.
const json = files.map(f => path + f)
ky.post(`${endpoint}?path=${encodeURIComponent(path + newPath + '.zip')}`, { json }).then(res => {
setOverlay('')
if (res.ok) {
if (res.ok) {
// Poll the token every second until the compression is finished.
res.json<{ token: string }>().then(async ({ token }) => {
while (true) {
const res = await ky.get(`${endpoint}/v2?token=${token}`).json<{ finished: boolean, error: string }>()
if (res.finished || res.error) {
reload()
setMessage('Compressed all files successfully!')
} else {
res.json<{ error: string }>()
.then(({ error }) => setMessage(error ?? 'Failed to compress the files!'))
.catch(() => setMessage('Failed to compress the files!'))
setOverlay('')
setMessage(res.error ?? 'Compressed all files successfully!')
break
}
}).catch(() => { setOverlay(''); setMessage('Failed to compress the files!') })
} else {
await new Promise(resolve => setTimeout(resolve, 1000))
}
}).catch(() => setMessage('Failed to compress the files!'))
} else if (res.status === 404 && archiveType !== 'zip') {
setOverlay('')
setMessage('Compressing `tar` archives requires Octyne v1.2 or newer!')
} else if (res.status === 404) {
// Fallback to v1 API without async compression and basePath.
const json = files.map(f => path + f)
ky.post(`${endpoint}?path=${encodeURIComponent(path + newPath + '.zip')}`, { json }).then(res => {
setOverlay('')
res.json<{ error: string }>()
.then(({ error }) => setMessage(error ?? 'Failed to compress the files!'))
.catch(() => setMessage('Failed to compress the files!'))
}
}).catch(() => { setOverlay(''); setMessage('Failed to compress the files!') })
return
}
if (res.ok) {
reload()
setMessage('Compressed all files successfully!')
} else {
res.json<{ error: string }>()
.then(({ error }) => setMessage(error ?? 'Failed to compress the files!'))
.catch(() => setMessage('Failed to compress the files!'))
}
}).catch(() => { setOverlay(''); setMessage('Failed to compress the files!') })
} else {
setOverlay('')
res.json<{ error: string }>()
.then(({ error }) => setMessage(error ?? 'Failed to compress the files!'))
.catch(() => setMessage('Failed to compress the files!'))
}
}).catch(() => { setOverlay(''); setMessage('Failed to compress the files!') })
}

const handleMoveCopyOperation = (): void => {
let left = files.length
setOverlay({ text: `${moving} ${left} out of ${files.length} files.`, progress: 0 })
const operations = []
Expand All @@ -93,7 +93,7 @@ const MassActionDialog = ({
}
const progress = (files.length - left) * 100 / files.length
setOverlay({ text: `${moving} ${--left} out of ${files.length} files.`, progress })
if (localStorage.getItem('logAsyncMassActions')) console.log(moved + ' ' + file)
if (localStorage.getItem('ecthelion:logAsyncMassActions')) console.log(moved + ' ' + file)
})
.catch(e => setMessage(`Error ${movingl} ${file}\n${e}`)))
}
Expand All @@ -103,6 +103,15 @@ const MassActionDialog = ({
setMessage(moved + ' all files successfully!')
}).catch(console.error) // Should not be called, ideally.
}

const handleOperation = (): void => {
handleClose()
if (operation === 'compress') {
handleCompressOperation()
} else {
handleMoveCopyOperation()
}
}
const prompt = operation === 'compress'
? 'Enter path to archive to create:'
: `Enter path of folder to ${operation} to:`
Expand Down
4 changes: 2 additions & 2 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export default function MyApp (props: AppProps & { emotionCache?: EmotionCache }
const [currentTheme, setCurrentTheme] = React.useState(defaultTheme)
const updateTheme = (): void => {
if (typeof localStorage !== 'object') return
const squareCorners = localStorage.getItem('square-corners') === 'true'
const lightMode = localStorage.getItem('light-mode') === 'true'
const squareCorners = localStorage.getItem('ecthelion:square-corners') === 'true'
const lightMode = localStorage.getItem('ecthelion:light-mode') === 'true'

// If no square corners and no light theme...
if (!squareCorners && !lightMode) return setCurrentTheme(defaultTheme)
Expand Down
2 changes: 1 addition & 1 deletion pages/dashboard/[server]/console.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const CommandTextField = ({ ws, id, buffer }: {
)
}

const terminalUi = typeof localStorage === 'object' && localStorage.getItem('terminal-ui') === 'true'
const terminalUi = typeof localStorage === 'object' && localStorage.getItem('ecthelion:terminal-ui') === 'true'
? { backgroundColor: '#141729', color: '#00cc74' } : {}

const Console = ({ setAuthenticated, setServerExists }: {
Expand Down
18 changes: 9 additions & 9 deletions pages/settings/about.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ const About = (): JSX.Element => {
useEffect(() => {
if (typeof localStorage !== 'object') return
ky.get('servers', { throwHttpErrors: true }).catch(() => setLoggedIn(false))
setLightMode(localStorage.getItem('light-mode') === 'true')
setTerminalUi(localStorage.getItem('terminal-ui') === 'true')
setSquareCorners(localStorage.getItem('square-corners') === 'true')
setLightMode(localStorage.getItem('ecthelion:light-mode') === 'true')
setTerminalUi(localStorage.getItem('ecthelion:terminal-ui') === 'true')
setSquareCorners(localStorage.getItem('ecthelion:square-corners') === 'true')
}, [ky])
const handleSquareCornersToggle = (e: React.ChangeEvent<HTMLInputElement>): void => {
setSquareCorners(e.target.checked)
if (e.target.checked) localStorage.setItem('square-corners', 'true')
else localStorage.removeItem('square-corners')
if (e.target.checked) localStorage.setItem('ecthelion:square-corners', 'true')
else localStorage.removeItem('ecthelion:square-corners')
updateTheme()
}
const handleTerminalUiToggle = (e: React.ChangeEvent<HTMLInputElement>): void => {
setTerminalUi(e.target.checked)
if (e.target.checked) localStorage.setItem('terminal-ui', 'true')
else localStorage.removeItem('terminal-ui')
if (e.target.checked) localStorage.setItem('ecthelion:terminal-ui', 'true')
else localStorage.removeItem('ecthelion:terminal-ui')
updateTheme()
}
const handleLightModeToggle = (e: React.ChangeEvent<HTMLInputElement>): void => {
setLightMode(e.target.checked)
if (e.target.checked) localStorage.setItem('light-mode', 'true')
else localStorage.removeItem('light-mode')
if (e.target.checked) localStorage.setItem('ecthelion:light-mode', 'true')
else localStorage.removeItem('ecthelion:light-mode')
updateTheme()
}

Expand Down

0 comments on commit 5b013f3

Please sign in to comment.