feat: chat layer tracer: bootstrap, channel list, messages, composer
- createChatClient: bearer auth, one refresh-and-retry on 401, 422 field errors
- useChat over useState: host bootstrap, open a channel, send
- ChatPage, ChatChannelList, ChatMessageList, ChatComposer
- runtime config chat.apiBase, public.chat.{apiBase,channelsApiBase,botsApiBase,hostApiBase}
This commit is contained in:
25
app/components/Chat/ChannelList.vue
Normal file
25
app/components/Chat/ChannelList.vue
Normal file
@@ -0,0 +1,25 @@
|
||||
<script setup lang="ts">
|
||||
// The seeded channels of the community. There is no create button: members
|
||||
// join and leave the channels the host has.
|
||||
const { t } = useI18n()
|
||||
const chat = useChat()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<nav class="chat-sidebar" :aria-label="t('chat.channels')">
|
||||
<h2 class="chat-sidebar-title">{{ t('chat.channels') }}</h2>
|
||||
<ul class="chat-channels">
|
||||
<li v-for="c in chat.channels.value" :key="c.id">
|
||||
<button
|
||||
class="chat-channel"
|
||||
type="button"
|
||||
:data-channel-slug="c.slug"
|
||||
:aria-current="chat.activeId.value === c.conversation_id ? 'true' : undefined"
|
||||
@click="chat.open(c)"
|
||||
>
|
||||
# {{ c.name }}
|
||||
</button>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
</template>
|
||||
28
app/components/Chat/Composer.vue
Normal file
28
app/components/Chat/Composer.vue
Normal file
@@ -0,0 +1,28 @@
|
||||
<script setup lang="ts">
|
||||
// The message form: Enter sends, Shift+Enter adds a line.
|
||||
const props = defineProps<{ conversationId: number }>()
|
||||
const { t } = useI18n()
|
||||
const chat = useChat()
|
||||
|
||||
const text = computed({
|
||||
get: () => chat.drafts.value[props.conversationId] ?? '',
|
||||
set: (v: string) => (chat.drafts.value[props.conversationId] = v),
|
||||
})
|
||||
|
||||
async function submit() {
|
||||
await chat.send(text.value)
|
||||
}
|
||||
|
||||
function onKeydown(e: KeyboardEvent) {
|
||||
if (e.isComposing || e.key !== 'Enter' || e.shiftKey) return
|
||||
e.preventDefault()
|
||||
void submit()
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<form class="chat-composer" @submit.prevent="submit">
|
||||
<textarea v-model="text" class="chat-composer-input" rows="2" :aria-label="t('chat.message')" :readonly="chat.sending.value" @keydown="onKeydown"></textarea>
|
||||
<button class="chat-send" type="submit" :disabled="!text.trim() || chat.sending.value">{{ t('chat.send') }}</button>
|
||||
</form>
|
||||
</template>
|
||||
16
app/components/Chat/MessageList.vue
Normal file
16
app/components/Chat/MessageList.vue
Normal file
@@ -0,0 +1,16 @@
|
||||
<script setup lang="ts">
|
||||
// The messages of one conversation, oldest first, as plain text.
|
||||
const props = defineProps<{ conversationId: number }>()
|
||||
const { t } = useI18n()
|
||||
const chat = useChat()
|
||||
const list = computed(() => chat.messages.value[props.conversationId] ?? [])
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="chat-list" role="log" :aria-label="t('chat.messages')">
|
||||
<article v-for="m in list" :key="m.id" class="chat-msg" :data-message-id="m.id">
|
||||
<span class="chat-msg-name">{{ m.sender_name }}</span>
|
||||
<p class="chat-msg-body">{{ m.content }}</p>
|
||||
</article>
|
||||
</div>
|
||||
</template>
|
||||
26
app/components/Chat/Page.vue
Normal file
26
app/components/Chat/Page.vue
Normal file
@@ -0,0 +1,26 @@
|
||||
<script setup lang="ts">
|
||||
// The full chat: the channel sidebar and the open conversation. Render it
|
||||
// inside <ClientOnly> (it uses window and the DOM).
|
||||
const { t } = useI18n()
|
||||
const chat = useChat()
|
||||
|
||||
onMounted(() => {
|
||||
void chat.start()
|
||||
})
|
||||
|
||||
const active = computed(() => chat.channels.value.find((c) => c.conversation_id === chat.activeId.value) ?? null)
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="chat-page">
|
||||
<ChatChannelList />
|
||||
<section class="chat-pane" :aria-label="active ? `# ${active.name}` : t('chat.name')">
|
||||
<template v-if="chat.activeId.value !== null">
|
||||
<ChatMessageList :conversation-id="chat.activeId.value" />
|
||||
<ChatComposer :conversation-id="chat.activeId.value" />
|
||||
</template>
|
||||
<p v-else class="chat-pane-empty">{{ t('chat.pickChannel') }}</p>
|
||||
<p v-if="chat.error.value" class="chat-error" role="alert">{{ t(`chat.error.${chat.error.value}`) }}</p>
|
||||
</section>
|
||||
</div>
|
||||
</template>
|
||||
Reference in New Issue
Block a user