Skip to content

Commit 66291d9

Browse files
refactor: create composable for permalinkValue handling
1 parent a1ffda0 commit 66291d9

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

app/components/Package/TrendsChart.vue

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -320,19 +320,13 @@ const effectivePackageNames = computed<string[]>(() => {
320320
return single ? [single] : []
321321
})
322322
323-
const granularityLocal = shallowRef<ChartTimeGranularity>(DEFAULT_GRANULARITY)
324-
const granularityRoute = useRouteQuery<ChartTimeGranularity>('granularity', DEFAULT_GRANULARITY)
325-
326-
const selectedGranularity = computed({
327-
get: () => (props.permalink ? granularityRoute.value : granularityLocal.value),
328-
set: (value: ChartTimeGranularity) => {
329-
if (props.permalink) {
330-
granularityRoute.value = value
331-
} else {
332-
granularityLocal.value = value
333-
}
323+
const selectedGranularity = usePermalinkValue<ChartTimeGranularity>(
324+
'granularity',
325+
DEFAULT_GRANULARITY,
326+
{
327+
permanent: props.permalink,
334328
},
335-
})
329+
)
336330
337331
const displayedGranularity = shallowRef<ChartTimeGranularity>(DEFAULT_GRANULARITY)
338332
@@ -363,31 +357,11 @@ const shouldRenderEstimationOverlay = computed(
363357
() => !pending.value && isEstimationGranularity.value,
364358
)
365359
366-
const startDateLocal = shallowRef<string>('')
367-
const endDateLocal = shallowRef<string>('')
368-
const startDateRoute = useRouteQuery<string>('start', '')
369-
const endDateRoute = useRouteQuery<string>('end', '')
370-
371-
const startDate = computed({
372-
get: () => (props.permalink ? startDateRoute.value : startDateLocal.value),
373-
set: (value: string) => {
374-
if (props.permalink) {
375-
startDateRoute.value = value
376-
} else {
377-
startDateLocal.value = value
378-
}
379-
},
360+
const startDate = usePermalinkValue<string>('start', '', {
361+
permanent: props.permalink,
380362
})
381-
382-
const endDate = computed({
383-
get: () => (props.permalink ? endDateRoute.value : endDateLocal.value),
384-
set: (value: string) => {
385-
if (props.permalink) {
386-
endDateRoute.value = value
387-
} else {
388-
endDateLocal.value = value
389-
}
390-
},
363+
const endDate = usePermalinkValue<string>('end', '', {
364+
permanent: props.permalink,
391365
})
392366
393367
const hasUserEditedDates = shallowRef(false)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Creates a computed property that uses route query parameters by default,
3+
* with an option to use local state instead.
4+
*/
5+
export function usePermalinkValue<T extends string | number = string>(
6+
queryKey: string,
7+
defaultValue: T = '' as T,
8+
options: { permanent?: boolean } = {},
9+
): WritableComputedRef<T> {
10+
const { permanent = true } = options
11+
const localValue = shallowRef<T>(defaultValue)
12+
const routeValue = useRouteQuery<T>(queryKey, defaultValue)
13+
14+
const parmalinkValue = computed({
15+
get: () => (permanent ? routeValue.value : localValue.value),
16+
set: (value: T) => {
17+
if (permanent) {
18+
routeValue.value = value
19+
} else {
20+
localValue.value = value
21+
}
22+
},
23+
})
24+
25+
return parmalinkValue
26+
}

0 commit comments

Comments
 (0)