game-village-politics-frontend/app/components/game/Chat.vue

91 lines
4.3 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import type { ChatPush } from '~/types/game'
const props = defineProps<{ worldId: number; tribe: boolean }>()
const chat = useChat()
const { state } = chat
const { t } = useI18n()
const clock = useNow()
watch(() => props.worldId, (id) => chat.enter(id), { immediate: true })
// A tribe joined or left changes the channels.
watch(() => props.tribe, () => chat.enter(props.worldId))
ws.listenWhileMounted<ChatPush>('chat:message', null, (data) => {
if (data?.world_id === props.worldId) chat.receive(data.channel, data.message)
})
const name = (c: { channel: string; with: { name: string } | null }) => (c.with ? c.with.name : t(`chat.channels.${c.channel}`))
const messages = computed(() => state.value.messages[state.value.channel] ?? [])
const draft = ref('')
const someone = ref('')
const talking = ref(false)
const list = ref<HTMLElement | null>(null)
async function send() {
if (draft.value.trim() && (await chat.send(draft.value))) draft.value = ''
}
async function talk() {
if (someone.value.trim() && (await chat.talkTo(someone.value))) {
someone.value = ''
talking.value = false
}
}
// Keep the newest message in view.
watch(
() => messages.value.length,
async () => {
await nextTick()
if (list.value) list.value.scrollTop = list.value.scrollHeight
},
)
watch(() => state.value.open, async (open) => {
await nextTick()
if (open && list.value) list.value.scrollTop = list.value.scrollHeight
})
const time = (iso: string) => formatArrival(Date.parse(iso), clock.value, t)
</script>
<!-- The world's chat in a window over the stage: a tab per channel, the talk, a line to write. -->
<template>
<div v-if="state.open" class="pointer-events-none fixed bottom-16 left-1 z-20 flex h-[26rem] max-h-[70dvh] w-[24rem] max-w-[calc(100vw-0.5rem)] md:bottom-2 md:left-2">
<UiWindow class="w-full" :title="$t('chat.title')" closable drag-key="chat" @close="chat.toggle()">
<div class="flex h-[calc(26rem-2rem)] max-h-[calc(70dvh-2rem)] flex-col text-sm">
<nav class="flex flex-wrap gap-0.5 border-b border-wood-700 bg-parchment-200 p-1" role="tablist">
<button
v-for="c in state.channels"
:key="c.channel"
type="button"
role="tab"
:aria-selected="state.channel === c.channel"
class="inline-flex items-center gap-1 border border-wood-800 px-1.5 py-0.5 font-semibold"
:class="state.channel === c.channel ? 'bg-wood-800 text-gold-400' : 'bg-parchment-300 text-wood-950 hover:bg-parchment-100'"
@click="chat.show(c.channel)"
>
{{ name(c) }}
<span v-if="c.unread" class="font-bold text-blood-700 tabular-nums">({{ c.unread }})</span>
</button>
<button type="button" class="btn btn-ghost px-1.5 py-0.5" :title="$t('chat.talk_to')" @click="talking = !talking">+</button>
</nav>
<form v-if="talking" class="flex gap-1 border-b border-wood-700 p-1" @submit.prevent="talk">
<input v-model="someone" class="input min-w-0 flex-1 py-0.5" :placeholder="$t('chat.player')" />
<button type="submit" class="btn btn-primary py-0.5">{{ $t('chat.open') }}</button>
</form>
<div ref="list" class="scroll-y min-h-0 flex-1 space-y-1 bg-parchment-50 p-1.5">
<button v-if="state.more[state.channel]" type="button" class="btn btn-ghost w-full py-0.5 text-xs" @click="chat.older()">{{ $t('chat.older') }}</button>
<p v-if="!messages.length" class="py-6 text-center text-ink-500">{{ $t('chat.empty') }}</p>
<p v-for="m in messages" :key="m.id" class="break-words">
<span class="font-semibold text-wood-900">{{ m.player.name }}</span>
<span class="ml-1 text-xs text-ink-500 tabular-nums">{{ time(m.at) }}</span>
<span class="block whitespace-pre-line">{{ m.body }}</span>
</p>
</div>
<p v-if="chat.error.value" class="alert m-1" role="alert">{{ chat.error.value }}</p>
<form class="flex gap-1 border-t border-wood-700 p-1" @submit.prevent="send">
<input v-model="draft" class="input min-w-0 flex-1 py-1" maxlength="500" :placeholder="$t('chat.write')" />
<button type="submit" class="btn btn-primary py-1">{{ $t('chat.send') }}</button>
</form>
</div>
</UiWindow>
</div>
</template>