summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJoakim Hulthe <joakim@hulthe.net>2024-06-12 14:43:06 +0200
committerOskar Nyberg <oskar@mullvad.net>2024-07-04 15:07:26 +0200
commit4aed3375b0e9ed85cc23709e9220b673859ae0b9 (patch)
tree409acbfc18e4fcc3bbe5e7f41e45f6652059f880
parent771e2506eaa9fe26dcac0258e717397f69d42314 (diff)
downloadmullvadvpn-4aed3375b0e9ed85cc23709e9220b673859ae0b9.tar.xz
mullvadvpn-4aed3375b0e9ed85cc23709e9220b673859ae0b9.zip
Trim custom list names in gui
-rw-r--r--gui/src/renderer/components/select-location/CustomListDialogs.tsx26
-rw-r--r--gui/src/renderer/components/select-location/CustomLists.tsx5
2 files changed, 18 insertions, 13 deletions
diff --git a/gui/src/renderer/components/select-location/CustomListDialogs.tsx b/gui/src/renderer/components/select-location/CustomListDialogs.tsx
index 56244e73eb..4f97706210 100644
--- a/gui/src/renderer/components/select-location/CustomListDialogs.tsx
+++ b/gui/src/renderer/components/select-location/CustomListDialogs.tsx
@@ -147,21 +147,25 @@ export function EditListDialog(props: EditListProps) {
const { updateCustomList } = useAppContext();
const [newName, setNewName] = useState(props.list.name);
+ const newNameTrimmed = newName.trim();
+ const newNameValid = newNameTrimmed !== '';
const [error, setError, unsetError] = useBoolean();
// Update name in list and save it.
const save = useCallback(async () => {
- try {
- const updatedList = { ...props.list, name: newName };
- const result = await updateCustomList(updatedList);
- if (result && result.type === 'name already exists') {
- setError();
- } else {
- props.hide();
+ if (newNameValid) {
+ try {
+ const updatedList = { ...props.list, name: newNameTrimmed };
+ const result = await updateCustomList(updatedList);
+ if (result && result.type === 'name already exists') {
+ setError();
+ } else {
+ props.hide();
+ }
+ } catch (e) {
+ const error = e as Error;
+ log.error(`Failed to edit custom list ${props.list.id}: ${error.message}`);
}
- } catch (e) {
- const error = e as Error;
- log.error(`Failed to edit custom list ${props.list.id}: ${error.message}`);
}
}, [props.list, newName, props.hide]);
@@ -175,7 +179,7 @@ export function EditListDialog(props: EditListProps) {
<ModalAlert
isOpen={props.isOpen}
buttons={[
- <AppButton.BlueButton key="save" onClick={save}>
+ <AppButton.BlueButton key="save" disabled={!newNameValid} onClick={save}>
{messages.gettext('Save')}
</AppButton.BlueButton>,
<AppButton.BlueButton key="cancel" onClick={props.hide}>
diff --git a/gui/src/renderer/components/select-location/CustomLists.tsx b/gui/src/renderer/components/select-location/CustomLists.tsx
index eafcb161b2..f335f7ae87 100644
--- a/gui/src/renderer/components/select-location/CustomLists.tsx
+++ b/gui/src/renderer/components/select-location/CustomLists.tsx
@@ -123,7 +123,8 @@ interface AddListFormProps {
function AddListForm(props: AddListFormProps) {
const [name, setName] = useState('');
- const nameValid = name.trim() !== '';
+ const nameTrimmed = name.trim();
+ const nameValid = nameTrimmed !== '';
const [error, setError, unsetError] = useBoolean();
const containerRef = useStyledRef<HTMLDivElement>();
const inputRef = useStyledRef<HTMLInputElement>();
@@ -137,7 +138,7 @@ function AddListForm(props: AddListFormProps) {
const createList = useCallback(async () => {
if (nameValid) {
try {
- const result = await props.onCreateList(name);
+ const result = await props.onCreateList(nameTrimmed);
if (result) {
setError();
}