Skip to content

Commit

Permalink
Enable several more ESLint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Dec 20, 2024
1 parent 67995fd commit 14eb95d
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 91 deletions.
29 changes: 14 additions & 15 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,32 @@ export default tseslint.config(
},
rules: {
'@typescript-eslint/no-confusing-void-expression': 'off',
/* '@typescript-eslint/restrict-template-expressions': [
'@typescript-eslint/no-import-type-side-effects': ['error'],
'@typescript-eslint/consistent-type-imports': [
'error',
{
prefer: 'type-imports',
disallowTypeAnnotations: true,
fixStyle: 'inline-type-imports',
},
],
'@typescript-eslint/restrict-template-expressions': [
'error',
{
allowAny: false,
allowBoolean: false,
allowNullish: false,
allowNullish: true, // TODO: Turn this off!
allowNumber: true,
allowRegExp: false,
allowNever: false,
},
], */
],
'promise/no-nesting': 'off', // TODO: Re-enable!
'promise/always-return': ['error', { ignoreLastCallback: true }],
'n/no-missing-import': 'off',
'n/no-unsupported-features/node-builtins': 'off',
'n/no-unsupported-features/es-syntax': 'off',
'import/no-unresolved': 'off',
// TODO: Re-enable these!
'@typescript-eslint/use-unknown-in-catch-callback-variable': 'off',
'@typescript-eslint/restrict-template-expressions': 'off',
'@typescript-eslint/no-unnecessary-condition': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'promise/catch-or-return': 'off',
'promise/always-return': 'off',
'promise/no-nesting': 'off',
},
},
eslintPluginPrettierRecommended,
Expand Down
8 changes: 4 additions & 4 deletions imports/dashboard/files/fileList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
} from '@mui/material'
import AutoSizer from 'react-virtualized-auto-sizer'
import { FixedSizeList, type ListChildComponentProps } from 'react-window'
import { useRouter } from 'next/router'
import Folder from '@mui/icons-material/Folder'
import MoreVert from '@mui/icons-material/MoreVert'
import InsertDriveFile from '@mui/icons-material/InsertDriveFile'
import UnstyledLink from '../../helpers/unstyledLink'
import { joinPath } from './fileUtils'
import useOctyneData from '../useOctyneData'

const rtd = (num: number): number => Math.round(num * 100) / 100
const bytesToGb = (bytes: number): string => {
Expand Down Expand Up @@ -101,7 +101,7 @@ const FileListItemRenderer = ({
}: ListChildComponentProps): React.JSX.Element => {
const { files, path, disabled, filesSelected, setFilesSelected, openMenu, onClick } =
data as FileItemData
const router = useRouter()
const { node, server } = useOctyneData()
const file = files[index]
const selectItem = (): void => {
if (!filesSelected.includes(file.name)) setFilesSelected([...filesSelected, file.name])
Expand All @@ -120,7 +120,7 @@ const FileListItemRenderer = ({
}
const subpath = file.folder ? joinPath(path, file.name) : path
const params = new URLSearchParams()
if (router.query.node) params.append('node', router.query.node as string)
if (node) params.append('node', node)
if (!file.folder) params.append('file', file.name)
return (
<FileListItem
Expand All @@ -138,7 +138,7 @@ const FileListItemRenderer = ({
: onClick(file)
}
onCheck={e => (e.shiftKey ? shiftClickItem() : selectItem())}
url={`/dashboard/${router.query.server}/files${subpath}${params.size ? '?' : ''}${params}`}
url={`/dashboard/${server}/files${subpath}${params.size ? '?' : ''}${params}`}
/>
)
}
Expand Down
66 changes: 37 additions & 29 deletions imports/dashboard/files/fileManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import FolderCreationDialog from './folderCreationDialog'

const euc: (uriComponent: string | number | boolean) => string =
typeof encodeURIComponent === 'function' ? encodeURIComponent : e => e.toString()
const errorMessage = (err: unknown) =>
err instanceof Error ? err.message : typeof err === 'string' ? err : JSON.stringify(err)
const editorExts = ['properties', 'json', 'yaml', 'yml', 'xml', 'js', 'log', 'sh', 'txt']

const FileManager = (props: {
Expand Down Expand Up @@ -116,7 +118,7 @@ const FileManager = (props: {
setError(null)
const files = await ky
.get(`server/${server}/files?path=${euc(path)}`)
.json<{ error?: string; contents: File[] }>()
.json<{ error?: string; contents?: File[] }>()
if (files.error === 'This server does not exist!') setServerExists(false)
else if (files.error === 'You are not authenticated to access this resource!')
setAuthenticated(false)
Expand All @@ -132,14 +134,14 @@ const FileManager = (props: {
.pop(),
true,
)
} else if (files) {
} else if (files.contents) {
setFiles(files.contents)
setFilesSelected([])
}
setFetching(false)
})().catch(e => {
})().catch((e: unknown) => {
console.error(e)
setMessage(`Failed to fetch files: ${e.message}`)
setMessage(`Failed to fetch files: ${errorMessage(e)}`)
setFetching(false)
})
}, [path, ky, server, updatePath, setAuthenticated, setServerExists])
Expand Down Expand Up @@ -207,7 +209,7 @@ const FileManager = (props: {
fileInf.size < 2 * 1024 * 1024 &&
(editorExts.includes(filename.split('.').pop() ?? '') || fileInf.mimeType.startsWith('text/'))
) {
loadFileInEditor(filename).catch(err => {
loadFileInEditor(filename).catch((err: unknown) => {
console.error(err)
setMessage('An error occurred while loading file!')
})
Expand All @@ -224,8 +226,8 @@ const FileManager = (props: {
if (createFolder.success) fetchFiles()
else setMessage(createFolder.error)
setFetching(false)
} catch (e: any) {
setMessage(e.message as string)
} catch (e: unknown) {
setMessage(errorMessage(e))
setFetching(false)
}
}
Expand All @@ -252,8 +254,8 @@ const FileManager = (props: {
if (editFile.success) fetchFiles()
else setMessage(editFile.error)
setFetching(false)
} catch (e: any) {
setMessage(e.message as string)
} catch (e: unknown) {
setMessage(errorMessage(e))
setFetching(false)
}
}
Expand Down Expand Up @@ -282,7 +284,7 @@ const FileManager = (props: {
return
}
} catch (e) {
setMessage(`Error deleting files: ${e}`)
setMessage(`Error deleting files: ${errorMessage(e)}`)
setOverlay('')
fetchFiles()
return
Expand All @@ -308,10 +310,11 @@ const FileManager = (props: {
progress,
})
}
// eslint-disable-next-line promise/always-return -- false positive
if (localStorage.getItem('ecthelion:logAsyncMassActions'))
console.log('Deleted ' + file)
})
.catch(e => setMessage(`Error deleting ${file}\n${e}`)),
.catch((e: unknown) => setMessage(`Error deleting ${file}\n${errorMessage(e)}`)),
)
}
Promise.allSettled(ops)
Expand All @@ -338,16 +341,17 @@ const FileManager = (props: {
},
)
if (r.status !== 200) {
setMessage(`Error uploading ${file.name}\n${JSON.parse(r.body).error}`)
const { error } = JSON.parse(r.body) as { error?: string }
setMessage(`Error uploading ${file.name}: ${error ?? 'Unknown Error'}`)
}
setOverlay('')
}
setMessage('Uploaded all files successfully!')
if (path === prevPath.current) fetchFiles() // prevPath is current path after useEffect call.
})().catch(e => {
})().catch((e: unknown) => {
console.error(e)
setOverlay('')
setMessage(`Failed to upload files: ${e.message}`)
setMessage(`Failed to upload files: ${errorMessage(e)}`)
})
}
// Single file logic.
Expand All @@ -362,19 +366,19 @@ const FileManager = (props: {
setFetching(false)
setMenuOpen('')
fetchFiles()
})().catch(e => {
})().catch((e: unknown) => {
console.error(e)
setMessage(`Failed to delete file: ${e.message}`)
setMessage(`Failed to delete file: ${errorMessage(e)}`)
})
}
const handleDownloadMenuButton = (): void => {
;(async () => {
setMenuOpen('')
const ticket = encodeURIComponent((await ky.get('ott').json<{ ticket: string }>()).ticket)
window.location.href = `${ip}/server/${server}/file?ticket=${ticket}&path=${path}${menuOpen}`
})().catch(e => {
})().catch((e: unknown) => {
console.error(e)
setMessage(`Failed to download file: ${e.message}`)
setMessage(`Failed to download file: ${errorMessage(e)}`)
})
}
const handleDecompressMenuButton = (): void => {
Expand All @@ -392,9 +396,9 @@ const FileManager = (props: {
setFetching(false)
setMenuOpen('')
fetchFiles()
})().catch(e => {
})().catch((e: unknown) => {
console.error(e)
setMessage(`Failed to decompress file: ${e.message}`)
setMessage(`Failed to decompress file: ${errorMessage(e)}`)
})
}
const handleCloseDownload = (): void => {
Expand All @@ -407,9 +411,9 @@ const FileManager = (props: {
const ticket = encodeURIComponent((await ky.get('ott').json<{ ticket: string }>()).ticket)
const loc = `${ip}/server/${server}/file?ticket=${ticket}&path=${euc(joinPath(path, download))}`
window.location.href = loc
})().catch((e: any) => {
})().catch((e: unknown) => {
console.error(e)
setMessage(`Failed to download file: ${e.message}`)
setMessage(`Failed to download file: ${errorMessage(e)}`)
})
}
const handleSaveFile = async (name: string, content: string): Promise<void> => {
Expand All @@ -420,8 +424,8 @@ const FileManager = (props: {
const r = await ky.post(`server/${server}/file?path=${encodedPath}`, { body: formData })
if (r.status !== 200) setMessage((await r.json<{ error: string }>()).error)
else setMessage('Saved successfully!')
} catch (e: any) {
setMessage(`Error saving file! ${e}`)
} catch (e: unknown) {
setMessage(`Error saving file! ${errorMessage(e)}`)
console.error(e)
}
}
Expand Down Expand Up @@ -479,9 +483,9 @@ const FileManager = (props: {
(await ky.get('ott').json<{ ticket: string }>()).ticket,
)
window.location.href = `${ip}/server/${server}/file?path=${path}${file.name}&ticket=${ott}`
})().catch(e => {
})().catch((e: unknown) => {
console.error(e)
setMessage(`Failed to download file: ${e.message}`)
setMessage(`Failed to download file: ${errorMessage(e)}`)
})
}}
/>
Expand Down Expand Up @@ -629,15 +633,19 @@ const FileManager = (props: {
{folderPromptOpen && (
<FolderCreationDialog
handleClose={() => setFolderPromptOpen(false)}
handleCreateFolder={async (name: string) => await handleCreateFolder(name)}
handleCreateFolder={(name: string) => {
handleCreateFolder(name).catch(console.error)
}}
/>
)}
{modifyFileDialogOpen && (
<ModifyFileDialog
filename={menuOpen}
operation={modifyFileDialogOpen}
handleClose={() => setModifyFileDialogOpen('')}
handleEdit={async path => await handleModifyFile(path, modifyFileDialogOpen)}
handleEdit={path => {
handleModifyFile(path, modifyFileDialogOpen).catch(console.error)
}}
/>
)}
{massActionDialogOpen && (
Expand Down Expand Up @@ -671,7 +679,7 @@ const FileManager = (props: {
</MenuItem>
<MenuItem
onClick={() => {
handleFilesDelete().catch(() => {})
handleFilesDelete().catch(console.error)
}}
disabled={!!overlay}
>
Expand Down
2 changes: 1 addition & 1 deletion imports/dashboard/files/folderCreationDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const FolderCreationDialog = ({
handleCreateFolder,
handleClose,
}: {
handleCreateFolder: (name: string) => any
handleCreateFolder: (name: string) => void
handleClose: () => void
}): React.JSX.Element => {
const [name, setName] = useState('')
Expand Down
30 changes: 17 additions & 13 deletions imports/dashboard/files/massActionDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ const MassActionDialog = ({
res
.json<{ token: string }>()
.then(async ({ token }) => {
while (true) {
let finished: boolean | string = false
while (!finished) {
await new Promise(resolve => setTimeout(resolve, 1000))
const res = await ky
.get(`server/${server}/compress/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))
.json<{ finished: boolean; error?: string }>()

finished = res.finished || !!res.error
if (finished) setMessage(res.error ?? 'Compressed all files successfully!')
}
reload()
setOverlay('')
})
.catch(() => setMessage('Failed to compress the files!'))
} else if (res.status === 404 && archiveType !== 'zip') {
Expand All @@ -101,7 +101,7 @@ const MassActionDialog = ({
} else {
res
.json<{ error: string }>()
.then(({ error }) => setMessage(error ?? 'Failed to compress the files!'))
.then(({ error }) => setMessage(error))
.catch(() => setMessage('Failed to compress the files!'))
}
})
Expand All @@ -113,7 +113,7 @@ const MassActionDialog = ({
setOverlay('')
res
.json<{ error: string }>()
.then(({ error }) => setMessage(error ?? 'Failed to compress the files!'))
.then(({ error }) => setMessage(error))
.catch(() => setMessage('Failed to compress the files!'))
}
})
Expand Down Expand Up @@ -152,7 +152,8 @@ const MassActionDialog = ({
}
} catch (e) {
reload()
setMessage(`Error ${movingl} files: ${e}`)
console.error(e)
setMessage(`Error ${movingl} files: ${e instanceof Error ? e.message : 'Unknown Error!'}`)
setOverlay('')
return
}
Expand All @@ -174,10 +175,13 @@ const MassActionDialog = ({
}
const progress = ((files.length - left) * 100) / files.length
setOverlay({ text: `${moving} ${--left} out of ${files.length} files.`, progress })
// eslint-disable-next-line promise/always-return -- false positive
if (localStorage.getItem('ecthelion:logAsyncMassActions'))
console.log(moved + ' ' + file)
})
.catch(e => setMessage(`Error ${movingl} ${file}\n${e}`)),
.catch((e: unknown) =>
setMessage(`Error ${movingl} ${file}: ${e instanceof Error ? e.message : 'Unknown'}`),
),
)
}
Promise.allSettled(requests)
Expand Down
2 changes: 1 addition & 1 deletion imports/dashboard/files/modifyFileDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const ModifyFileDialog = ({
operation,
filename,
}: {
handleEdit: (path: string) => any
handleEdit: (path: string) => void
handleClose: () => void
operation: 'move' | 'copy' | 'rename'
filename: string
Expand Down
2 changes: 1 addition & 1 deletion imports/dashboard/useOctyneData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const useOctyneAuth = (): OctyneDataWithAuth => {
if (servers.ok) setServerExists(!!resp[server])
if (servers.ok || servers.status === 401 || servers.status === 403) setAuth(servers.ok)
else setConnectionFailure(true)
})().catch(err => {
})().catch((err: unknown) => {
console.error(err)
setConnectionFailure(true)
})
Expand Down
Loading

0 comments on commit 14eb95d

Please sign in to comment.