better code

Massimo Melina committed Jun 19, 2023 at 18:32 UTC 7a3a91586e646deb6ba1958be45d63a147ecd53a
3 files changed +13 -11
admin/src/misc.ts
+2 -2
@@ -1,6 +1,6 @@
1 // This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3 -import { createElement as h, FC, Fragment, ReactNode } from 'react'
3 +import { createElement as h, FC, Fragment, ReactNode, KeyboardEvent } from 'react'
4 import {
5 Box,
6 Breakpoint,
@@ -167,7 +167,7 @@ export function reloadBtn(onClick: any, props?: any) {
167 }
168
169 const isMac = navigator.platform.match('Mac')
170 -export function isCtrlKey(ev: React.KeyboardEvent) {
170 +export function isCtrlKey(ev: KeyboardEvent) {
171 return (ev.ctrlKey || isMac && ev.metaKey) && ev.key
172 }
173
frontend/src/dialog.ts
+5 -4
@@ -6,6 +6,7 @@ import { newDialog, closeDialog, DialogOptions, DialogCloser } from '@hfs/shared
6 import _ from 'lodash'
7 import { useInterval } from 'usehooks-ts'
8 import { t } from './i18n'
9 +import { err2msg } from './misc'
10 export * from '@hfs/shared/dialogs'
11
12 interface PromptOptions extends Partial<DialogOptions> { def?:string, type?:string, trim?: boolean }
@@ -59,10 +60,8 @@ export async function promptDialog(msg: string, { def, type, trim=true, ...rest
60 type AlertType = 'error' | 'warning' | 'info'
61
62 export async function alertDialog(msg: ReactElement | string | Error, type:AlertType='info', { getClose=_.noop }={}) {
62 - if (msg instanceof Error) {
63 - msg = msg.message
63 + if (msg instanceof Error)
64 type = 'error'
65 - }
65 return new Promise(resolve => getClose(newDialog({
66 className: 'dialog-alert dialog-alert-'+type,
67 title: t(_.capitalize(type)),
@@ -72,7 +71,9 @@ export async function alertDialog(msg: ReactElement | string | Error, type:Alert
71 })))
72
73 function Content(){
75 - if (typeof msg === 'string' || msg instanceof Error)
74 + if (msg instanceof Error)
75 + msg = err2msg(msg)
76 + if (typeof msg === 'string')
77 msg = h('p', {}, String(msg))
78 return msg
79 }
src/api.monitor.ts
+6 -5
@@ -36,7 +36,7 @@ const apis: ApiHandlers = {
36 },
37 connectionClosed(conn: Connection) {
38 if (cancel(conn)) return
39 - list.remove(serializeConnection(conn, true))
39 + list.remove(getConnAddress(conn))
40 conn[state] = false
41 },
42 connectionUpdated(conn: Connection, change: Change) {
@@ -65,19 +65,20 @@ const apis: ApiHandlers = {
65
66 function update(conn: Connection, change: Change) {
67 if (conn[state] === false) return
68 - list.update(serializeConnection(conn, true), change)
68 + list.update(getConnAddress(conn), change)
69 }
70
71 - function serializeConnection(conn: Connection, minimal?:true) {
71 + function serializeConnection(conn: Connection) {
72 const { socket, started, secure } = conn
73 - return Object.assign(getConnAddress(conn), !minimal && {
73 + return {
74 + ...getConnAddress(conn),
75 v: (socket.remoteFamily?.endsWith('6') ? 6 : 4),
76 got: socket.bytesRead,
77 sent: socket.bytesWritten,
78 started,
79 secure: (secure || undefined) as boolean|undefined, // undefined will save some space once json-ed
80 ...fromCtx(conn.ctx),
80 - })
81 + }
82 }
83
84 function fromCtx(ctx?: Koa.Context) {