@samitouri / QOSami-HFS / commits / c37e1504

search: warn about illegal chars

Massimo Melina committed Feb 11, 2024 at 00:00 UTC c37e1504eee4e58438368f5f1595e3b5d36efe5b
4 files changed +28 -10
frontend/src/dialog.ts
+21 -6
@@ -1,18 +1,26 @@
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, ReactElement, ReactNode, useEffect, useRef, useState, KeyboardEvent } from 'react'
3 +import { createElement as h, ReactElement, ReactNode, useEffect, useRef, useState, KeyboardEvent,
4 + InputHTMLAttributes } from 'react'
5 import './dialog.css'
6 import { newDialog, closeDialog, DialogOptions, dialogsDefaults } from '@hfs/shared/dialogs'
7 import _ from 'lodash'
8 import { useInterval } from 'usehooks-ts'
9 import { t } from './i18n'
9 -import { err2msg, isCtrlKey, pendingPromise } from './misc'
10 +import { err2msg, isCtrlKey, pendingPromise, Promisable } from './misc'
11 export * from '@hfs/shared/dialogs'
12
13 _.merge(dialogsDefaults, { closableProps: { 'aria-label': t`Close` } })
14
14 -interface PromptOptions extends Partial<DialogOptions> { def?:string, type?:string, trim?: boolean, helperText?: ReactNode }
15 -export async function promptDialog(msg: string, { def, type, helperText, trim=true, ...rest }:PromptOptions={}) : Promise<string | undefined> {
15 +interface PromptOptions extends Partial<DialogOptions> {
16 + def?: string,
17 + type?: string,
18 + trim?: boolean,
19 + helperText?: ReactNode,
20 + inputProps?: Partial<InputHTMLAttributes<string>>
21 + onSubmit?: (v: string) => Promisable<string>
22 +}
23 +export async function promptDialog(msg: string, { def, type, helperText, trim=true, inputProps, onSubmit, ...rest }:PromptOptions={}) : Promise<string | undefined> {
24 const textarea = type === 'textarea' && type
25 return new Promise(resolve => newDialog({
26 className: 'dialog-prompt',
@@ -65,11 +73,18 @@ export async function promptDialog(msg: string, { def, type, helperText, trim=tr
73 h('button', { onClick: go }, t`Continue`)),
74 )
75
68 - function go() {
76 + async function go() {
77 let res = ref.current?.value
78 if (trim)
79 res = res?.trim()
72 - closeDialog(res)
80 + try {
81 + if (onSubmit && res !== undefined)
82 + res = await onSubmit?.(res) ?? res
83 + closeDialog(res)
84 + }
85 + catch(e: any) {
86 + alertDialog(e, 'error')
87 + }
88 }
89 }
90 }
frontend/src/menu.ts
+3 -2
@@ -3,7 +3,7 @@
3 import { state, useSnapState } from './state'
4 import { createElement as h, Fragment, useEffect, useMemo, useState } from 'react'
5 import { alertDialog, confirmDialog, ConfirmOptions, promptDialog } from './dialog'
6 -import { defaultPerms, err2msg, ErrorMsg, onlyTruthy, prefix, useStateMounted, VfsPerms, working } from './misc'
6 +import { defaultPerms, err2msg, ErrorMsg, onlyTruthy, prefix, throw_, useStateMounted, VfsPerms, working } from './misc'
7 import { loginDialog } from './login'
8 import { showOptions } from './options'
9 import showUserPanel from './UserPanel'
@@ -141,7 +141,8 @@ export function MenuPanel() {
141 label: t`Search`,
142 onClickAnimation: false,
143 async onClick() {
144 - state.remoteSearch = await promptDialog(t('search_msg', "Search this folder and sub-folders"), { title: t`Search` }) || ''
144 + state.remoteSearch = await promptDialog(t('search_msg', "Search this folder and sub-folders"),
145 + { title: t`Search`, onSubmit: x => x.includes('/') ? throw_(t`Invalid value`) : x }) || ''
146 }
147 }
148 }
src/langs/hfs-lang-en.json
+2 -1
@@ -157,6 +157,7 @@
157 "go_last": "Go to last item",
158 "Shuffle": "Shuffle",
159 "Repeat": "Repeat",
160 - "showHelpListShortcut": "From the file list, click holding {key} to Show quickly"
160 + "showHelpListShortcut": "From the file list, click holding {key} to Show quickly",
161 + "Invalid value": "Invalid value"
162 }
163 }
src/langs/hfs-lang-it.json
+2 -1
@@ -149,6 +149,7 @@
149 "go_last": "Vai alla fine",
150 "Shuffle": "Riproduzione casuale",
151 "Repeat": "Ripeti a fine lista",
152 - "showHelpListShortcut": "Dalla lista dei file, clicca tenendo {key} per accedere velocemente al File Show"
152 + "showHelpListShortcut": "Dalla lista dei file, clicca tenendo {key} per accedere velocemente al File Show",
153 + "Invalid value": "Valore non valido"
154 }
155 }