|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and Juno contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +const ACTIVE_FILTERS = "f" |
| 7 | +const SEARCH_TERM = "s" |
| 8 | +const DETAILS_VIOLATION_GROUP = "v" |
| 9 | + |
| 10 | +export const readLegacyUrlState = (state: any) => { |
| 11 | + const activeFilters = state?.[ACTIVE_FILTERS] |
| 12 | + const searchTerm = state?.[SEARCH_TERM] |
| 13 | + const violationGroup = state?.[DETAILS_VIOLATION_GROUP] |
| 14 | + |
| 15 | + return { |
| 16 | + activeFilters, |
| 17 | + searchTerm, |
| 18 | + violationGroup, |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +export const extractSearchStringFromHashFragment = (searchString: string) => { |
| 23 | + /* |
| 24 | + * In case of hashed routing Tanstack router returns URL search params of the entire URL rather than just from the hashed part. |
| 25 | + * We'll have to extract the query part from the hash because otherwise in embedded mode the app will be taking search params from the shell app as well. |
| 26 | + * Sanitize the search string by extracting the substring between the first '?' and the next '?' (if any), keeping the first '?'. |
| 27 | + * https://github.com/TanStack/router/issues/4370 |
| 28 | + * http://localhost:3000/?preHashParam=prehashtest#/services?postHashParam1=test1?preHashParam=prehashtest |
| 29 | + * searchString = "?postHashParam1=test1?preHashParam=prehashtest" |
| 30 | + * searchStringFromHash = "?postHashParam1=test1" |
| 31 | + */ |
| 32 | + const postHashParams = searchString.indexOf("?") |
| 33 | + if (postHashParams === -1) return "" // If no query part is found, return an empty object |
| 34 | + const preHashParams = searchString.indexOf("?", postHashParams + 1) |
| 35 | + return searchString.slice(postHashParams, preHashParams === -1 ? undefined : preHashParams) |
| 36 | +} |
| 37 | + |
| 38 | +export const getFiltersForUrl = (prefix: string, filters: any) => |
| 39 | + filters.reduce((acc: Record<string, string | string[]>, filter: any) => { |
| 40 | + // if the filter key already exists, convert the value to an array and add the new value |
| 41 | + if (acc[`${prefix}${filter.key}`]) { |
| 42 | + if (Array.isArray(acc[`${prefix}${filter.key}`])) { |
| 43 | + ;(acc[`${prefix}${filter.key}`] as string[]).push(filter.value) |
| 44 | + } else { |
| 45 | + acc[`${prefix}${filter.key}`] = [acc[`${prefix}${filter.key}`], filter.value] |
| 46 | + } |
| 47 | + } else { |
| 48 | + acc[`${prefix}${filter.key}`] = filter.value |
| 49 | + } |
| 50 | + return acc |
| 51 | + }, {}) |
| 52 | + |
| 53 | +export const convertAppStateToUrlState = (appState: any) => { |
| 54 | + const activeFiltersForUrl = getFiltersForUrl("f_", appState.activeFilters || {}) |
| 55 | + |
| 56 | + return { |
| 57 | + ...activeFiltersForUrl, |
| 58 | + searchTerm: appState.searchTerm || undefined, |
| 59 | + violationGroup: appState.violationGroup || undefined, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +export const getFiltersForApp = (prefix: string, urlState: Record<string, string | string[]>) => { |
| 64 | + return Object.entries(urlState) |
| 65 | + .filter(([key]) => key.startsWith(prefix)) |
| 66 | + .reduce((acc: Array<{ key: string; value: string }>, [key, value]) => { |
| 67 | + const filterKey = key.replace(prefix, "") |
| 68 | + if (value === undefined || value === null) return acc |
| 69 | + // if the value is an array, add each value as a separate filter |
| 70 | + if (Array.isArray(value)) { |
| 71 | + acc = [ |
| 72 | + ...acc, |
| 73 | + ...value.map((v) => ({ |
| 74 | + key: filterKey, |
| 75 | + value: v.trim(), |
| 76 | + })), |
| 77 | + ] |
| 78 | + } |
| 79 | + // if the value is a string, add it as a single filter |
| 80 | + else if (typeof value === "string") { |
| 81 | + acc.push({ key: filterKey, value: value.trim() }) |
| 82 | + } |
| 83 | + return acc |
| 84 | + }, []) |
| 85 | +} |
| 86 | + |
| 87 | +export const convertUrlStateToAppState = (urlState: any) => { |
| 88 | + return { |
| 89 | + activeFilters: getFiltersForApp("f_", urlState), |
| 90 | + searchTerm: urlState.searchTerm, |
| 91 | + violationGroup: urlState.violationGroup, |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// removes a specific filter from the filters object and returns a new object |
| 96 | +export const removeFilter = (filters: any, filterKey: string, filterValue: string) => { |
| 97 | + // if filter has more than one value, remove the specific value |
| 98 | + if (Array.isArray(filters?.[filterKey])) { |
| 99 | + const updatedFilters: any = { |
| 100 | + ...filters, |
| 101 | + [filterKey]: filters?.[filterKey].filter((value: string) => value !== filterValue), |
| 102 | + } |
| 103 | + if (updatedFilters?.[filterKey]?.length === 0) { |
| 104 | + delete updatedFilters[filterKey] |
| 105 | + } |
| 106 | + return updatedFilters |
| 107 | + } |
| 108 | + |
| 109 | + // if filter is a single value, remove the filter key |
| 110 | + const updatedFilters = { ...filters } |
| 111 | + if (updatedFilters?.[filterKey] === filterValue) delete updatedFilters[filterKey] |
| 112 | + |
| 113 | + return updatedFilters |
| 114 | +} |
| 115 | + |
| 116 | +// adds a specific filter to the filters object and returns a new object |
| 117 | +export const addFilter = (filters: any, filterKey: string, filterValue: string) => { |
| 118 | + // if the filter already exists, add the value to the existing array |
| 119 | + if (filters?.[filterKey] && Array.isArray(filters[filterKey])) { |
| 120 | + // Create a Set from existing values to remove duplicates |
| 121 | + const filterSet = new Set(filters[filterKey]) |
| 122 | + // Add the new value |
| 123 | + filterSet.add(filterValue) |
| 124 | + // Convert back to array |
| 125 | + return { |
| 126 | + ...filters, |
| 127 | + [filterKey]: Array.from(filterSet), |
| 128 | + } |
| 129 | + } |
| 130 | + // If the filter is a single value, convert it to an array |
| 131 | + if (filters?.[filterKey] && typeof filters[filterKey] === "string") { |
| 132 | + return { |
| 133 | + ...filters, |
| 134 | + [filterKey]: [filters[filterKey], filterValue], |
| 135 | + } |
| 136 | + } |
| 137 | + // If the filter does not exist, create a new key with the value |
| 138 | + return { |
| 139 | + ...filters, |
| 140 | + [filterKey]: filterValue, |
| 141 | + } |
| 142 | +} |
0 commit comments