-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Group URLs in Select component (#443)
- Loading branch information
1 parent
c01750a
commit 14f94da
Showing
3 changed files
with
104 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/views/Generator/TestOptions/Thresholds/Thresholds.hooks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { selectFilteredRequests, useGeneratorStore } from '@/store/generator' | ||
|
||
type GroupItem = { path: string; protocol: string } | ||
|
||
export const useThresholdURLOptions = () => { | ||
const requests = useGeneratorStore(selectFilteredRequests) | ||
|
||
// Group requests by host | ||
const groupedRequests = requests.reduce<Record<string, GroupItem[]>>( | ||
(acc, { request }) => { | ||
const { host, path, url } = request | ||
const { protocol } = new URL(url) | ||
if (!acc[host]) { | ||
acc[host] = [] | ||
} | ||
acc[host].push({ path, protocol }) | ||
|
||
return acc | ||
}, | ||
{} | ||
) | ||
|
||
// Flatten the results so it can be used in the Select component | ||
const urlOptionsMap = Object.keys(groupedRequests).map((host) => { | ||
const addedPaths = new Set<string>() | ||
const options: { label: string; value: string }[] = [] | ||
|
||
// Add the requests for each host | ||
groupedRequests[host]?.forEach((request) => { | ||
const { protocol, path } = request | ||
const fullPath = new URL(`${protocol}//${host}${path}`).href | ||
|
||
if (!addedPaths.has(fullPath)) { | ||
options.push({ | ||
label: path, | ||
value: fullPath, | ||
}) | ||
addedPaths.add(fullPath) | ||
} | ||
}) | ||
|
||
return { | ||
label: host, | ||
options, | ||
} | ||
}) | ||
|
||
return [{ label: 'Across all URLs', value: '*' }, ...urlOptionsMap] | ||
} |