From b3bc78d23c72b0c073beae9f46c86930beeea942 Mon Sep 17 00:00:00 2001 From: Vikrant Gupta Date: Wed, 14 Feb 2024 16:47:39 +0530 Subject: [PATCH] fix: date time value retain on location switch (#4546) * fix: date time value retain on location switch * chore: added inline comments * feat: added shortcut strings based on user os * feat: added shortcut strings based on user os * feat: added shortcut strings based on user os --- .../CustomTimePicker/CustomTimePicker.tsx | 11 +++++++++++ .../src/constants/shortcuts/globalShortcuts.ts | 13 +++++++++++++ .../constants/shortcuts/logsExplorerShortcuts.ts | 10 ++++++++++ frontend/src/pages/Shortcuts/utils.ts | 10 +++++++++- frontend/src/utils/getUserOS.ts | 14 ++++++++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 frontend/src/utils/getUserOS.ts diff --git a/frontend/src/components/CustomTimePicker/CustomTimePicker.tsx b/frontend/src/components/CustomTimePicker/CustomTimePicker.tsx index 5586d2230c..9e1aeadfd4 100644 --- a/frontend/src/components/CustomTimePicker/CustomTimePicker.tsx +++ b/frontend/src/components/CustomTimePicker/CustomTimePicker.tsx @@ -21,6 +21,7 @@ import { useEffect, useState, } from 'react'; +import { useLocation } from 'react-router-dom'; import { popupContainer } from 'utils/selectPopupContainer'; import CustomTimePickerPopoverContent from './CustomTimePickerPopoverContent'; @@ -68,6 +69,7 @@ function CustomTimePicker({ const [inputErrorMessage, setInputErrorMessage] = useState( null, ); + const location = useLocation(); const [isInputFocused, setIsInputFocused] = useState(false); const getSelectedTimeRangeLabel = ( @@ -222,6 +224,15 @@ function CustomTimePicker({ setIsInputFocused(false); }; + // this is required as TopNav component wraps the components and we need to clear the state on path change + useEffect(() => { + setInputStatus(''); + onError(false); + setInputErrorMessage(null); + setInputValue(''); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [location.pathname]); + return (
> = { 'Logs Explorer Shortcuts': LogsExplorerShortcuts, }; +export const ALL_SHORTCUTS_LABEL: Record> = { + 'Global Shortcuts': GlobalShortcutsName, + 'Logs Explorer Shortcuts': LogsExplorerShortcutsName, +}; + export const ALL_SHORTCUTS_DESCRIPTION: Record< string, Record @@ -46,9 +53,10 @@ export function generateTableData( ): TableProps['dataSource'] { const shortcuts = ALL_SHORTCUTS[shortcutSection]; const shortcutsDescription = ALL_SHORTCUTS_DESCRIPTION[shortcutSection]; + const shortcutsLabel = ALL_SHORTCUTS_LABEL[shortcutSection]; return Object.keys(shortcuts).map((shortcutName) => ({ key: `${shortcuts[shortcutName]} ${shortcutName}`, - shortcutKey: shortcuts[shortcutName], + shortcutKey: shortcutsLabel[shortcutName], shortcutDescription: shortcutsDescription[shortcutName], })); } diff --git a/frontend/src/utils/getUserOS.ts b/frontend/src/utils/getUserOS.ts new file mode 100644 index 0000000000..164b2b8234 --- /dev/null +++ b/frontend/src/utils/getUserOS.ts @@ -0,0 +1,14 @@ +export enum UserOperatingSystem { + WINDOWS = 'Windows', + MACOS = 'Mac OS', +} + +export function getUserOperatingSystem(): UserOperatingSystem { + // https://developer.mozilla.org/en-US/docs/Web/API/Navigator/userAgent + if (window.navigator.userAgent.indexOf(UserOperatingSystem.WINDOWS) !== -1) { + return UserOperatingSystem.WINDOWS; + } + + // default return is MacOS + return UserOperatingSystem.MACOS; +}