add new chat logic and message service

This commit is contained in:
JCEEE
2026-08-05 11:20:27 +01:00
parent 3378e1dce7
commit fed2f32258
11 changed files with 1013 additions and 14 deletions
+353
View File
@@ -0,0 +1,353 @@
<script lang="ts">
import { onMount } from 'svelte';
import { chatStore } from '$lib/stores/chat.svelte';
import { chatIcon, sendIcon } from './icons';
import { formatWeekday, formatHumanDate } from '$lib/format';
let { role = 'child' }: { role?: string } = $props();
let draft = $state('');
let sending = $state(false);
let error = $state('');
let container: HTMLDivElement | undefined = $state();
let idleTimer: ReturnType<typeof setTimeout> | undefined;
const isOwn = (msg: any) => msg.authorId === chatStore.actorId;
function scrollToBottom(instant = false) {
if (!container) return;
container.scrollTo({
top: container.scrollHeight,
behavior: instant ? 'auto' : 'smooth'
});
}
$effect(() => {
const n = chatStore.messages.length;
if (n > 0) {
const last = chatStore.messages[n - 1];
// Only auto-scroll when the newest message is ours, or on open.
if (isOwn(last) || chatStore.open) scrollToBottom(true);
}
});
async function send() {
const text = draft.trim();
if (!text || sending) return;
sending = true;
error = '';
draft = '';
clearIdle();
chatStore.setTyping(false).catch(() => {});
try {
await chatStore.send(text);
scrollToBottom(true);
} catch {
error = 'Could not send. Try again.';
} finally {
sending = false;
requestAnimationFrame(() => scrollToBottom(true));
}
}
function onInput() {
chatStore.setTyping(true).catch(() => {});
clearIdle();
idleTimer = setTimeout(() => {
chatStore.setTyping(false).catch(() => {});
}, 2500);
}
function clearIdle() {
if (idleTimer) clearTimeout(idleTimer);
idleTimer = undefined;
}
function onClose() {
clearIdle();
chatStore.setTyping(false).catch(() => {});
chatStore.closeChat();
}
function insertMention(name: string) {
draft = draft.trimEnd();
draft = (draft ? draft + ' ' : '') + '@' + name + ' ';
}
// Render content, wrapping @mentions in a styled token.
function renderContent(text: string) {
const segs: { text: string; mention: boolean }[] = [];
const re = /(@[A-Za-z0-9_.-]+)/g;
let last = 0;
let m: RegExpExecArray | null;
while ((m = re.exec(text)) !== null) {
if (m.index > last) segs.push({ text: text.slice(last, m.index), mention: false });
segs.push({ text: m[1], mention: true });
last = m.index + m[0].length;
}
if (last < text.length) segs.push({ text: text.slice(last), mention: false });
return segs;
}
onMount(() => {
requestAnimationFrame(() => scrollToBottom(true));
});
</script>
<div class="chat-panel" class:open={chatStore.open}>
<div class="chat-head">
<span class="chat-title">Family Chat</span>
<button class="chat-close" onclick={onClose} aria-label="Close chat">✕</button>
</div>
<div class="chat-messages" bind:this={container}>
{#if chatStore.messages.length === 0}
<p class="chat-empty">No messages yet. Say hi! 👋</p>
{:else}
{#each chatStore.messages as msg (msg.id)}
<div class="msg-row" class:own={isOwn(msg)}>
{#if !isOwn(msg)}
<span class="avatar" style="background:{msg.authorColor || '#6366f1'}">
{msg.authorName?.charAt(0).toUpperCase()}
</span>
{/if}
<div class="bubble-wrap">
{#if !isOwn(msg)}
<span class="msg-author">{msg.authorName}</span>
{/if}
<div class="bubble">
{#each renderContent(msg.content) as seg (msg.id + ':' + seg.text)}
{#if seg.mention}
<button class="mention" onclick={() => insertMention(seg.text.slice(1))}
>{seg.text}</button
>
{:else}
{seg.text}
{/if}
{/each}
<span class="msg-time">{formatWeekday(msg.createdAt)}</span>
</div>
</div>
</div>
{/each}
{/if}
</div>
<div class="chat-typing">
{#if chatStore.typingNames.length > 0}
<span class="typing-text">
{chatStore.typingNames.join(', ')}
{chatStore.typingNames.length === 1 ? 'is' : 'are'} typing…
</span>
{:else}
<span class="typing-text"></span>
{/if}
</div>
<div class="chat-composer">
{#if error}<div class="chat-error">{error}</div>{/if}
<input
type="text"
class="chat-input"
placeholder="Message the family…"
bind:value={draft}
oninput={onInput}
onkeydown={(e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
send();
}
}}
disabled={sending}
/>
<button class="chat-send" onclick={send} disabled={sending || !draft.trim()} aria-label="Send">
{@html sendIcon}
</button>
</div>
</div>
<style>
.chat-panel {
position: fixed;
top: 0;
right: 0;
height: 100dvh;
width: 500px;
background: #fff;
border-left: 1px solid #e5e7eb;
display: flex;
flex-direction: column;
z-index: 110;
transform: translateX(100%);
transition: transform 0.25s ease;
box-shadow: -8px 0 24px rgba(0, 0, 0, 0.08);
}
.chat-panel.open {
transform: translateX(0);
}
/* Mobile: full-screen fixed layer. */
@media (max-width: 767.98px) {
.chat-panel {
width: 100vw;
}
}
.chat-head {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.85rem 1.25rem;
border-bottom: 1px solid #e5e7eb;
background: #4338ca;
color: #fff;
}
.chat-title {
font-weight: 700;
font-size: 1rem;
}
.chat-close {
background: none;
border: none;
color: #c7d2fe;
font-size: 1.1rem;
cursor: pointer;
padding: 0.25rem 0.5rem;
}
.chat-close:hover {
color: #fff;
}
.chat-messages {
flex: 1;
overflow-y: auto;
padding: 1rem 1.25rem;
display: flex;
flex-direction: column;
gap: 0.6rem;
}
.chat-empty {
color: #9ca3af;
text-align: center;
margin-top: 3rem;
}
.msg-row {
display: flex;
align-items: flex-end;
gap: 0.5rem;
}
.msg-row.own {
justify-content: flex-end;
}
.avatar {
width: 28px;
height: 28px;
border-radius: 50%;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
font-size: 0.8rem;
font-weight: 700;
flex-shrink: 0;
}
.bubble-wrap {
max-width: 72%;
display: flex;
flex-direction: column;
}
.msg-row.own .bubble-wrap {
align-items: flex-end;
}
.msg-author {
font-size: 0.72rem;
color: #6b7280;
margin: 0 0 0.15rem 0.35rem;
font-weight: 600;
}
.bubble {
background: #f3f4f6;
border-radius: 14px;
border-bottom-left-radius: 4px;
padding: 0.5rem 0.75rem;
font-size: 0.9rem;
color: #111827;
line-height: 1.4;
word-break: break-word;
position: relative;
}
.msg-row.own .bubble {
background: #6366f1;
color: #fff;
border-bottom-left-radius: 14px;
border-bottom-right-radius: 4px;
}
.msg-time {
display: block;
font-size: 0.62rem;
color: #9ca3af;
margin-top: 0.25rem;
text-align: right;
text-transform: capitalize;
}
.msg-row.own .msg-time {
color: rgba(255, 255, 255, 0.7);
}
.mention {
background: #ede9fe;
color: #6d28d9;
border: none;
border-radius: 6px;
padding: 0 0.25rem;
font-weight: 600;
font-size: 0.85rem;
cursor: pointer;
}
.chat-typing {
min-height: 1.5rem;
padding: 0 1.25rem;
}
.typing-text {
font-size: 0.75rem;
color: #6b7280;
font-style: italic;
}
.chat-composer {
display: flex;
gap: 0.5rem;
padding: 0.75rem 1.25rem 1rem;
border-top: 1px solid #e5e7eb;
align-items: center;
}
.chat-error {
position: absolute;
bottom: 4.5rem;
color: #dc2626;
font-size: 0.75rem;
}
.chat-input {
flex: 1;
padding: 0.6rem 0.9rem;
border: 1px solid #d1d5db;
border-radius: 999px;
font-size: 0.9rem;
outline: none;
}
.chat-input:focus {
border-color: #6366f1;
}
.chat-send {
width: 40px;
height: 40px;
border-radius: 50%;
border: none;
background: #6366f1;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
flex-shrink: 0;
}
.chat-send:disabled {
opacity: 0.5;
cursor: default;
}
</style>
+2
View File
@@ -9,3 +9,5 @@ export const prefsIcon = '<svg width="18" height="18" viewBox="0 0 24 24" fill="
export const chevronLeft = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="15 18 9 12 15 6"/></svg>'
export const chevronRight = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="9 18 15 12 9 6"/></svg>'
export const bellIcon = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M18 8A6 6 0 006 8c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.73 21a2 2 0 01-3.46 0"/></svg>'
export const chatIcon = '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z"/></svg>'
export const sendIcon = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>'
+1
View File
@@ -6,3 +6,4 @@ export { default as Card } from './Card.svelte';
export { default as CardGrid } from './CardGrid.svelte';
export { default as Button } from './Button.svelte';
export { default as Accordion } from './Accordion.svelte';
export { default as Chat } from './Chat.svelte';