Skip to content

Commit b7f4756

Browse files
committed
Update all query param configs utils to manage keys as well. Set date range preset default to fix condition (and align with existing utils)
1 parent 59a940e commit b7f4756

File tree

7 files changed

+50
-38
lines changed

7 files changed

+50
-38
lines changed

frontend/src/lib/components/ActionButtons/ActionButtons.svelte

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import IconSquare3Stack3d from '~icons/heroicons/square-3-stack-3d-16-solid';
1010
import IconXMark from '~icons/heroicons/x-mark-16-solid';
1111
12-
import { getSortParamKey, type SortContext, getSortParamConfig } from '$lib/util/sort';
12+
import { getSortParamKey, type SortContext, getSortParamsConfig } from '$lib/util/sort';
1313
1414
let {
1515
showCluster = false,
@@ -27,10 +27,7 @@
2727
2828
const sortKey = $derived(getSortParamKey(context));
2929
const params = $derived(
30-
queryParameters(
31-
{ [sortKey]: getSortParamConfig() },
32-
{ pushHistory: false, showDefaults: false }
33-
)
30+
queryParameters(getSortParamsConfig(context), { pushHistory: false, showDefaults: false })
3431
);
3532
3633
function toggleSort() {

frontend/src/lib/components/DateRangeSelector/DateRangeSelector.svelte

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import { untrack } from 'svelte';
3-
import { queryParameters, ssp } from 'sveltekit-search-params';
3+
import { queryParameters } from 'sveltekit-search-params';
44
import type { DateRange } from 'bits-ui';
55
import { DateFormatter, getLocalTimeZone, fromAbsolute } from '@internationalized/date';
66
@@ -18,15 +18,12 @@
1818
1919
import { cn } from '$lib/utils';
2020
import { RangeCalendar } from '$lib/components/ui/range-calendar/index';
21+
import { getDateRangeParamsConfig } from '$lib/util/date-ranges';
2122
22-
const params = queryParameters(
23-
{
24-
[DATE_RANGE_PARAM]: ssp.string(),
25-
[DATE_RANGE_START_PARAM]: ssp.number(),
26-
[DATE_RANGE_END_PARAM]: ssp.number()
27-
},
28-
{ pushHistory: false, showDefaults: false }
29-
);
23+
const params = queryParameters(getDateRangeParamsConfig(), {
24+
pushHistory: false,
25+
showDefaults: false
26+
});
3027
3128
const df = new DateFormatter('en-US', {
3229
dateStyle: 'medium'

frontend/src/lib/components/MetricTypeToggle/MetricTypeToggle.svelte

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import {
66
METRIC_LABELS,
77
METRIC_TYPES,
8-
getMetricTypeParamConfig
8+
getMetricTypeParamsConfig
99
} from '$lib/types/MetricType/MetricType';
1010
11-
const params = queryParameters(
12-
{ metric: getMetricTypeParamConfig() },
13-
{ pushHistory: false, showDefaults: false }
14-
);
11+
const params = queryParameters(getMetricTypeParamsConfig(), {
12+
pushHistory: false,
13+
showDefaults: false
14+
});
1515
</script>
1616

1717
<div class="flex space-x-[1px]">

frontend/src/lib/types/MetricType/MetricType.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@ export const METRIC_SCALES: Record<MetricType, { min: number; max: number }> = {
1717
psi: { min: 0, max: 25 }
1818
};
1919

20-
export function getMetricTypeParamConfig() {
20+
export function getMetricTypeParamsConfig() {
2121
return {
22-
encode: (value) => value,
23-
decode: (value) => (METRIC_TYPES.includes(value as MetricType) ? (value as MetricType) : null),
24-
defaultValue: DEFAULT_METRIC_TYPE
25-
} satisfies EncodeAndDecodeOptions<MetricType>;
22+
metric: {
23+
encode: (value) => value,
24+
decode: (value) =>
25+
METRIC_TYPES.includes(value as MetricType) ? (value as MetricType) : null,
26+
defaultValue: DEFAULT_METRIC_TYPE
27+
} satisfies EncodeAndDecodeOptions<MetricType>
28+
};
2629
}
2730

2831
export function getMetricTypeFromParams(searchParams: URLSearchParams): MetricType {
29-
const metric = searchParams.get('metric');
30-
return METRIC_TYPES.includes(metric as MetricType) ? (metric as MetricType) : DEFAULT_METRIC_TYPE;
32+
const paramsConfig = getMetricTypeParamsConfig();
33+
const [paramName, paramConfig] = Object.entries(paramsConfig)[0]; // single `metric` config
34+
const value = searchParams.get(paramName);
35+
return paramConfig.decode(value) ?? paramConfig.defaultValue;
3136
}

frontend/src/lib/util/date-ranges.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
PAST_1_WEEK,
66
getDateRangeByValue
77
} from '$lib/constants/date-ranges';
8+
import { ssp } from 'sveltekit-search-params';
89

910
export function getDateRange(range: string): [number, number] {
1011
const dateRange = getDateRangeByValue(range);
@@ -32,3 +33,11 @@ export function parseDateRangeParams(searchParams: URLSearchParams) {
3233
endTimestamp
3334
};
3435
}
36+
37+
export function getDateRangeParamsConfig() {
38+
return {
39+
[DATE_RANGE_PARAM]: ssp.string(PAST_1_WEEK),
40+
[DATE_RANGE_START_PARAM]: ssp.number(),
41+
[DATE_RANGE_END_PARAM]: ssp.number()
42+
};
43+
}

frontend/src/lib/util/sort.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ export function getSortParamKey(context: SortContext): string {
99
return `${context}Sort`;
1010
}
1111

12-
export function getSortParamConfig() {
12+
export function getSortParamsConfig(context: SortContext) {
13+
const sortKey = getSortParamKey(context);
1314
return {
14-
encode: (value) => value,
15-
decode: (value) =>
16-
SORT_DIRECTIONS.includes(value as SortDirection) ? (value as SortDirection) : null,
17-
defaultValue: 'asc'
18-
} satisfies EncodeAndDecodeOptions<SortDirection>;
15+
[sortKey]: {
16+
encode: (value) => value,
17+
decode: (value) =>
18+
SORT_DIRECTIONS.includes(value as SortDirection) ? (value as SortDirection) : null,
19+
defaultValue: 'asc'
20+
} satisfies EncodeAndDecodeOptions<SortDirection>
21+
};
1922
}
2023

2124
export function getSortDirection(

frontend/src/routes/joins/[slug]/+page.svelte

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import { handleChartHighlight } from '$lib/util/chart';
3232
import ChartControls from '$lib/components/ChartControls/ChartControls.svelte';
3333
import {
34-
getSortParamConfig,
34+
getSortParamsConfig,
3535
getSortParamKey,
3636
sortDistributions,
3737
type SortContext
@@ -452,11 +452,12 @@
452452
loadDistributions();
453453
});
454454
455-
const sortKey = getSortParamKey('distributions');
456-
const params = queryParameters(
457-
{ [sortKey]: getSortParamConfig() },
458-
{ pushHistory: false, showDefaults: false }
459-
);
455+
const sortContext = 'distributions';
456+
const sortKey = getSortParamKey(sortContext);
457+
const params = queryParameters(getSortParamsConfig(sortContext), {
458+
pushHistory: false,
459+
showDefaults: false
460+
});
460461
const sortedDistributions = $derived(sortDistributions(distributions, params[sortKey]));
461462
462463
let selectedTab = $state<SortContext>('drift');

0 commit comments

Comments
 (0)