better code

Massimo Melina committed Feb 24, 2022 at 15:08 UTC ee45268e1a125a04b48e8e8684e7b6c224f30448
5 files changed +17 -15
admin/src/MenuButton.ts
+4 -5
@@ -4,9 +4,9 @@ import { Button, Menu, MenuItem } from '@mui/material'
4 interface Props { items: any[], [rest:string]:any }
5
6 export default function MenuButton({ items, ...rest }: Props) {
7 - const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null)
7 + const [anchorEl, setAnchorEl] = React.useState<HTMLElement>()
8 const open = Boolean(anchorEl)
9 - const onClose = useCallback(() => setAnchorEl(null), [])
9 + const onClose = useCallback(() => setAnchorEl(undefined), [])
10 return h(React.Fragment, {},
11 h(Button, {
12 'aria-controls': open ? 'basic-menu' : undefined,
@@ -22,8 +22,7 @@ export default function MenuButton({ items, ...rest }: Props) {
22 open,
23 onClose,
24 MenuListProps: { 'aria-labelledby': 'basic-button' },
25 - },
26 - items.map((it,idx) =>
25 + children: items.map((it,idx) =>
26 h(MenuItem, {
27 key: idx,
28 ...it,
@@ -32,6 +31,6 @@ export default function MenuButton({ items, ...rest }: Props) {
31 it.onClick?.apply(this, arguments)
32 }
33 }) )
35 - )
34 + })
35 )
36 }
frontend/src/BrowseFiles.ts
+1 -1
@@ -26,7 +26,7 @@ export function BrowseFiles() {
26
27 function FilesList() {
28 const { filteredList, list, loading, stoppedSearch } = useSnapState()
29 - const midnight = useMidnight() // as an optimization we calculate this only once per list
29 + const midnight = useMidnight() // as an optimization we calculate this only once per list and pass it down
30 const pageSize = 100
31 const [page, setPage] = useState(0)
32 const offset = page * pageSize
frontend/src/menu.ts
+2 -2
@@ -25,7 +25,7 @@ export function MenuPanel() {
25 setTimeout(() => setStarted1secAgo(true), 1000)
26 }, [stopSearch])
27
28 - //TODO do something for list > 63KB (1kb reserved for the rest for the url)
28 + //TODO do something for list > 63KB as it hit the url limit (1kb reserved for the rest for the url)
29 const list = Object.keys(selected).map(s => s.endsWith('/') ? s.slice(0,-1) : s).join('*')
30 return h('div', { id: 'menu-panel' },
31 h('div', { id: 'menu-bar' },
@@ -60,7 +60,7 @@ export function MenuPanel() {
60 showFilter && h('div', { id: 'filter-bar' },
61 h('input', {
62 id: 'filter',
63 - placeholder: 'Filter',
63 + placeholder: 'Filter list',
64 autoComplete: 'off',
65 value: filter,
66 autoFocus: true,
src/misc.ts
+3 -3
@@ -33,8 +33,8 @@ export function setHidden(dest: object, src:object) {
33 })))
34 }
35
36 -export function objSameKeys<T,R>(src: Record<string,T>, newValue:(value:T,key:string)=>R) {
37 - return Object.fromEntries(Object.entries(src).map(([k,v]) => [k, newValue(v,k)]))
36 +export function objSameKeys<S extends object,VR=any>(src: S, newValue:(value:any, key:keyof S)=>any) {
37 + return Object.fromEntries(Object.entries(src).map(([k,v]) => [k, newValue(v,k as keyof S)])) as { [K in keyof S]:VR }
38 }
39
40 export function wait(ms: number) {
@@ -88,7 +88,7 @@ export function generatorAsCallback<T>(caller: Callback<{ callback:Callback<T> }
88 }
89 }
90
91 -export function getOrSet<T>(o:any, k:string, creator:()=>T): T {
91 +export function getOrSet<T>(o: Record<string,T>, k:string, creator:()=>T): T {
92 return k in o ? o[k]
93 : (o[k] = creator())
94 }
src/throttler.ts
+7 -4
@@ -11,8 +11,11 @@ const mainThrottleGroup = new ThrottleGroup(Infinity)
11 subscribeConfig({ k:'max_kbps', defaultValue:null }, v =>
12 mainThrottleGroup.updateLimit(v ?? Infinity))
13
14 -interface GroupThrottler { count:number, throttler:ThrottledStream, destroy:()=>void }
15 -const ip2group: Record<string,GroupThrottler> = {}
14 +const ip2group: Record<string, {
15 + count: number
16 + group: ThrottleGroup
17 + destroy: () => void
18 +}> = {}
19
20 export function throttler(): Koa.Middleware {
21 return async (ctx, next) => {
@@ -24,9 +27,9 @@ export function throttler(): Koa.Middleware {
27 const tg = new ThrottleGroup(Infinity, mainThrottleGroup)
28 const unsub = subscribeConfig({ k:'max_kbps_per_ip', defaultValue:null }, v =>
29 tg.updateLimit(v ?? Infinity))
27 - return { tg, count:0, destroy: unsub }
30 + return { group:tg, count:0, destroy: unsub }
31 })
29 - const ts = new ThrottledStream(ipGroup.tg)
32 + const ts = new ThrottledStream(ipGroup.group)
33 ++ipGroup.count
34 ts.on('close', ()=> {
35 if (--ipGroup.count) return // any left?