Skip to content
Merged
93 changes: 93 additions & 0 deletions components/PhotoDeleteDialog.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<script setup lang="ts">
// Asks before a photo is deleted. It opens as soon as it appears in the page.
import type { Photo, PhotoOutcome } from '../composables/usePhotos'

const props = defineProps<{
photo: Photo
// The upload time as text.
date: string
// True while the request is on the way.
deleting: boolean
remove: (photo: Photo) => Promise<PhotoOutcome>
}>()

// Closed without deleting. After a deletion the whole tile disappears instead.
const emit = defineEmits<{ close: [] }>()

const { t } = useI18n()

const id = useId()
const dialog = useTemplateRef<HTMLDialogElement>('dialog')
const failure = ref<string>()

// A modal dialog keeps the focus inside and closes with Escape.
onMounted(() => dialog.value?.showModal())

function close() {
dialog.value?.close()
}

async function confirm() {
if (props.deleting) {
return
}
failure.value = undefined
const outcome = await props.remove(props.photo)
failure.value = outcome.messageKey
}
</script>

<template>
<!--
"m-auto" centres the dialog again, because Tailwind removes the default margin.
The dialog itself is invisible; its padding keeps the box away from the screen edges.
-->
<dialog
ref="dialog"
:aria-labelledby="`${id}-title`"
:aria-describedby="`${id}-text`"
class="m-auto bg-transparent p-4"
@close="emit('close')"
>
<div class="max-w-form rounded-lg border border-field bg-surface p-4 text-ink">
<h2
:id="`${id}-title`"
class="text-xl"
>
{{ t('photos.delete.title') }}
</h2>
<p
:id="`${id}-text`"
class="mt-3"
>
{{ t('photos.delete.text', { date: props.date }) }}
</p>
<p
v-if="failure"
role="alert"
class="mt-3 rounded-md border-2 border-danger bg-surface p-3 text-danger"
>
{{ t(failure) }}
</p>
<div class="mt-4 flex flex-wrap justify-end gap-2">
<!-- First and focused: the harmless choice. -->
<button
type="button"
autofocus
class="min-h-touch rounded-md border border-field bg-surface px-4 font-strong"
@click="close"
>
{{ t('photos.delete.cancel') }}
</button>
<button
type="button"
:aria-disabled="props.deleting"
class="min-h-touch rounded-md border-2 border-danger bg-surface px-4 font-strong text-danger aria-disabled:cursor-wait aria-disabled:faded"
@click="confirm"
>
{{ t('photos.delete.confirm') }}
</button>
</div>
</div>
</dialog>
</template>
103 changes: 103 additions & 0 deletions components/PhotoTile.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<script setup lang="ts">
// One photo in the gallery, with its delete button.
import type { Photo, PhotoOutcome } from '../composables/usePhotos'

const props = defineProps<{
photo: Photo
// True if the image could not be loaded, also with a new address.
broken: boolean
// True while it is being deleted.
deleting: boolean
remove: (photo: Photo) => Promise<PhotoOutcome>
}>()

const emit = defineEmits<{
loaded: [name: string]
failed: [name: string]
reload: [name: string]
}>()

const { t, localeProperties } = useI18n()

const deleteButton = useTemplateRef<HTMLButtonElement>('deleteButton')
const asking = ref(false)

// There is no caption in the API, so the upload time describes the photo.
const uploadTime = computed(() => formatUploadTime(props.photo.createdAt, localeProperties.value.language!))
const description = computed(() => t('photos.alt', { date: uploadTime.value }))

function askToDelete() {
if (!props.deleting) {
asking.value = true
}
}

// The question was closed without deleting.
async function closeQuestion() {
asking.value = false
await nextTick()
deleteButton.value?.focus()
}
</script>

<template>
<figure class="flex h-full flex-col gap-2">
<!--
The tile shows a square cut-out; the link opens the whole photo. "noopener noreferrer":
the new tab cannot reach this page and does not learn where the visit comes from.
-->
<a
v-if="props.photo.url && !props.broken"
:href="props.photo.url"
target="_blank"
rel="noopener noreferrer"
:aria-label="t('photos.openFull', { date: uploadTime })"
class="block rounded-md"
>
<img
:src="props.photo.url"
:alt="description"
loading="lazy"
decoding="async"
class="aspect-square w-full rounded-md bg-surface object-cover"
@load="emit('loaded', props.photo.name)"
@error="emit('failed', props.photo.name)"
>
</a>
<div
v-else
class="flex aspect-square w-full flex-col items-center justify-center gap-2 rounded-md border border-field bg-surface p-2 text-center"
>
<p class="text-sm">{{ t('photos.unavailable') }}</p>
<p class="sr-only">{{ description }}</p>
<button
type="button"
class="min-h-touch rounded-md border border-field bg-surface px-3 text-sm font-strong"
@click="emit('reload', props.photo.name)"
>
{{ t('photos.reload') }}
</button>
</div>

<!-- The label starts with the visible word and adds which photo is meant. -->
<button
ref="deleteButton"
type="button"
:aria-label="t('photos.delete.openNamed', { date: uploadTime })"
:aria-disabled="props.deleting"
class="min-h-touch rounded-md border-2 border-danger bg-surface px-3 font-strong text-danger aria-disabled:cursor-wait aria-disabled:faded"
@click="askToDelete"
>
{{ t('photos.delete.open') }}
</button>

<PhotoDeleteDialog
v-if="asking"
:photo="props.photo"
:date="uploadTime"
:deleting="props.deleting"
:remove="props.remove"
@close="closeQuestion"
/>
</figure>
</template>
178 changes: 178 additions & 0 deletions components/PhotoUploader.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<script setup lang="ts">
// Choosing a photo, and following its upload until it is in the gallery.
import type { UploadStep } from '../stores/photoUpload'

const props = defineProps<{
// How many photos have been deleted while this page is open.
deletions: number
}>()

const { t } = useI18n()
const { step, pending, failure, selectionError, completed, leftover, select, retry, discard } = usePhotoUpload()

// The order of the steps, for "step 2 of 4".
const steps: UploadStep[] = ['preparing', 'reserving', 'uploading', 'confirming']
const accepted = 'image/jpeg,image/png,image/webp'

const id = useId()
const heading = useTemplateRef<HTMLHeadingElement>('heading')
const chooseButton = useTemplateRef<HTMLButtonElement>('chooseButton')
const fileInput = useTemplateRef<HTMLInputElement>('fileInput')
const cameraInput = useTemplateRef<HTMLInputElement>('cameraInput')
const added = ref(false)

// While a step runs or an upload is unfinished, no other photo can be chosen.
const blocked = computed(() => Boolean(step.value) || pending.value)

watch(completed, () => {
added.value = true
})

// After a deletion, "photo added" would sound as if that photo were still there.
watch(() => props.deletions, () => {
added.value = false
})

function open(input: HTMLInputElement | null) {
if (!blocked.value) {
input?.click()
}
}

function chosen(event: Event) {
const input = event.target as HTMLInputElement
const file = input.files?.[0]
// Emptied, so that choosing the same file again counts as a new choice.
input.value = ''
if (file) {
added.value = false
select(file)
}
}

// The pressed button disappears, so the focus moves first.
function tryAgain() {
heading.value?.focus()
retry()
}

async function throwAway() {
discard()
await nextTick()
chooseButton.value?.focus()
}
</script>

<template>
<section
:aria-labelledby="`${id}-title`"
class="flex flex-col gap-3 rounded-lg border border-line bg-surface p-4"
>
<h2
:id="`${id}-title`"
ref="heading"
tabindex="-1"
class="text-xl"
>
{{ t('photos.upload.title') }}
</h2>
<p class="text-sm text-muted">{{ t('photos.upload.hint') }}</p>

<p
v-if="selectionError"
role="alert"
class="rounded-md border-2 border-danger bg-surface p-4 text-danger"
>
{{ t(selectionError) }}
</p>

<!-- aria-disabled, because a disabled button would lose the keyboard focus. -->
<div class="flex flex-wrap gap-2">
<button
ref="chooseButton"
type="button"
:aria-disabled="blocked"
class="min-h-touch grow rounded-md bg-accent px-4 font-strong text-on-accent aria-disabled:cursor-wait aria-disabled:faded md:grow-0"
@click="open(fileInput)"
>
{{ t('photos.upload.choose') }}
</button>
<!-- Only on touch devices. Elsewhere "capture" just opens the same file dialog. -->
<button
type="button"
:aria-disabled="blocked"
class="hidden min-h-touch grow items-center justify-center rounded-md border border-field bg-surface px-4 font-strong aria-disabled:cursor-wait aria-disabled:faded pointer-coarse:inline-flex"
@click="open(cameraInput)"
>
{{ t('photos.upload.camera') }}
</button>
</div>
<!-- The inputs do the work; the buttons above are what people see and focus. -->
<input
ref="fileInput"
type="file"
:accept="accepted"
tabindex="-1"
aria-hidden="true"
class="sr-only"
@change="chosen"
>
<input
ref="cameraInput"
type="file"
:accept="accepted"
capture="environment"
tabindex="-1"
aria-hidden="true"
class="sr-only"
@change="chosen"
>

<!-- Stays in the page, so that screen readers announce every step. -->
<div
role="status"
class="flex flex-col gap-2 empty:sr-only"
>
<template v-if="step">
<p>{{ t(`photos.upload.step.${step}`, { number: steps.indexOf(step) + 1, total: steps.length }) }}</p>
<progress
:aria-label="t('photos.upload.progress')"
class="w-full accent-accent"
/>
<p class="text-sm text-muted">{{ t('photos.upload.keepOpen') }}</p>
</template>
<p v-else-if="added">{{ t('photos.upload.done') }}</p>
</div>

<div
v-if="failure && !step"
class="flex flex-col gap-3 rounded-md border-2 border-danger bg-surface p-4"
>
<div
role="alert"
class="text-danger"
>
<p class="font-strong">{{ t('photos.upload.failed') }}</p>
<p>{{ t(failure.messageKey) }}</p>
</div>
<!-- What stays behind if the upload is thrown away. -->
<p v-if="leftover !== 'nothing'">{{ t(`photos.upload.leftover.${leftover}`) }}</p>
<div class="flex flex-wrap gap-2">
<button
type="button"
class="min-h-touch grow rounded-md bg-accent px-4 font-strong text-on-accent md:grow-0"
@click="tryAgain"
>
{{ t('photos.upload.retry') }}
</button>
<button
type="button"
class="min-h-touch grow rounded-md border border-field bg-surface px-4 font-strong md:grow-0"
@click="throwAway"
>
{{ t('photos.upload.discard') }}
</button>
</div>
</div>
</section>
</template>
6 changes: 3 additions & 3 deletions components/SpaceHint.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
// On the dashboard and the location page: points to the space page as long as there is
// no partner. It shows nothing while loading, after a failed load, and to a pair.
// The pages load the space themselves (useDashboard, useLocation), so nothing is requested here.
// On the dashboard, location and photos pages: points to the space page as long as there
// is no partner. It shows nothing while loading, after a failed load, and to a pair.
// The pages load the space themselves (in their composables), so nothing is requested here.
const { t } = useI18n()
const { state } = useSpace()

Expand Down
Loading