Skip to content

Commit f4943b9

Browse files
chore: add generic type annotations to chart components (#1615)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 6ce52be commit f4943b9

File tree

6 files changed

+47
-55
lines changed

6 files changed

+47
-55
lines changed

app/components/Package/TrendsChart.vue

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import type { VueUiXyDatasetItem } from 'vue-data-ui'
2+
import type { VueUiXyConfig, VueUiXyDatasetItem } from 'vue-data-ui'
33
import { VueUiXy } from 'vue-data-ui/vue-ui-xy'
44
import { useDebounceFn, useElementSize } from '@vueuse/core'
55
import { useCssVariables } from '~/composables/useColors'
@@ -1404,9 +1404,9 @@ function drawSvgPrintLegend(svg: Record<string, any>) {
14041404
}
14051405
14061406
// VueUiXy chart component configuration
1407-
const chartConfig = computed(() => {
1407+
const chartConfig = computed<VueUiXyConfig>(() => {
14081408
return {
1409-
theme: isDarkMode.value ? 'dark' : 'default',
1409+
theme: isDarkMode.value ? 'dark' : '',
14101410
chart: {
14111411
height: isMobile.value ? 950 : 600,
14121412
backgroundColor: colors.value.bg,
@@ -1428,10 +1428,13 @@ const chartConfig = computed(() => {
14281428
altCopy: undefined, // TODO: set to proper translation key
14291429
},
14301430
callbacks: {
1431-
img: ({ imageUri }: { imageUri: string }) => {
1431+
img: args => {
1432+
const imageUri = args?.imageUri
1433+
if (!imageUri) return
14321434
loadFile(imageUri, buildExportFilename('png'))
14331435
},
1434-
csv: (csvStr: string) => {
1436+
csv: csvStr => {
1437+
if (!csvStr) return
14351438
const PLACEHOLDER_CHAR = '\0'
14361439
const multilineDateTemplate = $t('package.trends.date_range_multiline', {
14371440
start: PLACEHOLDER_CHAR,
@@ -1448,7 +1451,9 @@ const chartConfig = computed(() => {
14481451
loadFile(url, buildExportFilename('csv'))
14491452
URL.revokeObjectURL(url)
14501453
},
1451-
svg: ({ blob }: { blob: Blob }) => {
1454+
svg: args => {
1455+
const blob = args?.blob
1456+
if (!blob) return
14521457
const url = URL.createObjectURL(blob)
14531458
loadFile(url, buildExportFilename('svg'))
14541459
URL.revokeObjectURL(url)
@@ -1510,10 +1515,9 @@ const chartConfig = computed(() => {
15101515
borderColor: 'transparent',
15111516
backdropFilter: false,
15121517
backgroundColor: 'transparent',
1513-
customFormat: ({ datapoint }: { datapoint: Record<string, any> | any[] }) => {
1514-
if (!datapoint || pending.value) return ''
1518+
customFormat: ({ datapoint: items }) => {
1519+
if (!items || pending.value) return ''
15151520
1516-
const items = Array.isArray(datapoint) ? datapoint : [datapoint[0]]
15171521
const hasMultipleItems = items.length > 1
15181522
15191523
const rows = items

app/components/Package/VersionDistribution.vue

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
<script setup lang="ts">
22
import { VueUiXy } from 'vue-data-ui/vue-ui-xy'
3-
import {
4-
type VueUiXyDatasetItem,
5-
type VueUiXyDatasetBarItem,
6-
type VueUiXyDatapointItem,
7-
type MinimalCustomFormatParams,
8-
} from 'vue-data-ui'
3+
import { type VueUiXyDatasetItem, type VueUiXyConfig } from 'vue-data-ui'
94
import { useElementSize } from '@vueuse/core'
105
import { useCssVariables } from '~/composables/useColors'
116
import { OKLCH_NEUTRAL_FALLBACK, transparentizeOklch, lightenHex } from '~/utils/colors'
@@ -15,10 +10,6 @@ import {
1510
} from '~/composables/useChartWatermark'
1611
import TooltipApp from '~/components/Tooltip/App.vue'
1712
18-
type TooltipParams = MinimalCustomFormatParams<VueUiXyDatapointItem[]> & {
19-
bars: VueUiXyDatasetBarItem[]
20-
}
21-
2213
const props = defineProps<{
2314
packageName: string
2415
inModal?: boolean
@@ -176,7 +167,7 @@ const hasMinimap = computed<boolean>(() => {
176167
return series.length > 6
177168
})
178169
179-
const chartConfig = computed(() => {
170+
const chartConfig = computed<VueUiXyConfig>(() => {
180171
return {
181172
theme: isDarkMode.value ? 'dark' : '',
182173
chart: {
@@ -209,10 +200,13 @@ const chartConfig = computed(() => {
209200
altCopy: undefined, // TODO: set to proper translation key
210201
},
211202
callbacks: {
212-
img: ({ imageUri }: { imageUri: string }) => {
203+
img: args => {
204+
const imageUri = args?.imageUri
205+
if (!imageUri) return
213206
loadFile(imageUri, buildExportFilename('png'))
214207
},
215-
csv: (csvStr: string) => {
208+
csv: csvStr => {
209+
if (!csvStr) return
216210
const PLACEHOLDER_CHAR = '\0'
217211
const multilineDateTemplate = $t('package.trends.date_range_multiline', {
218212
start: PLACEHOLDER_CHAR,
@@ -229,7 +223,9 @@ const chartConfig = computed(() => {
229223
loadFile(url, buildExportFilename('csv'))
230224
URL.revokeObjectURL(url)
231225
},
232-
svg: ({ blob }: { blob: Blob }) => {
226+
svg: args => {
227+
const blob = args?.blob
228+
if (!blob) return
233229
const url = URL.createObjectURL(blob)
234230
loadFile(url, buildExportFilename('svg'))
235231
URL.revokeObjectURL(url)
@@ -277,8 +273,7 @@ const chartConfig = computed(() => {
277273
borderColor: 'transparent',
278274
backdropFilter: false,
279275
backgroundColor: 'transparent',
280-
customFormat: (params: TooltipParams) => {
281-
const { datapoint, absoluteIndex, bars } = params
276+
customFormat: ({ datapoint, absoluteIndex, bars }) => {
282277
if (!datapoint || pending.value) return ''
283278
284279
// Use absoluteIndex to get the correct version from chartDataset
@@ -329,9 +324,6 @@ const chartConfig = computed(() => {
329324
},
330325
},
331326
},
332-
table: {
333-
show: false,
334-
},
335327
}
336328
})
337329
</script>
@@ -447,7 +439,7 @@ const chartConfig = computed(() => {
447439
role="region"
448440
aria-labelledby="version-distribution-title"
449441
class="relative"
450-
:class="isMobile ? 'min-h-[260px]' : 'min-h-[400px]'"
442+
:class="isMobile ? 'min-h-[260px]' : 'min-h-[520px]'"
451443
>
452444
<!-- Chart content -->
453445
<ClientOnly v-if="xyDataset.length > 0 && !error">
@@ -463,6 +455,16 @@ const chartConfig = computed(() => {
463455
v-if="svg.isPrintingSvg || svg.isPrintingImg"
464456
v-html="drawNpmxLogoAndTaglineWatermark(svg, watermarkColors, $t, 'bottom')"
465457
/>
458+
459+
<!-- Overlay covering the chart area to hide line resizing when switching granularities recalculates VueUiXy scaleMax when estimation lines are necessary -->
460+
<rect
461+
v-if="pending"
462+
:x="svg.drawingArea.left"
463+
:y="svg.drawingArea.top - 12"
464+
:width="svg.drawingArea.width + 12"
465+
:height="svg.drawingArea.height + 48"
466+
:fill="colors.bg"
467+
/>
466468
</template>
467469

468470
<!-- Custom bar gradient based on the series color -->
@@ -620,21 +622,6 @@ const chartConfig = computed(() => {
620622
:deep(.vue-ui-xy) svg rect {
621623
transition: none !important;
622624
}
623-
624-
@keyframes fadeInUp {
625-
from {
626-
opacity: 0;
627-
transform: translateY(8px);
628-
}
629-
to {
630-
opacity: 1;
631-
transform: translateY(0);
632-
}
633-
}
634-
635-
.chart-container {
636-
animation: fadeInUp 350ms cubic-bezier(0.4, 0, 0.2, 1);
637-
}
638625
</style>
639626

640627
<style>

app/components/Package/Versions.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ function majorGroupContainsCurrent(group: (typeof otherMajorGroups.value)[0]): b
10771077
<!-- Avoids CLS when the dialog has transitioned -->
10781078
<div
10791079
v-if="!hasDistributionModalTransitioned"
1080-
class="w-full aspect-[272/609] sm:aspect-[718/571]"
1080+
class="w-full aspect-[272/609] sm:aspect-[718/592.67]"
10811081
/>
10821082
</PackageChartModal>
10831083
</template>

app/components/Package/WeeklyDownloadStats.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useCssVariables } from '~/composables/useColors'
44
import type { WeeklyDataPoint } from '~/types/chart'
55
import { OKLCH_NEUTRAL_FALLBACK, lightenOklch } from '~/utils/colors'
66
import type { RepoRef } from '#shared/utils/git-providers'
7+
import type { VueUiSparklineConfig, VueUiSparklineDatasetItem } from 'vue-data-ui'
78
89
const props = defineProps<{
910
packageName: string
@@ -176,7 +177,7 @@ watch(
176177
() => loadWeeklyDownloads(),
177178
)
178179
179-
const dataset = computed(() =>
180+
const dataset = computed<VueUiSparklineDatasetItem[]>(() =>
180181
weeklyDownloads.value.map(d => ({
181182
value: d?.value ?? 0,
182183
period: $t('package.trends.date_range', {
@@ -188,7 +189,7 @@ const dataset = computed(() =>
188189
189190
const lastDatapoint = computed(() => dataset.value.at(-1)?.period ?? '')
190191
191-
const config = computed(() => {
192+
const config = computed<VueUiSparklineConfig>(() => {
192193
return {
193194
theme: 'dark',
194195
/**
@@ -234,7 +235,7 @@ const config = computed(() => {
234235
show: hasSparklineAnimation.value, // the pulse will not show if prefers-reduced-motion (enforced by vue-data-ui)
235236
loop: true, // runs only once if false
236237
radius: 1.5,
237-
color: pulseColor.value,
238+
color: pulseColor.value!,
238239
easing: 'ease-in-out',
239240
trail: {
240241
show: true,
@@ -248,7 +249,7 @@ const config = computed(() => {
248249
stroke: isDarkMode.value ? 'oklch(0.985 0 0)' : 'oklch(0.145 0 0)',
249250
},
250251
title: {
251-
text: lastDatapoint.value,
252+
text: String(lastDatapoint.value),
252253
fontSize: 12,
253254
color: colors.value.fgSubtle,
254255
bold: false,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
"vite-plugin-pwa": "1.2.0",
111111
"vite-plus": "0.0.0-833c515fa25cef20905a7f9affb156dfa6f151ab",
112112
"vue": "3.5.28",
113-
"vue-data-ui": "3.15.5"
113+
"vue-data-ui": "3.15.6"
114114
},
115115
"devDependencies": {
116116
"@e18e/eslint-plugin": "0.2.0",

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)