Feat v0.5.2 chat surface
Chat surface package (packages/chat/) with conversation list, message thread, compose bar, participant sidebar, create conversation dialog, typing indicators, read receipts, message editing, and message deletion. Built on chat-core library with realtime event subscriptions. Fix stale token login deadlock: REST client auth endpoints excluded from 401 retry loop, 8s defensive boot timeout on auth.boot(). Fix bundled package permissions: auto-grant with NULL granted_by and set status active on install. Switch docker-compose to named volume for remote Docker host compat. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
521
packages/chat/css/main.css
Normal file
521
packages/chat/css/main.css
Normal file
@@ -0,0 +1,521 @@
|
||||
/* ═══════════════════════════════════════════
|
||||
Chat Surface — Styles (v0.1.0)
|
||||
═══════════════════════════════════════════ */
|
||||
|
||||
/* ── Layout ─────────────────────────────── */
|
||||
|
||||
.chat-app {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100vh;
|
||||
overflow: hidden;
|
||||
background: var(--bg-primary, #fff);
|
||||
color: var(--text-primary, #1a1a1a);
|
||||
}
|
||||
|
||||
.chat-loading {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
.chat-body {
|
||||
display: flex;
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.chat-main {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
/* ── Topbar extras ──────────────────────── */
|
||||
|
||||
.chat-topbar__thread-title {
|
||||
font-weight: 600;
|
||||
font-size: 14px;
|
||||
margin-right: 8px;
|
||||
color: var(--text-secondary, #666);
|
||||
}
|
||||
|
||||
/* ── Sidebar ────────────────────────────── */
|
||||
|
||||
.chat-sidebar {
|
||||
width: 280px;
|
||||
min-width: 280px;
|
||||
border-right: 1px solid var(--border, #e2e2e2);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background: var(--bg-secondary, #fafafa);
|
||||
}
|
||||
|
||||
.chat-sidebar__header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 12px 16px;
|
||||
border-bottom: 1px solid var(--border, #e2e2e2);
|
||||
}
|
||||
|
||||
.chat-sidebar__title {
|
||||
font-weight: 600;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.chat-sidebar__list {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.chat-sidebar__empty {
|
||||
padding: 24px 16px;
|
||||
text-align: center;
|
||||
color: var(--text-muted, #999);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.chat-sidebar__item {
|
||||
padding: 10px 16px;
|
||||
cursor: pointer;
|
||||
border-bottom: 1px solid var(--border-light, #f0f0f0);
|
||||
transition: background 0.1s;
|
||||
}
|
||||
|
||||
.chat-sidebar__item:hover {
|
||||
background: var(--bg-hover, #f0f0f0);
|
||||
}
|
||||
|
||||
.chat-sidebar__item--active {
|
||||
background: var(--bg-active, #e8f0fe);
|
||||
}
|
||||
|
||||
.chat-sidebar__item-top {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: baseline;
|
||||
margin-bottom: 2px;
|
||||
}
|
||||
|
||||
.chat-sidebar__item-title {
|
||||
font-weight: 600;
|
||||
font-size: 13px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
flex: 1;
|
||||
margin-right: 8px;
|
||||
}
|
||||
|
||||
.chat-sidebar__item-time {
|
||||
font-size: 11px;
|
||||
color: var(--text-muted, #999);
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.chat-sidebar__item-bottom {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.chat-sidebar__item-preview {
|
||||
font-size: 12px;
|
||||
color: var(--text-secondary, #666);
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.chat-sidebar__badge {
|
||||
background: var(--primary, #2563eb);
|
||||
color: #fff;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
min-width: 18px;
|
||||
height: 18px;
|
||||
border-radius: 9px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 0 5px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
/* ── Message Thread ─────────────────────── */
|
||||
|
||||
.chat-thread {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.chat-thread--empty {
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: var(--text-muted, #999);
|
||||
}
|
||||
|
||||
.chat-thread__messages {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 16px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.chat-thread__loading {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
padding: 24px;
|
||||
}
|
||||
|
||||
.chat-thread__load-more {
|
||||
align-self: center;
|
||||
background: none;
|
||||
border: 1px solid var(--border, #e2e2e2);
|
||||
border-radius: 4px;
|
||||
padding: 4px 12px;
|
||||
font-size: 12px;
|
||||
color: var(--text-secondary, #666);
|
||||
cursor: pointer;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.chat-thread__load-more:hover {
|
||||
background: var(--bg-hover, #f0f0f0);
|
||||
}
|
||||
|
||||
.chat-thread__typing {
|
||||
padding: 4px 16px 8px;
|
||||
font-size: 12px;
|
||||
color: var(--text-muted, #999);
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
/* ── Message Bubble ─────────────────────── */
|
||||
|
||||
.chat-msg {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 8px;
|
||||
padding: 4px 0;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.chat-msg--own {
|
||||
flex-direction: row-reverse;
|
||||
}
|
||||
|
||||
.chat-msg--system {
|
||||
justify-content: center;
|
||||
padding: 2px 0;
|
||||
}
|
||||
|
||||
.chat-msg--system span {
|
||||
font-size: 12px;
|
||||
color: var(--text-muted, #999);
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.chat-msg--deleted {
|
||||
justify-content: center;
|
||||
padding: 2px 0;
|
||||
}
|
||||
|
||||
.chat-msg--deleted em {
|
||||
font-size: 12px;
|
||||
color: var(--text-muted, #999);
|
||||
}
|
||||
|
||||
.chat-msg__body {
|
||||
max-width: 65%;
|
||||
background: var(--bg-secondary, #f5f5f5);
|
||||
border-radius: 12px;
|
||||
padding: 8px 12px;
|
||||
}
|
||||
|
||||
.chat-msg--own .chat-msg__body {
|
||||
background: var(--primary, #2563eb);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.chat-msg__name {
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
color: var(--text-secondary, #666);
|
||||
display: block;
|
||||
margin-bottom: 2px;
|
||||
}
|
||||
|
||||
.chat-msg__content {
|
||||
font-size: 14px;
|
||||
line-height: 1.4;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
.chat-msg__meta {
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
align-items: center;
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
.chat-msg__time {
|
||||
font-size: 10px;
|
||||
color: var(--text-muted, #999);
|
||||
}
|
||||
|
||||
.chat-msg--own .chat-msg__time {
|
||||
color: rgba(255, 255, 255, 0.7);
|
||||
}
|
||||
|
||||
.chat-msg__edited {
|
||||
font-size: 10px;
|
||||
color: var(--text-muted, #999);
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.chat-msg--own .chat-msg__edited {
|
||||
color: rgba(255, 255, 255, 0.7);
|
||||
}
|
||||
|
||||
/* ── Message Actions ────────────────────── */
|
||||
|
||||
.chat-msg__actions {
|
||||
display: flex;
|
||||
gap: 2px;
|
||||
position: absolute;
|
||||
top: 0;
|
||||
right: 0;
|
||||
background: var(--bg-primary, #fff);
|
||||
border: 1px solid var(--border, #e2e2e2);
|
||||
border-radius: 6px;
|
||||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
|
||||
padding: 2px;
|
||||
}
|
||||
|
||||
.chat-msg--own .chat-msg__actions {
|
||||
right: auto;
|
||||
left: 0;
|
||||
}
|
||||
|
||||
.chat-msg__action {
|
||||
background: none;
|
||||
border: none;
|
||||
padding: 4px 6px;
|
||||
cursor: pointer;
|
||||
border-radius: 4px;
|
||||
font-size: 14px;
|
||||
line-height: 1;
|
||||
color: var(--text-secondary, #666);
|
||||
}
|
||||
|
||||
.chat-msg__action:hover {
|
||||
background: var(--bg-hover, #f0f0f0);
|
||||
}
|
||||
|
||||
.chat-msg__action--danger:hover {
|
||||
background: var(--danger-bg, #fee);
|
||||
color: var(--danger, #dc2626);
|
||||
}
|
||||
|
||||
/* ── Message Edit ───────────────────────── */
|
||||
|
||||
.chat-msg__edit {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.chat-msg__edit-input {
|
||||
width: 100%;
|
||||
min-width: 200px;
|
||||
border: 1px solid var(--border, #e2e2e2);
|
||||
border-radius: 6px;
|
||||
padding: 6px 8px;
|
||||
font-size: 14px;
|
||||
font-family: inherit;
|
||||
resize: vertical;
|
||||
background: var(--bg-primary, #fff);
|
||||
color: var(--text-primary, #1a1a1a);
|
||||
}
|
||||
|
||||
.chat-msg__edit-actions {
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
/* ── Compose Bar ────────────────────────── */
|
||||
|
||||
.chat-compose {
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
gap: 8px;
|
||||
padding: 12px 16px;
|
||||
border-top: 1px solid var(--border, #e2e2e2);
|
||||
background: var(--bg-primary, #fff);
|
||||
}
|
||||
|
||||
.chat-compose__input {
|
||||
flex: 1;
|
||||
border: 1px solid var(--border, #e2e2e2);
|
||||
border-radius: 8px;
|
||||
padding: 8px 12px;
|
||||
font-size: 14px;
|
||||
font-family: inherit;
|
||||
resize: none;
|
||||
line-height: 1.4;
|
||||
max-height: 160px;
|
||||
background: var(--bg-primary, #fff);
|
||||
color: var(--text-primary, #1a1a1a);
|
||||
}
|
||||
|
||||
.chat-compose__input:focus {
|
||||
outline: none;
|
||||
border-color: var(--primary, #2563eb);
|
||||
box-shadow: 0 0 0 2px var(--primary-light, rgba(37, 99, 235, 0.15));
|
||||
}
|
||||
|
||||
/* ── Participant Sidebar ────────────────── */
|
||||
|
||||
.chat-participants {
|
||||
width: 240px;
|
||||
min-width: 240px;
|
||||
border-left: 1px solid var(--border, #e2e2e2);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background: var(--bg-secondary, #fafafa);
|
||||
}
|
||||
|
||||
.chat-participants__header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 12px 16px;
|
||||
border-bottom: 1px solid var(--border, #e2e2e2);
|
||||
font-weight: 600;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.chat-participants__list {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 8px 0;
|
||||
}
|
||||
|
||||
.chat-participants__item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 6px 16px;
|
||||
}
|
||||
|
||||
.chat-participants__name {
|
||||
flex: 1;
|
||||
font-size: 13px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.chat-participants__badge {
|
||||
font-size: 10px;
|
||||
color: var(--primary, #2563eb);
|
||||
font-weight: 600;
|
||||
margin-left: 4px;
|
||||
}
|
||||
|
||||
.chat-participants__status {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
border-radius: 50%;
|
||||
background: var(--text-muted, #ccc);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.chat-participants__status--online {
|
||||
background: var(--success, #16a34a);
|
||||
}
|
||||
|
||||
.chat-participants__remove {
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--text-muted, #999);
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
padding: 0 4px;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.chat-participants__remove:hover {
|
||||
color: var(--danger, #dc2626);
|
||||
}
|
||||
|
||||
/* ── New Conversation Dialog ────────────── */
|
||||
|
||||
.chat-new {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
min-width: 320px;
|
||||
}
|
||||
|
||||
.chat-new__type {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.chat-new__type label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
font-size: 13px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.chat-new__title {
|
||||
border: 1px solid var(--border, #e2e2e2);
|
||||
border-radius: 6px;
|
||||
padding: 8px 10px;
|
||||
font-size: 14px;
|
||||
font-family: inherit;
|
||||
background: var(--bg-primary, #fff);
|
||||
color: var(--text-primary, #1a1a1a);
|
||||
}
|
||||
|
||||
.chat-new__selected {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.chat-new__chip {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
background: var(--bg-active, #e8f0fe);
|
||||
color: var(--primary, #2563eb);
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
padding: 3px 8px;
|
||||
border-radius: 12px;
|
||||
}
|
||||
|
||||
.chat-new__chip button {
|
||||
background: none;
|
||||
border: none;
|
||||
color: inherit;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
padding: 0;
|
||||
line-height: 1;
|
||||
}
|
||||
772
packages/chat/js/main.js
Normal file
772
packages/chat/js/main.js
Normal file
@@ -0,0 +1,772 @@
|
||||
/**
|
||||
* Chat — Surface Entry Point (v0.1.0)
|
||||
*
|
||||
* Messaging surface built on chat-core library:
|
||||
* sw.api.ext('chat-core') — conversation/message CRUD
|
||||
* sw.api.ext('chat') — typing indicators
|
||||
* sw.realtime — live events
|
||||
* sw.ui.* — primitive components
|
||||
* sw.shell.Topbar — navigation bar
|
||||
*/
|
||||
(async function () {
|
||||
'use strict';
|
||||
|
||||
var mount = document.getElementById('extension-mount');
|
||||
if (!mount) return;
|
||||
|
||||
var base = window.__BASE__ || '';
|
||||
var ver = window.__VERSION__ || '0';
|
||||
|
||||
// ── Boot SDK ───────────────────────────────
|
||||
try {
|
||||
if (!window.preact) {
|
||||
var { h, render } = await import(base + '/js/sw/vendor/preact.module.js');
|
||||
var hooksModule = await import(base + '/js/sw/vendor/hooks.module.js');
|
||||
var htmModule = await import(base + '/js/sw/vendor/htm.module.js');
|
||||
window.preact = { h, render };
|
||||
window.hooks = hooksModule;
|
||||
window.html = htmModule.default.bind(h);
|
||||
}
|
||||
var sdk = await import(base + '/js/sw/sdk/index.js?v=' + ver);
|
||||
await sdk.boot();
|
||||
} catch (e) {
|
||||
mount.innerHTML = '<p style="color:var(--danger);padding:24px;">SDK boot failed: ' + e.message + '</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
var { html } = window;
|
||||
var { useState, useEffect, useCallback, useRef, useMemo } = hooks;
|
||||
var { render } = preact;
|
||||
|
||||
// ── SDK modules ────────────────────────────
|
||||
var api = sw.api.ext('chat-core');
|
||||
var chatApi = sw.api.ext('chat');
|
||||
var { Button, Spinner, Avatar, Dialog, Tabs } = sw.ui;
|
||||
var Topbar = sw.shell.Topbar;
|
||||
|
||||
// Import UserPicker directly (not in sw.ui index)
|
||||
var { UserPicker } = await import(base + '/js/sw/primitives/user-picker.js?v=' + ver);
|
||||
|
||||
// ── Helpers ────────────────────────────────
|
||||
var _timers = {};
|
||||
function debounce(key, fn, ms) {
|
||||
clearTimeout(_timers[key]);
|
||||
_timers[key] = setTimeout(fn, ms);
|
||||
}
|
||||
|
||||
function timeAgo(ts) {
|
||||
if (!ts) return '';
|
||||
var d = new Date(ts);
|
||||
var now = Date.now();
|
||||
var diff = Math.floor((now - d.getTime()) / 1000);
|
||||
if (diff < 60) return 'now';
|
||||
if (diff < 3600) return Math.floor(diff / 60) + 'm';
|
||||
if (diff < 86400) return Math.floor(diff / 3600) + 'h';
|
||||
if (diff < 604800) return Math.floor(diff / 86400) + 'd';
|
||||
return d.toLocaleDateString();
|
||||
}
|
||||
|
||||
function truncate(str, len) {
|
||||
if (!str) return '';
|
||||
return str.length > len ? str.slice(0, len) + '\u2026' : str;
|
||||
}
|
||||
|
||||
function currentUserId() {
|
||||
return sw.auth?.user?.id || '';
|
||||
}
|
||||
|
||||
function currentDisplayName() {
|
||||
return sw.auth?.user?.display_name || sw.auth?.user?.username || '';
|
||||
}
|
||||
|
||||
|
||||
// ═══════════════════════════════════════════
|
||||
// ConversationList — left sidebar
|
||||
// ═══════════════════════════════════════════
|
||||
|
||||
function ConversationList({ selected, onSelect, onNew, conversations, unread }) {
|
||||
return html`
|
||||
<div class="chat-sidebar">
|
||||
<div class="chat-sidebar__header">
|
||||
<span class="chat-sidebar__title">Conversations</span>
|
||||
<${Button} size="sm" onClick=${onNew}>New<//>
|
||||
</div>
|
||||
<div class="chat-sidebar__list">
|
||||
${conversations.length === 0 && html`
|
||||
<div class="chat-sidebar__empty">No conversations yet</div>`}
|
||||
${conversations.map(c => html`
|
||||
<div key=${c.id}
|
||||
class=${'chat-sidebar__item' + (selected === c.id ? ' chat-sidebar__item--active' : '')}
|
||||
onClick=${() => onSelect(c.id)}>
|
||||
<div class="chat-sidebar__item-top">
|
||||
<span class="chat-sidebar__item-title">${c.title || 'Untitled'}</span>
|
||||
<span class="chat-sidebar__item-time">${timeAgo(c.updated_at || c.created_at)}</span>
|
||||
</div>
|
||||
<div class="chat-sidebar__item-bottom">
|
||||
<span class="chat-sidebar__item-preview">
|
||||
${c.last_message
|
||||
? truncate(c.last_message.content_type === 'system'
|
||||
? '\u2022 ' + c.last_message.content
|
||||
: c.last_message.content, 60)
|
||||
: 'No messages yet'}
|
||||
</span>
|
||||
${(unread[c.id] || 0) > 0 && html`
|
||||
<span class="chat-sidebar__badge">${unread[c.id]}</span>`}
|
||||
</div>
|
||||
</div>`)}
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
|
||||
// ═══════════════════════════════════════════
|
||||
// MessageBubble — single message
|
||||
// ═══════════════════════════════════════════
|
||||
|
||||
function MessageBubble({ msg, isOwn, onEdit, onDelete }) {
|
||||
var [editing, setEditing] = useState(false);
|
||||
var [editText, setEditText] = useState('');
|
||||
var [hover, setHover] = useState(false);
|
||||
|
||||
if (msg._deleted) {
|
||||
return html`
|
||||
<div class="chat-msg chat-msg--deleted">
|
||||
<em>This message was deleted</em>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
if (msg.content_type === 'system') {
|
||||
return html`
|
||||
<div class="chat-msg chat-msg--system">
|
||||
<span>${msg.content}</span>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function startEdit() {
|
||||
setEditText(msg.content);
|
||||
setEditing(true);
|
||||
}
|
||||
|
||||
function cancelEdit() {
|
||||
setEditing(false);
|
||||
setEditText('');
|
||||
}
|
||||
|
||||
function saveEdit() {
|
||||
if (editText.trim() && editText !== msg.content) {
|
||||
onEdit(msg.id, editText.trim());
|
||||
}
|
||||
setEditing(false);
|
||||
}
|
||||
|
||||
function onEditKeyDown(e) {
|
||||
if (e.key === 'Escape') cancelEdit();
|
||||
if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); saveEdit(); }
|
||||
}
|
||||
|
||||
return html`
|
||||
<div class=${'chat-msg' + (isOwn ? ' chat-msg--own' : '')}
|
||||
onMouseEnter=${() => setHover(true)}
|
||||
onMouseLeave=${() => setHover(false)}>
|
||||
${!isOwn && html`
|
||||
<${Avatar} name=${msg._display_name || msg.participant_id} size="sm" />`}
|
||||
<div class="chat-msg__body">
|
||||
${!isOwn && html`<span class="chat-msg__name">${msg._display_name || msg.participant_id}</span>`}
|
||||
${editing ? html`
|
||||
<div class="chat-msg__edit">
|
||||
<textarea class="chat-msg__edit-input"
|
||||
value=${editText}
|
||||
onInput=${e => setEditText(e.target.value)}
|
||||
onKeyDown=${onEditKeyDown}
|
||||
rows="2" />
|
||||
<div class="chat-msg__edit-actions">
|
||||
<${Button} size="sm" variant="secondary" onClick=${cancelEdit}>Cancel<//>
|
||||
<${Button} size="sm" onClick=${saveEdit}>Save<//>
|
||||
</div>
|
||||
</div>
|
||||
` : html`
|
||||
<div class="chat-msg__content">${msg.content}</div>`}
|
||||
<div class="chat-msg__meta">
|
||||
<span class="chat-msg__time">${timeAgo(msg.created_at)}</span>
|
||||
${msg.edited_at && html`<span class="chat-msg__edited">(edited)</span>`}
|
||||
</div>
|
||||
</div>
|
||||
${hover && isOwn && !editing && html`
|
||||
<div class="chat-msg__actions">
|
||||
<button class="chat-msg__action" onClick=${startEdit} title="Edit">✎</button>
|
||||
<button class="chat-msg__action chat-msg__action--danger" onClick=${() => onDelete(msg.id)} title="Delete">🗑</button>
|
||||
</div>`}
|
||||
</div>`;
|
||||
}
|
||||
|
||||
|
||||
// ═══════════════════════════════════════════
|
||||
// MessageThread — center pane
|
||||
// ═══════════════════════════════════════════
|
||||
|
||||
function MessageThread({ conversationId, participants }) {
|
||||
var [messages, setMessages] = useState([]);
|
||||
var [loading, setLoading] = useState(false);
|
||||
var [hasMore, setHasMore] = useState(false);
|
||||
var [nextCursor, setNextCursor] = useState('');
|
||||
var [typingUsers, setTypingUsers] = useState({});
|
||||
var bottomRef = useRef(null);
|
||||
var listRef = useRef(null);
|
||||
var userId = currentUserId();
|
||||
|
||||
// Build participant lookup
|
||||
var partMap = useMemo(() => {
|
||||
var m = {};
|
||||
(participants || []).forEach(p => { m[p.participant_id] = p.display_name || p.participant_id; });
|
||||
return m;
|
||||
}, [participants]);
|
||||
|
||||
// Enrich messages with display names
|
||||
function enrichMessages(msgs) {
|
||||
return msgs.map(m => ({ ...m, _display_name: partMap[m.participant_id] || m.participant_id }));
|
||||
}
|
||||
|
||||
// Load initial messages
|
||||
useEffect(() => {
|
||||
if (!conversationId) { setMessages([]); return; }
|
||||
setLoading(true);
|
||||
setMessages([]);
|
||||
setNextCursor('');
|
||||
setHasMore(false);
|
||||
api.get('/messages/' + conversationId + '?limit=50').then(res => {
|
||||
var data = res || {};
|
||||
var msgs = (data.messages || []).reverse();
|
||||
setMessages(enrichMessages(msgs));
|
||||
setHasMore(!!data.has_more);
|
||||
setNextCursor(data.next_cursor || '');
|
||||
setLoading(false);
|
||||
setTimeout(() => scrollToBottom(), 50);
|
||||
}).catch(() => setLoading(false));
|
||||
}, [conversationId, partMap]);
|
||||
|
||||
// Load older messages
|
||||
function loadMore() {
|
||||
if (!hasMore || !nextCursor || loading) return;
|
||||
setLoading(true);
|
||||
api.get('/messages/' + conversationId + '?limit=50&cursor=' + encodeURIComponent(nextCursor)).then(res => {
|
||||
var data = res || {};
|
||||
var older = (data.messages || []).reverse();
|
||||
setMessages(prev => [...enrichMessages(older), ...prev]);
|
||||
setHasMore(!!data.has_more);
|
||||
setNextCursor(data.next_cursor || '');
|
||||
setLoading(false);
|
||||
}).catch(() => setLoading(false));
|
||||
}
|
||||
|
||||
function scrollToBottom() {
|
||||
if (bottomRef.current) bottomRef.current.scrollIntoView({ behavior: 'auto' });
|
||||
}
|
||||
|
||||
// Scroll listener for load-more
|
||||
useEffect(() => {
|
||||
var el = listRef.current;
|
||||
if (!el) return;
|
||||
function onScroll() {
|
||||
if (el.scrollTop < 80 && hasMore && !loading) loadMore();
|
||||
}
|
||||
el.addEventListener('scroll', onScroll);
|
||||
return () => el.removeEventListener('scroll', onScroll);
|
||||
}, [hasMore, loading, nextCursor, conversationId]);
|
||||
|
||||
// Realtime: new messages, edits, deletes
|
||||
useEffect(() => {
|
||||
if (!conversationId) return;
|
||||
var channel = 'conversation:' + conversationId;
|
||||
|
||||
var unsubs = [
|
||||
sw.realtime.subscribe(channel, 'message', (payload) => {
|
||||
var msg = { ...payload, _display_name: partMap[payload.participant_id] || payload.participant_id };
|
||||
setMessages(prev => [...prev, msg]);
|
||||
setTimeout(() => scrollToBottom(), 50);
|
||||
// Auto mark read if from someone else
|
||||
if (payload.participant_id !== userId && payload.id) {
|
||||
api.post('/read/' + conversationId, { last_read_message_id: payload.id }).catch(() => {});
|
||||
}
|
||||
}),
|
||||
sw.realtime.subscribe(channel, 'message.edited', (payload) => {
|
||||
setMessages(prev => prev.map(m =>
|
||||
m.id === payload.id ? { ...m, content: payload.content, edited_at: 'true' } : m
|
||||
));
|
||||
}),
|
||||
sw.realtime.subscribe(channel, 'message.deleted', (payload) => {
|
||||
setMessages(prev => prev.map(m =>
|
||||
m.id === payload.id ? { ...m, _deleted: true, content: '' } : m
|
||||
));
|
||||
}),
|
||||
];
|
||||
|
||||
// Typing indicators
|
||||
var typingTimers = {};
|
||||
unsubs.push(sw.realtime.subscribe(channel, 'typing', (payload) => {
|
||||
var pid = payload.participant_id;
|
||||
if (pid === userId) return;
|
||||
setTypingUsers(prev => ({ ...prev, [pid]: payload.display_name || pid }));
|
||||
clearTimeout(typingTimers[pid]);
|
||||
typingTimers[pid] = setTimeout(() => {
|
||||
setTypingUsers(prev => {
|
||||
var next = { ...prev };
|
||||
delete next[pid];
|
||||
return next;
|
||||
});
|
||||
}, 4000);
|
||||
}));
|
||||
|
||||
return () => {
|
||||
unsubs.forEach(fn => fn());
|
||||
Object.values(typingTimers).forEach(clearTimeout);
|
||||
setTypingUsers({});
|
||||
};
|
||||
}, [conversationId, partMap, userId]);
|
||||
|
||||
// Mark read on first load
|
||||
useEffect(() => {
|
||||
if (!conversationId || messages.length === 0) return;
|
||||
var last = messages[messages.length - 1];
|
||||
if (last && last.id) {
|
||||
api.post('/read/' + conversationId, { last_read_message_id: last.id }).catch(() => {});
|
||||
}
|
||||
}, [conversationId, messages.length > 0]);
|
||||
|
||||
// Edit message
|
||||
function handleEdit(msgId, newContent) {
|
||||
api.put('/messages/' + conversationId + '/' + msgId, { content: newContent }).then(() => {
|
||||
setMessages(prev => prev.map(m =>
|
||||
m.id === msgId ? { ...m, content: newContent, edited_at: 'true' } : m
|
||||
));
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
// Delete message
|
||||
async function handleDelete(msgId) {
|
||||
var ok = await sw.ui.confirm('Delete this message?', { destructive: true });
|
||||
if (!ok) return;
|
||||
api.del('/messages/' + conversationId + '/' + msgId).then(() => {
|
||||
setMessages(prev => prev.map(m =>
|
||||
m.id === msgId ? { ...m, _deleted: true, content: '' } : m
|
||||
));
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
// Typing indicator text
|
||||
var typingNames = Object.values(typingUsers);
|
||||
var typingText = '';
|
||||
if (typingNames.length === 1) typingText = typingNames[0] + ' is typing\u2026';
|
||||
else if (typingNames.length === 2) typingText = typingNames.join(' and ') + ' are typing\u2026';
|
||||
else if (typingNames.length > 2) typingText = 'Several people are typing\u2026';
|
||||
|
||||
if (!conversationId) {
|
||||
return html`
|
||||
<div class="chat-thread chat-thread--empty">
|
||||
<p>Select a conversation or start a new one</p>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
return html`
|
||||
<div class="chat-thread">
|
||||
<div class="chat-thread__messages" ref=${listRef}>
|
||||
${loading && messages.length === 0 && html`<div class="chat-thread__loading"><${Spinner} /></div>`}
|
||||
${hasMore && html`
|
||||
<button class="chat-thread__load-more" onClick=${loadMore} disabled=${loading}>
|
||||
Load older messages
|
||||
</button>`}
|
||||
${messages.map(m => html`
|
||||
<${MessageBubble}
|
||||
key=${m.id}
|
||||
msg=${m}
|
||||
isOwn=${m.participant_id === userId}
|
||||
onEdit=${handleEdit}
|
||||
onDelete=${handleDelete}
|
||||
/>`)}
|
||||
<div ref=${bottomRef} />
|
||||
</div>
|
||||
${typingText && html`<div class="chat-thread__typing">${typingText}</div>`}
|
||||
</div>`;
|
||||
}
|
||||
|
||||
|
||||
// ═══════════════════════════════════════════
|
||||
// ComposeBar — message input
|
||||
// ═══════════════════════════════════════════
|
||||
|
||||
function ComposeBar({ conversationId }) {
|
||||
var [text, setText] = useState('');
|
||||
var [sending, setSending] = useState(false);
|
||||
var textareaRef = useRef(null);
|
||||
|
||||
// Reset on conversation change
|
||||
useEffect(() => { setText(''); }, [conversationId]);
|
||||
|
||||
// Auto-focus
|
||||
useEffect(() => {
|
||||
if (textareaRef.current) textareaRef.current.focus();
|
||||
}, [conversationId]);
|
||||
|
||||
// Auto-resize
|
||||
function autoResize(el) {
|
||||
if (!el) return;
|
||||
el.style.height = 'auto';
|
||||
el.style.height = Math.min(el.scrollHeight, 160) + 'px';
|
||||
}
|
||||
|
||||
function handleInput(e) {
|
||||
setText(e.target.value);
|
||||
autoResize(e.target);
|
||||
// Typing indicator (debounced)
|
||||
debounce('typing', () => {
|
||||
chatApi.post('/typing/' + conversationId, {
|
||||
display_name: currentDisplayName(),
|
||||
}).catch(() => {});
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
async function handleSend() {
|
||||
var content = text.trim();
|
||||
if (!content || sending) return;
|
||||
setSending(true);
|
||||
try {
|
||||
await api.post('/messages/' + conversationId, { content: content, content_type: 'text' });
|
||||
setText('');
|
||||
if (textareaRef.current) {
|
||||
textareaRef.current.style.height = 'auto';
|
||||
textareaRef.current.focus();
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[chat] send failed:', e);
|
||||
}
|
||||
setSending(false);
|
||||
}
|
||||
|
||||
function onKeyDown(e) {
|
||||
if (e.key === 'Enter' && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
handleSend();
|
||||
}
|
||||
}
|
||||
|
||||
if (!conversationId) return null;
|
||||
|
||||
return html`
|
||||
<div class="chat-compose">
|
||||
<textarea class="chat-compose__input"
|
||||
ref=${textareaRef}
|
||||
value=${text}
|
||||
placeholder="Type a message\u2026"
|
||||
rows="1"
|
||||
onInput=${handleInput}
|
||||
onKeyDown=${onKeyDown} />
|
||||
<${Button} onClick=${handleSend} disabled=${!text.trim() || sending}>Send<//>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
|
||||
// ═══════════════════════════════════════════
|
||||
// ParticipantSidebar — right panel
|
||||
// ═══════════════════════════════════════════
|
||||
|
||||
function ParticipantSidebar({ conversationId, participants, onRefresh, isAdmin }) {
|
||||
var [addOpen, setAddOpen] = useState(false);
|
||||
var [presence, setPresence] = useState({});
|
||||
|
||||
// Query presence
|
||||
useEffect(() => {
|
||||
if (!participants || participants.length === 0) return;
|
||||
var ids = participants.map(p => p.participant_id).join(',');
|
||||
sw.api.get('/api/v1/presence?users=' + ids).then(res => {
|
||||
setPresence(res || {});
|
||||
}).catch(() => {});
|
||||
}, [participants]);
|
||||
|
||||
async function addUser(user) {
|
||||
setAddOpen(false);
|
||||
await api.post('/participants/' + conversationId, {
|
||||
participant_id: user.id,
|
||||
display_name: user.display_name || user.username,
|
||||
}).catch(() => {});
|
||||
onRefresh();
|
||||
}
|
||||
|
||||
async function removeUser(pid) {
|
||||
var ok = await sw.ui.confirm('Remove this participant?', { destructive: true });
|
||||
if (!ok) return;
|
||||
await api.del('/participants/' + conversationId + '/' + pid).catch(() => {});
|
||||
onRefresh();
|
||||
}
|
||||
|
||||
return html`
|
||||
<div class="chat-participants">
|
||||
<div class="chat-participants__header">
|
||||
<span>Participants (${(participants || []).length})</span>
|
||||
${isAdmin && html`<${Button} size="sm" onClick=${() => setAddOpen(true)}>Add<//>` }
|
||||
</div>
|
||||
<div class="chat-participants__list">
|
||||
${(participants || []).map(p => html`
|
||||
<div key=${p.participant_id} class="chat-participants__item">
|
||||
<${Avatar} name=${p.display_name || p.participant_id} size="sm" />
|
||||
<span class="chat-participants__name">
|
||||
${p.display_name || p.participant_id}
|
||||
${p.role === 'admin' && html`<span class="chat-participants__badge">admin</span>`}
|
||||
</span>
|
||||
<span class=${'chat-participants__status' + (presence[p.participant_id] ? ' chat-participants__status--online' : '')} />
|
||||
${isAdmin && p.participant_id !== currentUserId() && html`
|
||||
<button class="chat-participants__remove" onClick=${() => removeUser(p.participant_id)} title="Remove">\u00d7</button>`}
|
||||
</div>`)}
|
||||
</div>
|
||||
|
||||
<${Dialog} open=${addOpen} title="Add Participant" onClose=${() => setAddOpen(false)}>
|
||||
<${UserPicker} onSelect=${addUser} placeholder="Search users\u2026" autoFocus />
|
||||
<//>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
|
||||
// ═══════════════════════════════════════════
|
||||
// NewConversationDialog
|
||||
// ═══════════════════════════════════════════
|
||||
|
||||
function NewConversationDialog({ open, onClose, onCreated }) {
|
||||
var [type, setType] = useState('group');
|
||||
var [title, setTitle] = useState('');
|
||||
var [selected, setSelected] = useState([]);
|
||||
var [creating, setCreating] = useState(false);
|
||||
|
||||
function reset() {
|
||||
setType('group');
|
||||
setTitle('');
|
||||
setSelected([]);
|
||||
setCreating(false);
|
||||
}
|
||||
|
||||
function addUser(user) {
|
||||
// Avoid duplicates
|
||||
if (selected.find(u => u.id === user.id)) return;
|
||||
// For DM, only allow one user
|
||||
if (type === 'direct' && selected.length >= 1) {
|
||||
setSelected([user]);
|
||||
return;
|
||||
}
|
||||
setSelected(prev => [...prev, user]);
|
||||
}
|
||||
|
||||
function removeSelected(id) {
|
||||
setSelected(prev => prev.filter(u => u.id !== id));
|
||||
}
|
||||
|
||||
async function handleCreate() {
|
||||
if (selected.length === 0) return;
|
||||
setCreating(true);
|
||||
try {
|
||||
var convTitle = type === 'direct'
|
||||
? ''
|
||||
: (title.trim() || selected.map(u => u.display_name || u.username).join(', '));
|
||||
var participants = selected.map(u => ({
|
||||
id: u.id,
|
||||
display_name: u.display_name || u.username,
|
||||
}));
|
||||
var res = await api.post('/conversations', {
|
||||
title: convTitle,
|
||||
type: type,
|
||||
participants: participants,
|
||||
creator_display_name: currentDisplayName(),
|
||||
});
|
||||
reset();
|
||||
onClose();
|
||||
onCreated(res.id || res.data?.id);
|
||||
} catch (e) {
|
||||
console.error('[chat] create failed:', e);
|
||||
}
|
||||
setCreating(false);
|
||||
}
|
||||
|
||||
var actions = [
|
||||
{ label: 'Cancel', variant: 'secondary', onClick: () => { reset(); onClose(); } },
|
||||
{ label: 'Create', variant: 'primary', onClick: handleCreate, disabled: creating || selected.length === 0 },
|
||||
];
|
||||
|
||||
return html`
|
||||
<${Dialog} open=${open} title="New Conversation" onClose=${() => { reset(); onClose(); }} actions=${actions}>
|
||||
<div class="chat-new">
|
||||
<div class="chat-new__type">
|
||||
<label>
|
||||
<input type="radio" name="convType" value="group"
|
||||
checked=${type === 'group'} onChange=${() => { setType('group'); setSelected([]); }} />
|
||||
Group
|
||||
</label>
|
||||
<label>
|
||||
<input type="radio" name="convType" value="direct"
|
||||
checked=${type === 'direct'} onChange=${() => { setType('direct'); setSelected([]); }} />
|
||||
Direct Message
|
||||
</label>
|
||||
</div>
|
||||
${type === 'group' && html`
|
||||
<input class="chat-new__title" type="text" value=${title}
|
||||
placeholder="Conversation title (optional)"
|
||||
onInput=${e => setTitle(e.target.value)} />`}
|
||||
<${UserPicker} onSelect=${addUser} placeholder=${type === 'direct' ? 'Search for a user\u2026' : 'Add participants\u2026'} />
|
||||
${selected.length > 0 && html`
|
||||
<div class="chat-new__selected">
|
||||
${selected.map(u => html`
|
||||
<span key=${u.id} class="chat-new__chip">
|
||||
${u.display_name || u.username}
|
||||
<button onClick=${() => removeSelected(u.id)}>\u00d7</button>
|
||||
</span>`)}
|
||||
</div>`}
|
||||
</div>
|
||||
<//>`;
|
||||
}
|
||||
|
||||
|
||||
// ═══════════════════════════════════════════
|
||||
// ChatApp — root component
|
||||
// ═══════════════════════════════════════════
|
||||
|
||||
function ChatApp() {
|
||||
var [conversations, setConversations] = useState([]);
|
||||
var [unread, setUnread] = useState({});
|
||||
var [selectedId, setSelectedId] = useState(null);
|
||||
var [participants, setParticipants] = useState([]);
|
||||
var [showParticipants, setShowParticipants] = useState(false);
|
||||
var [showNew, setShowNew] = useState(false);
|
||||
var [loading, setLoading] = useState(true);
|
||||
var userId = currentUserId();
|
||||
|
||||
// Load conversations
|
||||
// Note: SDK auto-unwraps {data: [...]} envelopes → res is the array directly
|
||||
function loadConversations() {
|
||||
return api.get('/conversations').then(res => {
|
||||
setConversations(Array.isArray(res) ? res : (res && res.data) || []);
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
// Load unread
|
||||
// Note: SDK returns {data: {id: count}} — NOT auto-unwrapped (data is object, not array)
|
||||
function loadUnread() {
|
||||
return api.get('/unread').then(res => {
|
||||
setUnread((res && res.data) || (typeof res === 'object' && !Array.isArray(res) ? res : {}));
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
// Initial load
|
||||
useEffect(() => {
|
||||
Promise.all([loadConversations(), loadUnread()]).then(() => setLoading(false));
|
||||
}, []);
|
||||
|
||||
// Load participants when conversation changes
|
||||
useEffect(() => {
|
||||
if (!selectedId) { setParticipants([]); return; }
|
||||
api.get('/participants/' + selectedId).then(res => {
|
||||
setParticipants(Array.isArray(res) ? res : (res && res.data) || []);
|
||||
}).catch(() => {});
|
||||
}, [selectedId]);
|
||||
|
||||
// Realtime: refresh conversation list on activity
|
||||
useEffect(() => {
|
||||
// Subscribe to all active conversations
|
||||
var unsubs = [];
|
||||
conversations.forEach(c => {
|
||||
unsubs.push(sw.realtime.subscribe('conversation:' + c.id, 'message', () => {
|
||||
loadConversations();
|
||||
loadUnread();
|
||||
}));
|
||||
unsubs.push(sw.realtime.subscribe('conversation:' + c.id, 'participant.added', () => {
|
||||
loadConversations();
|
||||
if (c.id === selectedId) {
|
||||
api.get('/participants/' + selectedId).then(res => {
|
||||
setParticipants(Array.isArray(res) ? res : (res && res.data) || []);
|
||||
}).catch(() => {});
|
||||
}
|
||||
}));
|
||||
unsubs.push(sw.realtime.subscribe('conversation:' + c.id, 'participant.removed', () => {
|
||||
loadConversations();
|
||||
if (c.id === selectedId) {
|
||||
api.get('/participants/' + selectedId).then(res => {
|
||||
setParticipants(Array.isArray(res) ? res : (res && res.data) || []);
|
||||
}).catch(() => {});
|
||||
}
|
||||
}));
|
||||
});
|
||||
return () => unsubs.forEach(fn => fn());
|
||||
}, [conversations.map(c => c.id).join(','), selectedId]);
|
||||
|
||||
// Select conversation
|
||||
function selectConversation(id) {
|
||||
setSelectedId(id);
|
||||
setShowParticipants(false);
|
||||
// Mark read
|
||||
setUnread(prev => { var next = { ...prev }; delete next[id]; return next; });
|
||||
}
|
||||
|
||||
// After creating a new conversation
|
||||
function onCreated(id) {
|
||||
loadConversations().then(() => {
|
||||
if (id) setSelectedId(id);
|
||||
});
|
||||
}
|
||||
|
||||
// Is current user admin in selected conversation?
|
||||
var isAdmin = useMemo(() => {
|
||||
return participants.some(p => p.participant_id === userId && p.role === 'admin');
|
||||
}, [participants, userId]);
|
||||
|
||||
// Refresh participants
|
||||
function refreshParticipants() {
|
||||
if (!selectedId) return;
|
||||
api.get('/participants/' + selectedId).then(res => {
|
||||
setParticipants(Array.isArray(res) ? res : (res && res.data) || []);
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
// Conversation title for topbar
|
||||
var selectedConv = conversations.find(c => c.id === selectedId);
|
||||
var threadTitle = selectedConv ? (selectedConv.title || 'Direct Message') : '';
|
||||
|
||||
if (loading) {
|
||||
return html`<div class="chat-loading"><${Spinner} /></div>`;
|
||||
}
|
||||
|
||||
return html`
|
||||
<div class="chat-app">
|
||||
<${Topbar} title="Chat">
|
||||
${selectedId && html`
|
||||
<span class="chat-topbar__thread-title">${threadTitle}</span>
|
||||
<${Button} size="sm" variant="secondary"
|
||||
onClick=${() => setShowParticipants(!showParticipants)}>
|
||||
${showParticipants ? 'Hide' : 'People'}
|
||||
<//>`}
|
||||
<//>
|
||||
<div class="chat-body">
|
||||
<${ConversationList}
|
||||
selected=${selectedId}
|
||||
onSelect=${selectConversation}
|
||||
onNew=${() => setShowNew(true)}
|
||||
conversations=${conversations}
|
||||
unread=${unread} />
|
||||
<div class="chat-main">
|
||||
<${MessageThread}
|
||||
conversationId=${selectedId}
|
||||
participants=${participants} />
|
||||
<${ComposeBar} conversationId=${selectedId} />
|
||||
</div>
|
||||
${showParticipants && selectedId && html`
|
||||
<${ParticipantSidebar}
|
||||
conversationId=${selectedId}
|
||||
participants=${participants}
|
||||
onRefresh=${refreshParticipants}
|
||||
isAdmin=${isAdmin} />`}
|
||||
</div>
|
||||
<${NewConversationDialog}
|
||||
open=${showNew}
|
||||
onClose=${() => setShowNew(false)}
|
||||
onCreated=${onCreated} />
|
||||
</div>`;
|
||||
}
|
||||
|
||||
|
||||
// ── Mount ──────────────────────────────────
|
||||
render(html`<${ChatApp} />`, mount);
|
||||
|
||||
})();
|
||||
30
packages/chat/manifest.json
Normal file
30
packages/chat/manifest.json
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"id": "chat",
|
||||
"title": "Chat",
|
||||
"type": "full",
|
||||
"tier": "starlark",
|
||||
"route": "/s/chat",
|
||||
"auth": "authenticated",
|
||||
"layout": "single",
|
||||
"version": "0.1.0",
|
||||
"icon": "\ud83d\udcac",
|
||||
"description": "Chat surface — conversations, messaging, typing indicators, read receipts.",
|
||||
"author": "switchboard",
|
||||
|
||||
"permissions": ["db.write", "realtime.publish"],
|
||||
|
||||
"depends": ["chat-core"],
|
||||
|
||||
"api_routes": [
|
||||
{"method": "POST", "path": "/typing/*"}
|
||||
],
|
||||
|
||||
"settings": {
|
||||
"enter_to_send": {
|
||||
"type": "boolean",
|
||||
"label": "Enter to Send",
|
||||
"description": "Press Enter to send messages (Shift+Enter for newline). When off, Enter inserts a newline.",
|
||||
"default": true
|
||||
}
|
||||
}
|
||||
}
|
||||
39
packages/chat/script.star
Normal file
39
packages/chat/script.star
Normal file
@@ -0,0 +1,39 @@
|
||||
# Chat Surface — Starlark Backend (v0.1.0)
|
||||
#
|
||||
# Thin surface layer on top of chat-core library.
|
||||
# Only handles typing indicator broadcast — all CRUD is
|
||||
# delegated to chat-core's own API routes.
|
||||
#
|
||||
# Modules: json, realtime
|
||||
|
||||
def _resp(status, data):
|
||||
return {"status": status, "body": json.encode(data), "headers": {"Content-Type": "application/json"}}
|
||||
|
||||
|
||||
def on_request(req):
|
||||
path = req["path"]
|
||||
method = req["method"]
|
||||
user_id = req.get("user_id", "")
|
||||
|
||||
# POST /typing/:conversation_id — broadcast typing indicator
|
||||
if method == "POST" and path.startswith("/typing/"):
|
||||
cid = path[len("/typing/"):]
|
||||
if not cid:
|
||||
return _resp(400, {"error": "conversation_id required"})
|
||||
|
||||
body = json.decode(req.get("body", "{}"))
|
||||
display_name = str(body.get("display_name", ""))
|
||||
|
||||
realtime.publish(
|
||||
"conversation:" + cid,
|
||||
"typing",
|
||||
{
|
||||
"participant_id": user_id,
|
||||
"display_name": display_name,
|
||||
"conversation_id": cid,
|
||||
},
|
||||
)
|
||||
|
||||
return _resp(200, {"ok": True})
|
||||
|
||||
return _resp(404, {"error": "not found"})
|
||||
Reference in New Issue
Block a user