From 826cc33f75801817a3c0b060f470fcad2399b702 Mon Sep 17 00:00:00 2001 From: Sina Date: Fri, 12 Jul 2024 12:54:24 +0200 Subject: [PATCH] [Fix] allow to see all resources (#265) --- src/locales/de-DE/messages.po | 28 ++--- src/locales/en-US/messages.po | 28 ++--- .../InventoryAdvanceSearchTextField.tsx | 14 +-- src/pages/panel/inventory/InventoryPage.tsx | 4 +- .../inventory-form/InventoryFormAccount.tsx | 27 +++-- .../InventoryFormCloudValues.tsx | 27 +++-- .../InventoryFormDefaultValue.tsx | 6 +- .../InventoryFormFilterRowStringValue.tsx | 2 +- .../inventory-form/InventoryFormKind.tsx | 7 +- .../InventoryFormRegionValues.tsx | 28 +++-- .../inventory-form/InventoryFormReset.tsx | 2 +- .../inventory-form/InventoryFormSeverity.tsx | 12 +- .../fix-query-parser/FixQueryContext.tsx | 112 ++++++++++-------- 13 files changed, 166 insertions(+), 131 deletions(-) diff --git a/src/locales/de-DE/messages.po b/src/locales/de-DE/messages.po index 788f0359..1794cb47 100644 --- a/src/locales/de-DE/messages.po +++ b/src/locales/de-DE/messages.po @@ -250,7 +250,7 @@ msgid "Access to your account is broken" msgstr "Der Zugriff auf Ihr Konto ist unterbrochen" #: src/pages/panel/benchmark-detail/BenchmarkDetailCheckDetail.tsx:195 -#: src/pages/panel/inventory/inventory-form/InventoryFormAccount.tsx:60 +#: src/pages/panel/inventory/inventory-form/InventoryFormAccount.tsx:65 #: src/pages/panel/resource-detail/ResourceDetailView.tsx:239 msgid "Account" msgstr "Konto" @@ -259,7 +259,7 @@ msgstr "Konto" msgid "Account: {0}" msgstr "Konto: {0}" -#: src/pages/panel/inventory/inventory-form/InventoryFormAccount.tsx:43 +#: src/pages/panel/inventory/inventory-form/InventoryFormAccount.tsx:57 #: src/pages/panel/inventory/inventory-form/utils/getAutoCompleteFromKey.tsx:125 #: src/pages/panel/inventory/InventoryTable.error.tsx:73 #: src/pages/panel/workspace-settings/WorkspaceSettingsPage.tsx:57 @@ -348,7 +348,7 @@ msgstr "Admin" msgid "Advanced" msgstr "" -#: src/pages/panel/inventory/InventoryAdvanceSearchTextField.tsx:85 +#: src/pages/panel/inventory/InventoryAdvanceSearchTextField.tsx:83 msgid "Advanced search query" msgstr "Erweiterte Suchabfrage" @@ -566,7 +566,7 @@ msgstr "Stornieren" msgid "Cannot enable this account" msgstr "" -#: src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx:211 +#: src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx:213 #: src/pages/panel/inventory/inventory-form/InventoryFormMoreValue.tsx:125 #: src/pages/panel/inventory/inventory-form/InventoryFormTagsValue.tsx:115 msgid "Change" @@ -665,7 +665,7 @@ msgid "Close" msgstr "Schließen" #: src/pages/panel/benchmark-detail/BenchmarkDetailCheckDetail.tsx:165 -#: src/pages/panel/inventory/inventory-form/InventoryFormCloudValues.tsx:57 +#: src/pages/panel/inventory/inventory-form/InventoryFormCloudValues.tsx:62 #: src/pages/panel/inventory/inventory-form/InventoryFormMore.tsx:96 #: src/pages/panel/resource-detail/ResourceDetailView.tsx:235 #: src/pages/panel/security/AccountCard.tsx:37 @@ -1341,7 +1341,7 @@ msgstr "Externen Benutzer einladen" msgid "Invites" msgstr "Lädt ein" -#: src/pages/panel/inventory/inventory-form/InventoryFormKind.tsx:42 +#: src/pages/panel/inventory/inventory-form/InventoryFormKind.tsx:45 msgid "kind" msgstr "" @@ -1351,7 +1351,7 @@ msgstr "" msgid "Kind" msgstr "Art" -#: src/pages/panel/inventory/inventory-form/InventoryFormKind.tsx:34 +#: src/pages/panel/inventory/inventory-form/InventoryFormKind.tsx:37 msgid "Kinds" msgstr "Arten" @@ -1530,7 +1530,7 @@ msgstr "Nächste Synchronisierung" msgid "No" msgstr "" -#: src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx:202 +#: src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx:204 msgid "No {label} found for {typed}" msgstr "" @@ -1633,7 +1633,7 @@ msgid "Oops, the username or password doesn't seem to match our records. Please msgstr "Hoppla, der Benutzername oder das Passwort scheinen nicht mit unseren Aufzeichnungen übereinzustimmen. Bitte versuche es erneut." #: src/pages/panel/inventory/InventoryAdvanceSearch.tsx:97 -#: src/pages/panel/inventory/InventoryAdvanceSearchTextField.tsx:114 +#: src/pages/panel/inventory/InventoryAdvanceSearchTextField.tsx:112 msgid "Oops! It looks like your query didn't match our format. Please check and try again." msgstr "Ups! Ihre Eingabe entspricht nicht unserem Format. Bitte überprüfen Sie sie und versuchen Sie es erneut." @@ -1853,7 +1853,7 @@ msgstr "Regenerieren" #: src/pages/panel/benchmark-detail/BenchmarkDetailCheckDetail.tsx:183 #: src/pages/panel/inventory/inventory-form/InventoryFormMore.tsx:97 -#: src/pages/panel/inventory/inventory-form/InventoryFormRegionValues.tsx:54 +#: src/pages/panel/inventory/inventory-form/InventoryFormRegionValues.tsx:60 #: src/pages/panel/resource-detail/ResourceDetailView.tsx:240 msgid "Region" msgstr "Region" @@ -1931,7 +1931,7 @@ msgstr "Risiko" msgid "Roles" msgstr "Rollen" -#: src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx:167 +#: src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx:169 msgid "Search {0}" msgstr "" @@ -2042,14 +2042,14 @@ msgstr "E-Mail-Benachrichtigungen einrichten" msgid "Setup cloud" msgstr "" -#: src/pages/panel/inventory/inventory-form/InventoryFormSeverity.tsx:40 -#: src/pages/panel/inventory/inventory-form/InventoryFormSeverity.tsx:56 +#: src/pages/panel/inventory/inventory-form/InventoryFormSeverity.tsx:46 +#: src/pages/panel/inventory/inventory-form/InventoryFormSeverity.tsx:60 msgid "Severities" msgstr "" #: src/pages/panel/benchmark-detail/BenchmarkCheckCollectionDetail.tsx:81 #: src/pages/panel/benchmark-detail/BenchmarkDetailView.tsx:102 -#: src/pages/panel/inventory/inventory-form/InventoryFormSeverity.tsx:50 +#: src/pages/panel/inventory/inventory-form/InventoryFormSeverity.tsx:54 #: src/pages/panel/inventory/inventory-form/utils/getAutoCompleteFromKey.tsx:156 #: src/pages/panel/workspace-settings/workspace-alerting-settings/WorkspaceAlertingSettings.tsx:163 #: src/pages/panel/workspace-settings/workspace-alerting-settings/WorkspaceAlertingSettings.tsx:217 diff --git a/src/locales/en-US/messages.po b/src/locales/en-US/messages.po index 15e93954..8ec19152 100644 --- a/src/locales/en-US/messages.po +++ b/src/locales/en-US/messages.po @@ -250,7 +250,7 @@ msgid "Access to your account is broken" msgstr "Access to your account is broken" #: src/pages/panel/benchmark-detail/BenchmarkDetailCheckDetail.tsx:195 -#: src/pages/panel/inventory/inventory-form/InventoryFormAccount.tsx:60 +#: src/pages/panel/inventory/inventory-form/InventoryFormAccount.tsx:65 #: src/pages/panel/resource-detail/ResourceDetailView.tsx:239 msgid "Account" msgstr "Account" @@ -259,7 +259,7 @@ msgstr "Account" msgid "Account: {0}" msgstr "Account: {0}" -#: src/pages/panel/inventory/inventory-form/InventoryFormAccount.tsx:43 +#: src/pages/panel/inventory/inventory-form/InventoryFormAccount.tsx:57 #: src/pages/panel/inventory/inventory-form/utils/getAutoCompleteFromKey.tsx:125 #: src/pages/panel/inventory/InventoryTable.error.tsx:73 #: src/pages/panel/workspace-settings/WorkspaceSettingsPage.tsx:57 @@ -348,7 +348,7 @@ msgstr "Admin" msgid "Advanced" msgstr "Advanced" -#: src/pages/panel/inventory/InventoryAdvanceSearchTextField.tsx:85 +#: src/pages/panel/inventory/InventoryAdvanceSearchTextField.tsx:83 msgid "Advanced search query" msgstr "Advanced search query" @@ -566,7 +566,7 @@ msgstr "Cancel" msgid "Cannot enable this account" msgstr "Cannot enable this account" -#: src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx:211 +#: src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx:213 #: src/pages/panel/inventory/inventory-form/InventoryFormMoreValue.tsx:125 #: src/pages/panel/inventory/inventory-form/InventoryFormTagsValue.tsx:115 msgid "Change" @@ -665,7 +665,7 @@ msgid "Close" msgstr "Close" #: src/pages/panel/benchmark-detail/BenchmarkDetailCheckDetail.tsx:165 -#: src/pages/panel/inventory/inventory-form/InventoryFormCloudValues.tsx:57 +#: src/pages/panel/inventory/inventory-form/InventoryFormCloudValues.tsx:62 #: src/pages/panel/inventory/inventory-form/InventoryFormMore.tsx:96 #: src/pages/panel/resource-detail/ResourceDetailView.tsx:235 #: src/pages/panel/security/AccountCard.tsx:37 @@ -1341,7 +1341,7 @@ msgstr "Invite External User" msgid "Invites" msgstr "Invites" -#: src/pages/panel/inventory/inventory-form/InventoryFormKind.tsx:42 +#: src/pages/panel/inventory/inventory-form/InventoryFormKind.tsx:45 msgid "kind" msgstr "kind" @@ -1351,7 +1351,7 @@ msgstr "kind" msgid "Kind" msgstr "Kind" -#: src/pages/panel/inventory/inventory-form/InventoryFormKind.tsx:34 +#: src/pages/panel/inventory/inventory-form/InventoryFormKind.tsx:37 msgid "Kinds" msgstr "Kinds" @@ -1530,7 +1530,7 @@ msgstr "Next Sync" msgid "No" msgstr "No" -#: src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx:202 +#: src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx:204 msgid "No {label} found for {typed}" msgstr "No {label} found for {typed}" @@ -1633,7 +1633,7 @@ msgid "Oops, the username or password doesn't seem to match our records. Please msgstr "Oops, the username or password doesn't seem to match our records. Please try again." #: src/pages/panel/inventory/InventoryAdvanceSearch.tsx:97 -#: src/pages/panel/inventory/InventoryAdvanceSearchTextField.tsx:114 +#: src/pages/panel/inventory/InventoryAdvanceSearchTextField.tsx:112 msgid "Oops! It looks like your query didn't match our format. Please check and try again." msgstr "Oops! It looks like your query didn't match our format. Please check and try again." @@ -1853,7 +1853,7 @@ msgstr "Regenerate" #: src/pages/panel/benchmark-detail/BenchmarkDetailCheckDetail.tsx:183 #: src/pages/panel/inventory/inventory-form/InventoryFormMore.tsx:97 -#: src/pages/panel/inventory/inventory-form/InventoryFormRegionValues.tsx:54 +#: src/pages/panel/inventory/inventory-form/InventoryFormRegionValues.tsx:60 #: src/pages/panel/resource-detail/ResourceDetailView.tsx:240 msgid "Region" msgstr "Region" @@ -1931,7 +1931,7 @@ msgstr "Risk" msgid "Roles" msgstr "Roles" -#: src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx:167 +#: src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx:169 msgid "Search {0}" msgstr "Search {0}" @@ -2042,14 +2042,14 @@ msgstr "Setting Up Email Notifications" msgid "Setup cloud" msgstr "Setup cloud" -#: src/pages/panel/inventory/inventory-form/InventoryFormSeverity.tsx:40 -#: src/pages/panel/inventory/inventory-form/InventoryFormSeverity.tsx:56 +#: src/pages/panel/inventory/inventory-form/InventoryFormSeverity.tsx:46 +#: src/pages/panel/inventory/inventory-form/InventoryFormSeverity.tsx:60 msgid "Severities" msgstr "Severities" #: src/pages/panel/benchmark-detail/BenchmarkCheckCollectionDetail.tsx:81 #: src/pages/panel/benchmark-detail/BenchmarkDetailView.tsx:102 -#: src/pages/panel/inventory/inventory-form/InventoryFormSeverity.tsx:50 +#: src/pages/panel/inventory/inventory-form/InventoryFormSeverity.tsx:54 #: src/pages/panel/inventory/inventory-form/utils/getAutoCompleteFromKey.tsx:156 #: src/pages/panel/workspace-settings/workspace-alerting-settings/WorkspaceAlertingSettings.tsx:163 #: src/pages/panel/workspace-settings/workspace-alerting-settings/WorkspaceAlertingSettings.tsx:217 diff --git a/src/pages/panel/inventory/InventoryAdvanceSearchTextField.tsx b/src/pages/panel/inventory/InventoryAdvanceSearchTextField.tsx index 2005c2d8..d52f2a1f 100644 --- a/src/pages/panel/inventory/InventoryAdvanceSearchTextField.tsx +++ b/src/pages/panel/inventory/InventoryAdvanceSearchTextField.tsx @@ -16,7 +16,7 @@ export const InventoryAdvanceSearchTextField = ({ hasError }: InventoryAdvanceSe const [hasTextError, setHasTextError] = useState(false) const { updateQuery, reset, error, q } = useFixQueryParser() - const [searchCritValue, setSearchCritValue] = useState(!q && q !== 'all' ? '' : q) + const [searchCritValue, setSearchCritValue] = useState(q ?? '') const timeoutRef = useRef() @@ -69,8 +69,6 @@ export const InventoryAdvanceSearchTextField = ({ hasError }: InventoryAdvanceSe setHasTextError(false) } - const searchCritValueWithoutAll = searchCritValue === 'all' ? '' : searchCritValue - return ( <> - + + Advanced search query @@ -88,16 +86,16 @@ export const InventoryAdvanceSearchTextField = ({ hasError }: InventoryAdvanceSe fullWidth size="small" inputProps={{ sx: { ml: 1 } }} - value={searchCritValueWithoutAll} + value={searchCritValue} onChange={handleChange} onFocus={() => setIsFocused(true)} onBlur={() => setIsFocused(false)} onReset={reset} InputProps={{ - startAdornment: focused || searchCritValueWithoutAll ? : undefined, + startAdornment: focused || searchCritValue ? : undefined, endAdornment: ( - {searchCritValueWithoutAll ? ( + {searchCritValue ? ( diff --git a/src/pages/panel/inventory/InventoryPage.tsx b/src/pages/panel/inventory/InventoryPage.tsx index 04f7eded..1ab4352e 100644 --- a/src/pages/panel/inventory/InventoryPage.tsx +++ b/src/pages/panel/inventory/InventoryPage.tsx @@ -25,7 +25,7 @@ export default function InventoryPage() { const handleSetSearchCrit = useCallback( (crit?: string) => { const searchValues = getLocationSearchValues() - if (!crit || crit === 'all') { + if (!crit) { setHasError(false) delete searchValues['q'] } else { @@ -79,7 +79,7 @@ export default function InventoryPage() { - {searchCrit && (searchCrit !== 'all' || (hasChanges && history.changes.length) || hasError) ? ( + {searchCrit || hasChanges || hasError ? ( <> ( diff --git a/src/pages/panel/inventory/inventory-form/InventoryFormAccount.tsx b/src/pages/panel/inventory/inventory-form/InventoryFormAccount.tsx index 103d8cc9..5871cf7c 100644 --- a/src/pages/panel/inventory/inventory-form/InventoryFormAccount.tsx +++ b/src/pages/panel/inventory/inventory-form/InventoryFormAccount.tsx @@ -1,7 +1,7 @@ import { t } from '@lingui/macro' // import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown' // import ArrowDropUpIcon from '@mui/icons-material/ArrowDropUp' -import { useMemo, useState } from 'react' +import { useCallback, useMemo, useState } from 'react' import { useFixQueryParser } from 'src/shared/fix-query-parser' import { InventoryFormDefaultValue } from './InventoryFormDefaultValue' import { InventoryFormField } from './InventoryFormField' @@ -36,6 +36,20 @@ export const InventoryFormAccount = ({ preItems }: { preItems: AutoCompletePreDe // eslint-disable-next-line react-hooks/exhaustive-deps }, [JSON.stringify(values), preItems.accounts]) + const handleChange = useCallback( + (values: string[]) => { + if (isId) { + deleteCloudAccountRegion('account') + } + if (values) { + setCloudAccountRegion('account', 'in', values, true) + } else { + deleteCloudAccountRegion('account', true) + } + }, + [deleteCloudAccountRegion, isId, setCloudAccountRegion], + ) + return ( <> : } /> { - if (isId) { - deleteCloudAccountRegion('account') - } - if (values) { - setCloudAccountRegion('account', 'in', values, true) - } else { - deleteCloudAccountRegion('account', true) - } - }} + onChange={handleChange} values={values} label={t`Account`} onClose={() => setOpen(null)} diff --git a/src/pages/panel/inventory/inventory-form/InventoryFormCloudValues.tsx b/src/pages/panel/inventory/inventory-form/InventoryFormCloudValues.tsx index e37ae63e..28da1b82 100644 --- a/src/pages/panel/inventory/inventory-form/InventoryFormCloudValues.tsx +++ b/src/pages/panel/inventory/inventory-form/InventoryFormCloudValues.tsx @@ -1,6 +1,6 @@ import { t } from '@lingui/macro' import { Stack, Typography } from '@mui/material' -import { useMemo } from 'react' +import { useCallback, useMemo } from 'react' import { CloudAvatar } from 'src/shared/cloud-avatar' import { useFixQueryParser } from 'src/shared/fix-query-parser' import { InventoryFormDefaultValue } from './InventoryFormDefaultValue' @@ -41,18 +41,23 @@ export const InventoryFormCloudValues = ({ preItems, onClose, open, values }: In // eslint-disable-next-line react-hooks/exhaustive-deps }, [JSON.stringify(values), preItems.clouds]) + const handleChange = useCallback( + (values: string[]) => { + if (isId) { + deleteCloudAccountRegion('cloud') + } + if (values) { + setCloudAccountRegion('cloud', 'in', values, true) + } else { + deleteCloudAccountRegion('cloud', true) + } + }, + [deleteCloudAccountRegion, isId, setCloudAccountRegion], + ) + return ( { - if (isId) { - deleteCloudAccountRegion('cloud') - } - if (values) { - setCloudAccountRegion('cloud', 'in', values, true) - } else { - deleteCloudAccountRegion('cloud', true) - } - }} + onChange={handleChange} values={curValues} label={t`Cloud`} onClose={onClose} diff --git a/src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx b/src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx index 52c49558..3363b7ef 100644 --- a/src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx +++ b/src/pages/panel/inventory/inventory-form/InventoryFormDefaultValue.tsx @@ -23,7 +23,7 @@ import { backdropClasses, } from '@mui/material' import { useDebounce } from '@uidotdev/usehooks' -import { ReactNode, useEffect, useId, useState } from 'react' +import { ReactNode, useEffect, useId, useRef, useState } from 'react' import { panelUI } from 'src/shared/constants' import { AutoCompleteValue } from 'src/shared/types/shared' @@ -108,9 +108,11 @@ export const InventoryFormDefaultValue = ({ const [typed, setTyped] = useState('') const [selectedValues, setSelectedValues] = useState(values) const debouncedValues = useDebounce(values, panelUI.fastInputChangeDebounce) + const previousValue = useRef(debouncedValues) useEffect(() => { - if (open) { + if (open && JSON.stringify(previousValue.current) !== JSON.stringify(debouncedValues)) { + previousValue.current = debouncedValues onChange(debouncedValues) } }, [debouncedValues, onChange, open]) diff --git a/src/pages/panel/inventory/inventory-form/InventoryFormFilterRowStringValue.tsx b/src/pages/panel/inventory/inventory-form/InventoryFormFilterRowStringValue.tsx index 3c1e38e5..f7881d75 100644 --- a/src/pages/panel/inventory/inventory-form/InventoryFormFilterRowStringValue.tsx +++ b/src/pages/panel/inventory/inventory-form/InventoryFormFilterRowStringValue.tsx @@ -64,7 +64,7 @@ export function InventoryFormFilterRowStringValue query.delete_predicate(propertyName), [propertyName, query]).toString() + const q = useMemo(() => query?.delete_predicate(propertyName), [propertyName, query])?.toString() ?? 'all' const { data = null, isLoading, diff --git a/src/pages/panel/inventory/inventory-form/InventoryFormKind.tsx b/src/pages/panel/inventory/inventory-form/InventoryFormKind.tsx index 1eccb06e..82a20418 100644 --- a/src/pages/panel/inventory/inventory-form/InventoryFormKind.tsx +++ b/src/pages/panel/inventory/inventory-form/InventoryFormKind.tsx @@ -1,7 +1,7 @@ import { t } from '@lingui/macro' // import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown' // import ArrowDropUpIcon from '@mui/icons-material/ArrowDropUp' -import { useMemo, useState } from 'react' +import { useCallback, useMemo, useState } from 'react' import { useFixQueryParser } from 'src/shared/fix-query-parser' import { InventoryFormDefaultValue } from './InventoryFormDefaultValue' import { InventoryFormField } from './InventoryFormField' @@ -27,6 +27,9 @@ export const InventoryFormKind = ({ preItems }: { preItems: AutoCompletePreDefin // eslint-disable-next-line react-hooks/exhaustive-deps }, [JSON.stringify(values), preItems.kinds]) const [open, setOpen] = useState(null) + + const handleChange = useCallback((values: string[]) => (values.length ? setIs(values) : deleteIs()), [deleteIs, setIs]) + return ( <> : } /> (values.length ? setIs(values) : deleteIs())} + onChange={handleChange} values={values} label={t`kind`} onClose={() => setOpen(null)} diff --git a/src/pages/panel/inventory/inventory-form/InventoryFormRegionValues.tsx b/src/pages/panel/inventory/inventory-form/InventoryFormRegionValues.tsx index 1988f2d2..49e46a60 100644 --- a/src/pages/panel/inventory/inventory-form/InventoryFormRegionValues.tsx +++ b/src/pages/panel/inventory/inventory-form/InventoryFormRegionValues.tsx @@ -1,5 +1,5 @@ import { t } from '@lingui/macro' -import { useMemo } from 'react' +import { useCallback, useMemo } from 'react' import { useFixQueryParser } from 'src/shared/fix-query-parser' import { InventoryFormDefaultValue } from './InventoryFormDefaultValue' import { termValueToStringArray } from './utils' @@ -38,18 +38,24 @@ export const InventoryFormRegionValues = ({ preItems, onClose, open, values }: I return result // eslint-disable-next-line react-hooks/exhaustive-deps }, [JSON.stringify(values), preItems.regions]) + + const handleChange = useCallback( + (values: string[]) => { + if (isId) { + deleteCloudAccountRegion('region') + } + if (values) { + setCloudAccountRegion('region', 'in', values, true) + } else { + deleteCloudAccountRegion('region', true) + } + }, + [deleteCloudAccountRegion, isId, setCloudAccountRegion], + ) + return ( { - if (isId) { - deleteCloudAccountRegion('region') - } - if (values) { - setCloudAccountRegion('region', 'in', values, true) - } else { - deleteCloudAccountRegion('region', true) - } - }} + onChange={handleChange} values={curValues} label={t`Region`} onClose={onClose} diff --git a/src/pages/panel/inventory/inventory-form/InventoryFormReset.tsx b/src/pages/panel/inventory/inventory-form/InventoryFormReset.tsx index 626dd58e..244452c0 100644 --- a/src/pages/panel/inventory/inventory-form/InventoryFormReset.tsx +++ b/src/pages/panel/inventory/inventory-form/InventoryFormReset.tsx @@ -5,7 +5,7 @@ import { useFixQueryParser } from 'src/shared/fix-query-parser' export const InventoryFormReset = () => { const { q, reset } = useFixQueryParser() - return q && q !== 'all' ? ( + return q ? ( + values.length ? setPredicate(DefaultPropertiesKeys.Severity, 'in', values) : deletePredicate(DefaultPropertiesKeys.Severity), + [deletePredicate, setPredicate], + ) + return ( <> : } /> - values.length ? setPredicate(DefaultPropertiesKeys.Severity, 'in', values) : deletePredicate(DefaultPropertiesKeys.Severity) - } + onChange={handleChange} values={values} label={t`Severity`} showItemLabel={(item) => ( diff --git a/src/shared/fix-query-parser/FixQueryContext.tsx b/src/shared/fix-query-parser/FixQueryContext.tsx index a5f1431b..548994d1 100644 --- a/src/shared/fix-query-parser/FixQueryContext.tsx +++ b/src/shared/fix-query-parser/FixQueryContext.tsx @@ -3,6 +3,8 @@ import { PropsWithChildren, createContext, memo } from 'react' import { WorkspaceInventorySearchTableHistory } from 'src/shared/types/server' import { JsonElement, Query } from './query' +type ReturnTypeWithUndefined = T extends (...args: infer Args) => unknown ? (...args: Args) => ReturnType | undefined : never + type CombineType = Query['combine'] type DeleteIsType = Query['delete_is'] type DeletePredicateType = Query['delete_predicate'] @@ -14,15 +16,15 @@ type UpdateFullTextType = Query['update_fulltext'] export type FixQueryContextValue = { // default variables - error: Error | undefined - query: Query - account: Query['account'] - cloud: Query['cloud'] - parts: Query['parts'] - region: Query['region'] - severity: Query['severity'] + error?: Error + query: Query | undefined + account?: Query['account'] + cloud?: Query['cloud'] + parts?: Query['parts'] + region?: Query['region'] + severity?: Query['severity'] tags: Query['tags'] - aggregate: Query['aggregate'] + aggregate?: Query['aggregate'] sorts: Query['sorts'] // get fullTextSearches: ReturnType @@ -35,19 +37,19 @@ export type FixQueryContextValue = { // get methods findPaths: Query['find_paths'] // update - combine: CombineType - deleteIs: DeleteIsType - deletePredicate: DeletePredicateType - deleteCloudAccountRegion: DeleteCloudAccountRegionType - setIs: (kinds: string[]) => Query - setPredicate: (name: string, op: string, value: JsonElement) => Query - setCloudAccountRegion: (name: 'cloud' | 'account' | 'region', op: string, value: JsonElement, useName?: boolean) => Query - changeCloudAccountRegionFromIdToName: (name: 'cloud' | 'account' | 'region', newValue: JsonElement, op?: string) => Query - updateFullTextSearch: UpdateFullTextType + combine: ReturnTypeWithUndefined + deleteIs: ReturnTypeWithUndefined + deletePredicate: ReturnTypeWithUndefined + deleteCloudAccountRegion: ReturnTypeWithUndefined + setIs: (kinds: string[]) => Query | undefined + setPredicate: (name: string, op: string, value: JsonElement) => Query | undefined + setCloudAccountRegion: (name: 'cloud' | 'account' | 'region', op: string, value: JsonElement, useName?: boolean) => Query | undefined + changeCloudAccountRegionFromIdToName: (name: 'cloud' | 'account' | 'region', newValue: JsonElement, op?: string) => Query | undefined + updateFullTextSearch: ReturnTypeWithUndefined deleteMatching: Query['delete_matching'] - updateQuery: (q: string) => Query + updateQuery: (q: string) => Query | undefined onHistoryChange: (history?: WorkspaceInventorySearchTableHistory) => void - reset: () => Query + reset: () => Query | undefined } export const FixQueryContext = createContext(null) @@ -62,25 +64,32 @@ interface FixQueryProviderProps extends PropsWithChildren { export const FixQueryProvider = memo( ({ searchQuery, onChange, history, onHistoryChange, children }: FixQueryProviderProps) => { useLingui() - let query = Query.parse('all') + let query: Query | undefined + let queryOrAll = query ?? Query.parse('all') let error: Error | undefined - try { - query = Query.parse(searchQuery || 'all') - } catch (err) { - error = err as Error + if (searchQuery) { + try { + queryOrAll = query = Query.parse(searchQuery) + } catch (err) { + error = err as Error + } } function forceUpdateAndCall(fn: (...args: Args) => Query) { return (...params: Args) => { const query = fn(...params) const result = query.toString() + if (result === 'all') { + onChange('') + return undefined + } onChange(result) return query } } - const { account, cloud, parts, region, severity, tags, aggregate, sorts } = query + const { account, cloud, parts = [], region, severity, tags = {}, aggregate, sorts = [] } = query || {} const value = { //history history, @@ -97,37 +106,41 @@ export const FixQueryProvider = memo( aggregate, sorts, // get - fullTextSearches: query.fulltexts(), - is: query.is(), - q: query.toString(), - predicates: query.predicates(), - provides_security_check_details: query.provides_security_check_details(), - uiSimpleQuery: query.ui_simple_query(), + fullTextSearches: query?.fulltexts() ?? [], + is: query?.is(), + q: query?.toString() ?? '', + predicates: query?.predicates() ?? [], + provides_security_check_details: query?.provides_security_check_details() ?? false, + uiSimpleQuery: query?.ui_simple_query() ?? true, // get methods - findPaths: query.find_paths.bind(query), + findPaths: query?.find_paths.bind(query) ?? (() => []), // update - combine: forceUpdateAndCall>(query.combine.bind(query)), - deleteIs: forceUpdateAndCall>(query.delete_is.bind(query)), - deletePredicate: forceUpdateAndCall>(query.delete_predicate.bind(query)), - deleteCloudAccountRegion: forceUpdateAndCall>(query.delete_cloud_account_region.bind(query)), - setIs: forceUpdateAndCall>(query.set_is.bind(query)), - setPredicate: forceUpdateAndCall>(query.set_predicate.bind(query)), - setCloudAccountRegion: forceUpdateAndCall>(query.set_cloud_account_region.bind(query)), - updateFullTextSearch: forceUpdateAndCall>(query.update_fulltext.bind(query)), - deleteMatching: query.delete_matching.bind(query), + combine: forceUpdateAndCall>(queryOrAll.combine.bind(queryOrAll)), + deleteIs: forceUpdateAndCall>(queryOrAll.delete_is.bind(queryOrAll)), + deletePredicate: forceUpdateAndCall>(queryOrAll.delete_predicate.bind(queryOrAll)), + deleteCloudAccountRegion: forceUpdateAndCall>( + queryOrAll.delete_cloud_account_region.bind(queryOrAll), + ), + setIs: forceUpdateAndCall>(queryOrAll.set_is.bind(queryOrAll)), + setPredicate: forceUpdateAndCall>(queryOrAll.set_predicate.bind(queryOrAll)), + setCloudAccountRegion: forceUpdateAndCall>( + queryOrAll.set_cloud_account_region.bind(queryOrAll), + ), + updateFullTextSearch: forceUpdateAndCall>(queryOrAll.update_fulltext.bind(queryOrAll)), + deleteMatching: query?.delete_matching.bind(queryOrAll) ?? (() => null), changeCloudAccountRegionFromIdToName: (name: 'cloud' | 'account' | 'region', newValue: JsonElement, op?: string) => { const prevPredicate = name === 'cloud' ? cloud : name === 'account' ? account : region - const newQuery = query - .delete_cloud_account_region(name) + const newQuery = queryOrAll + ?.delete_cloud_account_region(name) .set_cloud_account_region(name, op ?? prevPredicate?.op ?? 'in', newValue, true) - const result = newQuery.toString() + const result = newQuery.toString() ?? '' onChange(result) return newQuery }, updateQuery: (q: string) => { - if (query.toString() !== (q || 'all')) { - const newQuery = Query.parse(q || 'all') - const result = newQuery.toString() + if ((query?.toString() ?? '') !== q) { + const newQuery = q ? Query.parse(q) : undefined + const result = newQuery?.toString() ?? '' window.setTimeout(() => { onChange(result) }) @@ -137,11 +150,10 @@ export const FixQueryProvider = memo( } }, reset: () => { - const query = Query.parse('all') window.setTimeout(() => { - onChange('all') + onChange('') }) - return query + return undefined }, }