summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/analytics/shared/components/metric_tile.vue
blob: 845a3386f6c2d4419c78f0fd9e5dda7fafd244e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<script>
import { GlSingleStat } from '@gitlab/ui/dist/charts';
import { redirectTo } from '~/lib/utils/url_utility';
import MetricPopover from './metric_popover.vue';

export default {
  name: 'MetricTile',
  components: {
    GlSingleStat,
    MetricPopover,
  },
  props: {
    metric: {
      type: Object,
      required: true,
    },
  },
  computed: {
    decimalPlaces() {
      const parsedFloat = parseFloat(this.metric.value);
      return Number.isNaN(parsedFloat) || Number.isInteger(parsedFloat) ? 0 : 1;
    },
    hasLinks() {
      return this.metric.links?.length && this.metric.links[0].url;
    },
  },
  methods: {
    clickHandler({ links }) {
      if (this.hasLinks) {
        redirectTo(links[0].url);
      }
    },
  },
};
</script>
<template>
  <div v-bind="$attrs">
    <gl-single-stat
      :id="metric.identifier"
      :value="`${metric.value}`"
      :title="metric.label"
      :unit="metric.unit || ''"
      :should-animate="true"
      :animation-decimal-places="decimalPlaces"
      :class="{ 'gl-hover-cursor-pointer': hasLinks }"
      tabindex="0"
      @click="clickHandler(metric)"
    />
    <metric-popover :metric="metric" :target="metric.identifier" />
  </div>
</template>