Skip to content

Commit 313611f

Browse files
authored
fix: Opening yaml upload in create mode asks to discard changes (#4126)
* fix: opening yaml upload in create mode asks to discard changes * refactor: extract duplicated code to a new function
1 parent 5fb73ff commit 313611f

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

src/command-pallette/CommandPalletteUI/CommandPaletteUI.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ReactNode, useEffect, useRef, useState } from 'react';
22
import { useRecoilValue } from 'recoil';
33
import { useAtomValue } from 'jotai';
44
import { useEventListener } from 'hooks/useEventListener';
5-
import { addHistoryEntry, getHistoryEntries } from './search-history';
5+
import { getHistoryEntries } from './search-history';
66
import { activeNamespaceIdState } from 'state/activeNamespaceIdAtom';
77
import {
88
CommandPalletteHelp,
@@ -19,6 +19,7 @@ import { SCREEN_SIZE_BREAKPOINT_M } from './types';
1919
import { useFormNavigation } from 'shared/hooks/useFormNavigation';
2020

2121
import './CommandPaletteUI.scss';
22+
import { activateResult, isResultGoingToRedirect } from './helpers';
2223

2324
function Background({
2425
hide,
@@ -150,10 +151,11 @@ export function CommandPaletteUI({
150151
if (key === 'Enter' && results[0]) {
151152
// choose current entry
152153
e.preventDefault();
153-
navigateSafely(() => {
154-
addHistoryEntry(results[0].query);
155-
results[0].onActivate();
156-
});
154+
if (isResultGoingToRedirect(results[0].query)) {
155+
navigateSafely(() =>
156+
activateResult(results[0].query, results[0].onActivate),
157+
);
158+
} else activateResult(results[0].query, results[0].onActivate);
157159
} else if (key === 'Tab') {
158160
e.preventDefault();
159161
// fill search with active history entry

src/command-pallette/CommandPalletteUI/ResultsList/ResultsList.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { useEffect, useRef } from 'react';
22
import { useEventListener } from 'hooks/useEventListener';
33
import { Result } from './Result';
44
import { Spinner } from 'shared/components/Spinner/Spinner';
5-
import { addHistoryEntry } from '../search-history';
65
import './ResultsList.scss';
76
import { useTranslation } from 'react-i18next';
87
import { LOADING_INDICATOR } from '../types';
98
import { useFormNavigation } from 'shared/hooks/useFormNavigation';
9+
import { activateResult, isResultGoingToRedirect } from '../helpers';
1010

1111
function scrollInto(element: Element) {
1212
element.scrollIntoView({
@@ -38,6 +38,22 @@ export function ResultsList({
3838
//todo 2
3939
const isLoading = results.find((r: any) => r.type === LOADING_INDICATOR);
4040
results = results.filter((r: any) => r.type !== LOADING_INDICATOR);
41+
42+
const activateResultWithSafeNavigation = () => {
43+
if (isResultGoingToRedirect(results[activeIndex].query)) {
44+
navigateSafely(() =>
45+
activateResult(
46+
results[activeIndex].query,
47+
results[activeIndex].onActivate,
48+
),
49+
);
50+
} else
51+
activateResult(
52+
results[activeIndex].query,
53+
results[activeIndex].onActivate,
54+
);
55+
};
56+
4157
useEffect(() => {
4258
if (results?.length <= activeIndex) {
4359
setActiveIndex(0);
@@ -65,10 +81,7 @@ export function ResultsList({
6581
scrollInto(listRef.current!.children[activeIndex - 1]);
6682
} else if (key === 'Enter' && results?.[activeIndex]) {
6783
e.preventDefault();
68-
navigateSafely(() => {
69-
addHistoryEntry(results[activeIndex].query);
70-
results[activeIndex].onActivate();
71-
});
84+
activateResultWithSafeNavigation();
7285
}
7386
},
7487
[activeIndex, results, isHistoryMode],
@@ -85,10 +98,7 @@ export function ResultsList({
8598
activeIndex={activeIndex}
8699
setActiveIndex={setActiveIndex}
87100
onItemClick={() => {
88-
navigateSafely(() => {
89-
addHistoryEntry(result.query);
90-
result.onActivate();
91-
});
101+
activateResultWithSafeNavigation();
92102
}}
93103
/>
94104
))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { addHistoryEntry } from './search-history';
2+
3+
export const activateResult = (query: string, activateCallback: () => void) => {
4+
addHistoryEntry(query);
5+
activateCallback();
6+
};
7+
8+
export const isResultGoingToRedirect = (resultQuery: string) => {
9+
switch (resultQuery) {
10+
case 'preferences':
11+
case 'prefs':
12+
case 'upload':
13+
case 'up':
14+
return false;
15+
default:
16+
return true;
17+
}
18+
};

0 commit comments

Comments
 (0)