From 30c13478d61065a512f5bc8824fecbf2ee6afc81 Mon Sep 17 00:00:00 2001 From: Kit La Touche Date: Thu, 3 Oct 2024 23:30:10 -0400 Subject: Handle basics of interface scrolling --- hi-ui/src/apiServer.js | 61 ++++++++++++++++++++++++++++++++-- hi-ui/src/lib/ActiveChannel.svelte | 31 +++++++++-------- hi-ui/src/lib/CreateChannelForm.svelte | 3 +- hi-ui/src/lib/LogIn.svelte | 3 +- hi-ui/src/lib/LogOut.svelte | 3 +- hi-ui/src/lib/Message.svelte | 38 +++++++++++++++++++++ hi-ui/src/lib/MessageInput.svelte | 18 +++++----- hi-ui/src/routes/+page.svelte | 5 ++- 8 files changed, 130 insertions(+), 32 deletions(-) create mode 100644 hi-ui/src/lib/Message.svelte (limited to 'hi-ui') diff --git a/hi-ui/src/apiServer.js b/hi-ui/src/apiServer.js index 7365a36..5e521de 100644 --- a/hi-ui/src/apiServer.js +++ b/hi-ui/src/apiServer.js @@ -1,5 +1,5 @@ import axios from 'axios'; -import { events } from './store'; +import { activeChannel, channelsList, events } from './store'; export const apiServer = axios.create({ baseURL: '/api/', @@ -33,13 +33,70 @@ export async function postToChannel(channelId, message) { return apiServer.post(`/channels/${channelId}`, { message }); } +export async function deleteMessage(messageId) { + // TODO +} + export function subscribeToEvents() { const evtSource = new EventSource("/api/events"); + events.update(() => []); // TODO: this should process all incoming events and store them. // TODO: eventually we'll need to handle expiring old info, so as not to use // infinite browser memory. + /* + * Known message types as of now: + * - created: a channel is created. + * - action: ignore. + * - message: a message is created. + * - action: display message in channel. + * - message_deleted: a message is deleted. + * - action: replace message with <...>. + * - deleted: a channel is deleted. + * - action: remove channel from sidebar. + */ evtSource.onmessage = (evt) => { const data = JSON.parse(evt.data); - events.update((value) => [...value, data]); + + switch (data.type) { + case 'created': + break; + case 'message': + events.update((value) => { + const eventList = [...value, data]; + eventList.sort((a, b) => a.at - b.at); + return eventList; + }); + break; + case 'message_deleted': + events.update((value) => { + const eventList = value.map((el) => { + if (el.message?.id === data.message) { + el.message.body = '«…»'; + return el + } else { + return el; + } + }); + return eventList; + }); + break; + case 'deleted': + activeChannel.update((value) => { + if (value?.id === data.channel) { + return null; + } + return value; + }); + channelsList.update((value) => { + const channelIndex = value.map((e) => e.id).indexOf(data.channel); + if (channelIndex !== -1) { + value.splice(channelIndex, 1); + } + return value; + }); + break; + default: + break; + } } } diff --git a/hi-ui/src/lib/ActiveChannel.svelte b/hi-ui/src/lib/ActiveChannel.svelte index 680a785..84f9119 100644 --- a/hi-ui/src/lib/ActiveChannel.svelte +++ b/hi-ui/src/lib/ActiveChannel.svelte @@ -1,28 +1,33 @@ -
+
{#each messages as message} -
-
{message.at} @{message.sender.name}: {message.message.body}
+
+
{/each}
diff --git a/hi-ui/src/lib/CreateChannelForm.svelte b/hi-ui/src/lib/CreateChannelForm.svelte index 584fa61..70dc13d 100644 --- a/hi-ui/src/lib/CreateChannelForm.svelte +++ b/hi-ui/src/lib/CreateChannelForm.svelte @@ -7,7 +7,6 @@ let disabled = false; async function handleSubmit(event) { - event.preventDefault(); disabled = true; const response = await createChannel(name); if (200 <= response.status && response.status < 300) { @@ -18,7 +17,7 @@ } -
+
diff --git a/hi-ui/src/lib/LogIn.svelte b/hi-ui/src/lib/LogIn.svelte index df734ee..1ec6772 100644 --- a/hi-ui/src/lib/LogIn.svelte +++ b/hi-ui/src/lib/LogIn.svelte @@ -7,7 +7,6 @@ let password = ''; async function handleLogin(event) { - event.preventDefault(); disabled = true; const response = await logIn(username, password); if (200 <= response.status && response.status < 300) { @@ -19,7 +18,7 @@ } -
+
+ {/if} +
+ + diff --git a/hi-ui/src/lib/MessageInput.svelte b/hi-ui/src/lib/MessageInput.svelte index 96e9577..938e467 100644 --- a/hi-ui/src/lib/MessageInput.svelte +++ b/hi-ui/src/lib/MessageInput.svelte @@ -1,27 +1,25 @@ - - + + diff --git a/hi-ui/src/routes/+page.svelte b/hi-ui/src/routes/+page.svelte index 646665e..66b4f8d 100644 --- a/hi-ui/src/routes/+page.svelte +++ b/hi-ui/src/routes/+page.svelte @@ -25,6 +25,7 @@ case 200: currentUser.update(() => ({ username: response.data.login.name, + id: response.data.login.id, })); subscribeToEvents(); break; @@ -66,7 +67,7 @@ -- cgit v1.2.3