Skip to content

Commit 118ff4b

Browse files
committed
fix: ensure graph uses up-to-date data
1 parent 652a076 commit 118ff4b

2 files changed

Lines changed: 35 additions & 33 deletions

File tree

app/components/PackageDownloadAnalytics.vue

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ import { useDebounceFn, useElementSize } from '@vueuse/core'
55
import { useCssVariables } from '../composables/useColors'
66
import { OKLCH_NEUTRAL_FALLBACK, transparentizeOklch } from '../utils/colors'
77
8-
const {
9-
weeklyDownloads,
10-
inModal = false,
11-
packageName,
12-
createdIso,
13-
} = defineProps<{
8+
const props = defineProps<{
149
weeklyDownloads: WeeklyDownloadPoint[]
1510
inModal?: boolean
1611
packageName: string
@@ -131,7 +126,7 @@ function formatXyDataset(
131126
return {
132127
dataset: [
133128
{
134-
name: packageName,
129+
name: props.packageName,
135130
type: 'line',
136131
series: dataset.map(d => d.downloads),
137132
color: accent.value,
@@ -149,7 +144,7 @@ function formatXyDataset(
149144
return {
150145
dataset: [
151146
{
152-
name: packageName,
147+
name: props.packageName,
153148
type: 'line',
154149
series: dataset.map(d => d.downloads),
155150
color: accent.value,
@@ -162,7 +157,7 @@ function formatXyDataset(
162157
return {
163158
dataset: [
164159
{
165-
name: packageName,
160+
name: props.packageName,
166161
type: 'line',
167162
series: dataset.map(d => d.downloads),
168163
color: accent.value,
@@ -175,7 +170,7 @@ function formatXyDataset(
175170
return {
176171
dataset: [
177172
{
178-
name: packageName,
173+
name: props.packageName,
179174
type: 'line',
180175
series: dataset.map(d => d.downloads),
181176
color: accent.value,
@@ -235,10 +230,10 @@ const hasUserEditedDates = shallowRef(false)
235230
236231
function initDateRangeFromWeekly() {
237232
if (hasUserEditedDates.value) return
238-
if (!weeklyDownloads?.length) return
233+
if (!props.weeklyDownloads?.length) return
239234
240-
const first = weeklyDownloads[0]
241-
const last = weeklyDownloads[weeklyDownloads.length - 1]
235+
const first = props.weeklyDownloads[0]
236+
const last = props.weeklyDownloads[props.weeklyDownloads.length - 1]
242237
const start = first?.weekStart ? toIsoDateOnly(first.weekStart) : ''
243238
const end = last?.weekEnd ? toIsoDateOnly(last.weekEnd) : ''
244239
if (isValidIsoDateOnly(start)) startDate.value = start
@@ -265,7 +260,7 @@ function initDateRangeFallbackClient() {
265260
}
266261
267262
watch(
268-
() => weeklyDownloads?.length,
263+
() => props.weeklyDownloads?.length,
269264
() => {
270265
initDateRangeFromWeekly()
271266
initDateRangeFallbackClient()
@@ -342,7 +337,7 @@ watch(
342337
343338
const { fetchPackageDownloadEvolution } = useCharts()
344339
345-
const evolution = shallowRef<EvolutionData>(weeklyDownloads)
340+
const evolution = shallowRef<EvolutionData>(props.weeklyDownloads)
346341
const pending = shallowRef(false)
347342
348343
let lastRequestKey = ''
@@ -354,7 +349,7 @@ const debouncedLoad = useDebounceFn(() => {
354349
355350
async function load() {
356351
if (!import.meta.client) return
357-
if (!inModal) return
352+
if (!props.inModal) return
358353
359354
const o = options.value
360355
const extraBase =
@@ -366,14 +361,14 @@ async function load() {
366361
367362
const startKey = (o as any).startDate ?? ''
368363
const endKey = (o as any).endDate ?? ''
369-
const requestKey = `${packageName}|${createdIso ?? ''}|${o.granularity}|${extraBase}|${startKey}|${endKey}`
364+
const requestKey = `${props.packageName}|${props.createdIso ?? ''}|${o.granularity}|${extraBase}|${startKey}|${endKey}`
370365
371366
if (requestKey === lastRequestKey) return
372367
lastRequestKey = requestKey
373368
374369
const hasExplicitRange = Boolean((o as any).startDate || (o as any).endDate)
375-
if (o.granularity === 'week' && weeklyDownloads?.length && !hasExplicitRange) {
376-
evolution.value = weeklyDownloads
370+
if (o.granularity === 'week' && props.weeklyDownloads?.length && !hasExplicitRange) {
371+
evolution.value = props.weeklyDownloads
377372
pending.value = false
378373
displayedGranularity.value = 'weekly'
379374
return
@@ -384,8 +379,8 @@ async function load() {
384379
385380
try {
386381
const result = await fetchPackageDownloadEvolution(
387-
() => packageName,
388-
() => createdIso,
382+
() => props.packageName,
383+
() => props.createdIso,
389384
() => o as any, // FIXME: any
390385
)
391386
@@ -404,7 +399,7 @@ async function load() {
404399
}
405400
406401
watch(
407-
() => inModal,
402+
() => props.inModal,
408403
() => {
409404
// modal open/close should be immediate
410405
load()
@@ -414,8 +409,8 @@ watch(
414409
415410
watch(
416411
() => [
417-
packageName,
418-
createdIso,
412+
props.packageName,
413+
props.createdIso,
419414
options.value.granularity,
420415
(options.value as any).weeks,
421416
(options.value as any).months,
@@ -437,9 +432,9 @@ watch(
437432
)
438433
439434
const effectiveData = computed<EvolutionData>(() => {
440-
if (displayedGranularity.value === 'weekly' && weeklyDownloads?.length) {
435+
if (displayedGranularity.value === 'weekly' && props.weeklyDownloads?.length) {
441436
if (isWeeklyDataset(evolution.value) && evolution.value.length) return evolution.value
442-
return weeklyDownloads
437+
return props.weeklyDownloads
443438
}
444439
return evolution.value
445440
})
@@ -481,7 +476,7 @@ const config = computed(() => {
481476
img: ({ imageUri }: { imageUri: string }) => {
482477
loadFile(
483478
imageUri,
484-
`${packageName}-${selectedGranularity.value}_${startDate.value}_${endDate.value}.png`,
479+
`${props.packageName}-${selectedGranularity.value}_${startDate.value}_${endDate.value}.png`,
485480
)
486481
},
487482
csv: (csvStr: string) => {
@@ -502,15 +497,15 @@ const config = computed(() => {
502497
const url = URL.createObjectURL(blob)
503498
loadFile(
504499
url,
505-
`${packageName}-${selectedGranularity.value}_${startDate.value}_${endDate.value}.csv`,
500+
`${props.packageName}-${selectedGranularity.value}_${startDate.value}_${endDate.value}.csv`,
506501
)
507502
URL.revokeObjectURL(url)
508503
},
509504
svg: ({ blob }: { blob: Blob }) => {
510505
const url = URL.createObjectURL(blob)
511506
loadFile(
512507
url,
513-
`${packageName}-${selectedGranularity.value}_${startDate.value}_${endDate.value}.svg`,
508+
`${props.packageName}-${selectedGranularity.value}_${startDate.value}_${endDate.value}.svg`,
514509
)
515510
URL.revokeObjectURL(url)
516511
},
@@ -525,7 +520,7 @@ const config = computed(() => {
525520
yLabel: $t('package.downloads.y_axis_label', {
526521
granularity: $t(`package.downloads.granularity_${selectedGranularity.value}`),
527522
}),
528-
xLabel: packageName,
523+
xLabel: props.packageName,
529524
yLabelOffsetX: 12,
530525
fontSize: isMobile.value ? 32 : 24,
531526
},

app/components/PackageWeeklyDownloadStats.vue

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ const props = defineProps<{
99
1010
const chartModal = useModal('chart-modal')
1111
12+
const isChartModalOpen = shallowRef(false)
13+
function openChartModal() {
14+
isChartModalOpen.value = true
15+
// ensure the component renders before opening the dialog
16+
nextTick(() => chartModal.open())
17+
}
18+
1219
const { data: packument } = usePackage(() => props.packageName)
1320
const createdIso = computed(() => packument.value?.time?.created ?? null)
1421
@@ -194,7 +201,7 @@ const config = computed(() => {
194201
<template #actions>
195202
<button
196203
type="button"
197-
@click="chartModal.open()"
204+
@click="openChartModal"
198205
class="link-subtle font-mono text-sm inline-flex items-center gap-1.5 ms-auto shrink-0 self-center focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 rounded"
199206
:title="$t('package.downloads.analyze')"
200207
>
@@ -241,11 +248,11 @@ const config = computed(() => {
241248
</CollapsibleSection>
242249
</div>
243250

244-
<ChartModal>
251+
<ChartModal v-if="isChartModalOpen" @close="isChartModalOpen = false">
245252
<PackageDownloadAnalytics
246253
:weeklyDownloads="weeklyDownloads"
247254
:inModal="true"
248-
:packageName="packageName"
255+
:packageName="props.packageName"
249256
:createdIso="createdIso"
250257
/>
251258
</ChartModal>

0 commit comments

Comments
 (0)