Skip to content

AI-native design editor. Open-source Figma alternative.

4.2 KB
Raw Blame
<script setup lang="ts">
import { RadioGroupItem, RadioGroupRoot } from 'reka-ui'
import { tv } from 'tailwind-variants'
import { computed } from 'vue'

import { useCloudMessages, useCommonMessages } from '@open-pencil/vue/i18n'

import AppButton from '@/components/ui/button/AppButton.vue'
import AppDialogBody from '@/components/ui/dialog/AppDialogBody.vue'
import AppDialogFooter from '@/components/ui/dialog/AppDialogFooter.vue'
import AppDialogHeader from '@/components/ui/dialog/AppDialogHeader.vue'
import AppDialogRoot from '@/components/ui/dialog/AppDialogRoot.vue'
import cloudConflictTheme from '@/theme/cloud/conflict'

import type { ConflictChoice, ConflictVersion } from './types'

const cloudConflict = tv(cloudConflictTheme)

/**
 * A Cloud document saved elsewhere while this device held unsent changes. Both versions are kept
 * until the person chooses; the safe choice keeps both, the others say whose work they discard.
 */
const { documentName, mine, cloud } = defineProps<{
  documentName: string
  mine: ConflictVersion
  cloud: ConflictVersion
}>()

const open = defineModel<boolean>('open', { default: false })
const choice = defineModel<ConflictChoice>('choice', { default: 'keep-both' })
const emit = defineEmits<{ confirm: [choice: ConflictChoice]; later: [] }>()

const ui = cloudConflict()
const t = useCloudMessages()
const common = useCommonMessages()
const options = computed(() => [
  {
    value: 'keep-both' as const,
    label: t.value.conflictKeepBoth,
    description: t.value.conflictKeepBothDescription({
      name: documentName,
      copyName: t.value.conflictYourCopyName({ name: documentName })
    })
  },
  {
    value: 'use-cloud' as const,
    label: t.value.conflictUseCloud,
    description: t.value.conflictUseCloudDescription
  },
  {
    value: 'use-mine' as const,
    label: t.value.conflictUseMine,
    description: cloud.by
      ? t.value.conflictUseMineDescriptionBy({ name: cloud.by })
      : t.value.conflictUseMineDescription
  }
])
const confirmLabel = computed(
  () =>
    ({
      'keep-both': t.value.conflictKeepBoth,
      'use-cloud': t.value.conflictConfirmUseCloud,
      'use-mine': t.value.conflictConfirmUseMine
    })[choice.value]
)
</script>

<template>
  <AppDialogRoot v-model:open="open" size="md">
    <AppDialogHeader
      :heading="t.changedInTwoPlaces({ name: documentName })"
      :description="t.conflictDescription"
      :close-label="common.close"
    />
    <AppDialogBody>
      <div :class="ui.versions()">
        <figure
          v-for="version in [
            { ...mine, label: t.conflictYourVersion },
            { ...cloud, label: t.conflictCloudVersion }
          ]"
          :key="version.label"
          :class="ui.version()"
        >
          <span :class="ui.preview()">
            <img v-if="version.previewURL" :src="version.previewURL" alt="" :class="ui.image()" />
            <icon-lucide-file-image v-else class="size-6 text-muted/40" />
          </span>
          <figcaption :class="ui.caption()">
            <span :class="ui.versionLabel()">{{ version.label }}</span>
            <span :class="ui.versionMeta()">
              {{ version.by ? `${version.by} · ${version.savedAgo}` : version.savedAgo }}
            </span>
          </figcaption>
        </figure>
      </div>
      <RadioGroupRoot v-model="choice" :aria-label="t.conflictWhichVersion" :class="ui.options()">
        <RadioGroupItem
          v-for="option in options"
          :key="option.value"
          :value="option.value"
          :class="ui.option()"
          :data-tone="option.value === 'use-mine' ? 'danger' : undefined"
        >
          <span :class="ui.optionMark()" aria-hidden="true" />
          <span class="min-w-0">
            <span :class="ui.optionLabel()">{{ option.label }}</span>
            <span :class="ui.optionDescription()">{{ option.description }}</span>
          </span>
        </RadioGroupItem>
      </RadioGroupRoot>
    </AppDialogBody>
    <AppDialogFooter>
      <AppButton variant="ghost" @click="emit('later')">{{ t.conflictDecideLater }}</AppButton>
      <AppButton
        :color="choice === 'use-mine' ? 'error' : 'primary'"
        variant="solid"
        @click="emit('confirm', choice)"
      >
        {{ confirmLabel }}
      </AppButton>
    </AppDialogFooter>
  </AppDialogRoot>
</template>