Skip to content

Commit a1ffda0

Browse files
feat: use useRouteQuery
1 parent 687b62d commit a1ffda0

2 files changed

Lines changed: 73 additions & 19 deletions

File tree

app/components/Package/TrendsChart.vue

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ const props = defineProps<{
3636
3737
/** When true, shows facet selector (e.g. Downloads / Likes). */
3838
showFacetSelector?: boolean
39+
permalink?: boolean
3940
}>()
41+
const emit = defineEmits(['update:startDate', 'update:endDate'])
4042
4143
const { locale } = useI18n()
4244
const { accentColors, selectedAccentColor } = useAccentColor()
@@ -318,9 +320,20 @@ const effectivePackageNames = computed<string[]>(() => {
318320
return single ? [single] : []
319321
})
320322
321-
const selectedGranularity = defineModel<ChartTimeGranularity>('granularity', {
322-
default: DEFAULT_GRANULARITY,
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+
}
334+
},
323335
})
336+
324337
const displayedGranularity = shallowRef<ChartTimeGranularity>(DEFAULT_GRANULARITY)
325338
326339
const isEndDateOnPeriodEnd = computed(() => {
@@ -350,8 +363,33 @@ const shouldRenderEstimationOverlay = computed(
350363
() => !pending.value && isEstimationGranularity.value,
351364
)
352365
353-
const startDate = defineModel<string>('startDate', { default: '' })
354-
const endDate = defineModel<string>('endDate', { default: '' })
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+
},
380+
})
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+
},
391+
})
392+
355393
const hasUserEditedDates = shallowRef(false)
356394
357395
/**

app/components/Package/WeeklyDownloadStats.vue

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
<script setup lang="ts">
22
import { VueUiSparkline } from 'vue-data-ui/vue-ui-sparkline'
33
import { useCssVariables } from '~/composables/useColors'
4-
import type { ChartTimeGranularity, WeeklyDataPoint } from '~/types/chart'
4+
import type { WeeklyDataPoint } from '~/types/chart'
55
import { OKLCH_NEUTRAL_FALLBACK, lightenOklch } from '~/utils/colors'
66
77
const props = defineProps<{
88
packageName: string
99
createdIso: string | null
1010
}>()
1111
12+
const router = useRouter()
13+
const route = useRoute()
14+
1215
const chartModal = useModal('chart-modal')
1316
const hasChartModalTransitioned = shallowRef(false)
1417
15-
const modal = useRouteQuery<'downloads' | undefined>('modal')
16-
const granularity = useRouteQuery<ChartTimeGranularity | undefined>('granularity')
17-
const startDate = useRouteQuery<string | undefined>('start')
18-
const endDate = useRouteQuery<string | undefined>('end')
19-
20-
const isChartModalOpen = computed<boolean>(() => modal.value === 'downloads')
18+
const isChartModalOpen = shallowRef<boolean>(false)
2119
2220
function handleModalClose() {
23-
modal.value = undefined
24-
granularity.value = undefined
25-
startDate.value = undefined
26-
endDate.value = undefined
21+
isChartModalOpen.value = false
2722
hasChartModalTransitioned.value = false
23+
24+
router.replace({
25+
query: {
26+
...route.query,
27+
modal: undefined,
28+
granularity: undefined,
29+
end: undefined,
30+
start: undefined,
31+
},
32+
})
2833
}
2934
3035
function handleModalTransitioned() {
@@ -103,8 +108,16 @@ const hasWeeklyDownloads = computed(() => weeklyDownloads.value.length > 0)
103108
async function openChartModal() {
104109
if (!hasWeeklyDownloads.value) return
105110
106-
modal.value = 'downloads'
111+
isChartModalOpen.value = true
107112
hasChartModalTransitioned.value = false
113+
114+
await router.replace({
115+
query: {
116+
...route.query,
117+
modal: 'chart',
118+
},
119+
})
120+
108121
// ensure the component renders before opening the dialog
109122
await nextTick()
110123
await nextTick()
@@ -131,6 +144,11 @@ async function loadWeeklyDownloads() {
131144
132145
onMounted(async () => {
133146
await loadWeeklyDownloads()
147+
148+
if (route.query.modal === 'chart') {
149+
isChartModalOpen.value = true
150+
}
151+
134152
if (isChartModalOpen.value && hasWeeklyDownloads.value) {
135153
openChartModal()
136154
}
@@ -296,9 +314,7 @@ const config = computed(() => {
296314
:inModal="true"
297315
:packageName="props.packageName"
298316
:createdIso="createdIso"
299-
v-model:granularity="granularity"
300-
v-model:startDate="startDate"
301-
v-model:endDate="endDate"
317+
permalink
302318
show-facet-selector
303319
/>
304320
</Transition>

0 commit comments

Comments
 (0)