All files / src/components SpaceDataValue.vue

100% Statements 26/26
97.77% Branches 44/45
100% Functions 11/11
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188    67x                                         5x                   4x                                     2x                                 67x                       67x   67x 42x         67x 41x     67x 2x     67x 5x       2x 2x   1x           12x 12x 11x 11x 11x   1x                                                                                                                                            
<template>
  <span
    v-if="field.type === 'boolean'"
    class="space-data-badge"
    :class="value ? 'space-data-badge-yes' : 'space-data-badge-no'"
    >{{ value ? t('common.yes') : t('common.no') }}</span
  >
  <span v-else-if="field.type === 'workflow'" class="space-data-badge space-data-badge-state">{{
    shown
  }}</span>
  <span v-else-if="field.type === 'number'">{{ formatNumber(value) }}</span>
  <span v-else-if="field.type === 'date'">{{ formatDate(value) }}</span>
  <a v-else-if="field.type === 'email' && typeof shown === 'string'" :href="`mailto:${shown}`">{{
    shown
  }}</a>
  <img
    v-else-if="field.type === 'image'"
    :src="value"
    alt=""
    class="space-data-image"
    loading="lazy"
  />
  <span v-else-if="field.type === 'image_set'" class="space-data-images">
    <img
      v-for="(url, index) in visibleImages"
      :key="index"
      :src="url"
      alt=""
      class="space-data-image"
      loading="lazy"
    />
  </span>
  <span v-else-if="field.type === 'file' || field.type === 'file_set'" class="space-data-files">
    <a
      v-for="(file, index) in files"
      :key="index"
      :href="file.url"
      target="_blank"
      rel="noopener noreferrer"
    >
      <font-awesome-icon icon="fa-solid fa-paperclip" class="me-1" />{{ file.name || 'file' }}
    </a>
  </span>
  <a
    v-else-if="field.type === 'geo_location'"
    :href="`https://www.openstreetmap.org/?mlat=${value.lat}&mlon=${value.lng}#map=16/${value.lat}/${value.lng}`"
    target="_blank"
    rel="noopener noreferrer"
  >
    <font-awesome-icon icon="fa-solid fa-location-dot" class="me-1" />{{ t('spacePages.onMap') }}
  </a>
  <span v-else-if="Array.isArray(shown)" class="space-data-chips">
    <span v-for="(entry, index) in shown" :key="index" class="space-data-chip">{{ entry }}</span>
  </span>
  <span v-else :class="{ 'space-data-long': field.type === 'textarea' }">{{ shown }}</span>
</template>
 
<script setup lang="ts">
// @implements UC-SPACE-011.1
/**
 * One value of an item shown by a data block, as the API prepared it
 * (flows-api utils/spaceData.js displayValue): texts and labels may be
 * `{ lang: text }` maps, images and files are signed URLs.
 */
import { computed } from 'vue'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { useUILanguage } from '@/composables/useUILanguage'
import { localize, type DataField } from '@/utils/spacePages'
 
const props = withDefaults(
  defineProps<{
    field: DataField
    value: any
    lang: string
    fallbackLang?: string
    /** At most this many images of an image set (the details show them all) */
    maxImages?: number
  }>(),
  { fallbackLang: undefined, maxImages: undefined }
)
 
const { t } = useUILanguage()
 
const text = (value: unknown) =>
  value && typeof value === 'object' && !Array.isArray(value)
    ? localize(value as Record<string, string>, props.lang, props.fallbackLang)
    : value
 
/** Texts, labels and references, in the reader's language */
const shown = computed(() =>
  Array.isArray(props.value) ? props.value.map((entry) => text(entry)) : text(props.value)
)
 
const visibleImages = computed(() =>
  Array.isArray(props.value) ? props.value.slice(0, props.maxImages ?? props.value.length) : []
)
 
const files = computed(() =>
  (Array.isArray(props.value) ? props.value : [props.value]).filter((file) => file?.url)
)
 
function formatNumber(value: number) {
  try {
    return new Intl.NumberFormat(props.lang).format(value)
  } catch {
    return String(value)
  }
}
 
/** A `YYYY-MM-DD` date, in the reader's language, never shifted by their time zone */
function formatDate(value: string) {
  const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value)
  if (!match) return value
  const date = new Date(Date.UTC(Number(match[1]), Number(match[2]) - 1, Number(match[3])))
  try {
    return new Intl.DateTimeFormat(props.lang, { dateStyle: 'long', timeZone: 'UTC' }).format(date)
  } catch {
    return value
  }
}
</script>
 
<style scoped>
.space-data-image {
  max-width: 100%;
  border-radius: 6px;
  object-fit: cover;
}
 
.space-data-images {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}
 
.space-data-images .space-data-image {
  width: 96px;
  height: 96px;
}
 
.space-data-files {
  display: flex;
  flex-direction: column;
}
 
.space-data-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}
 
.space-data-chip {
  border: 1px solid currentColor;
  border-radius: 999px;
  padding: 0 0.5rem;
  font-size: 0.875rem;
}
 
.space-data-long {
  white-space: pre-line;
}
 
/* Booleans and workflow states as badges, like the app's item cards */
.space-data-badge {
  display: inline-block;
  padding: 0.25em 0.6em;
  font-size: 0.75em;
  font-weight: 600;
  line-height: 1;
  border-radius: 0.375rem;
}
 
.space-data-badge-yes {
  background: #198754;
  color: #fff;
}
 
.space-data-badge-no {
  background: #6c757d;
  color: #fff;
}
 
.space-data-badge-state {
  background: var(--space-primary);
  color: var(--space-on-primary);
}
</style>