diff options
| author | Kit La Touche <kit@transneptune.net> | 2024-12-03 13:44:05 -0500 |
|---|---|---|
| committer | Kit La Touche <kit@transneptune.net> | 2024-12-03 13:44:05 -0500 |
| commit | c6fbd21c27277dd76c4dbabf5f1bf24f58142a1a (patch) | |
| tree | 94392c1cafc88e91b14ebbb5a1e39eb86df41bc8 /ui/lib | |
| parent | d89d64a1bd71bbb0f545818794f574fc80046c0f (diff) | |
| parent | 4e3ad13aca163e733724b205c250bdb67cc56c29 (diff) | |
Merge branch 'main' into wip/stylize
Diffstat (limited to 'ui/lib')
| -rw-r--r-- | ui/lib/apiServer.js | 4 | ||||
| -rw-r--r-- | ui/lib/components/Channel.svelte | 8 | ||||
| -rw-r--r-- | ui/lib/components/Message.svelte | 12 | ||||
| -rw-r--r-- | ui/lib/store.js | 2 | ||||
| -rw-r--r-- | ui/lib/store/channels.js | 36 | ||||
| -rw-r--r-- | ui/lib/store/channels.svelte.js | 62 | ||||
| -rw-r--r-- | ui/lib/store/messages.svelte.js | 7 |
7 files changed, 85 insertions, 46 deletions
diff --git a/ui/lib/apiServer.js b/ui/lib/apiServer.js index fee1a81..e52daff 100644 --- a/ui/lib/apiServer.js +++ b/ui/lib/apiServer.js @@ -54,9 +54,9 @@ export async function acceptInvite(inviteId, username, password) { return apiServer.post(`/invite/${inviteId}`, data); } -export function subscribeToEvents(resume_point) { +export function subscribeToEvents(resumePoint) { const eventsUrl = new URL('/api/events', window.location); - eventsUrl.searchParams.append('resume_point', resume_point); + eventsUrl.searchParams.append('resume_point', resumePoint); const evtSource = new EventSource(eventsUrl.toString()); // 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 diff --git a/ui/lib/components/Channel.svelte b/ui/lib/components/Channel.svelte index e84c6d0..01b1c87 100644 --- a/ui/lib/components/Channel.svelte +++ b/ui/lib/components/Channel.svelte @@ -1,10 +1,14 @@ <script> - let { id, name, active } = $props(); + let { id, name, active, hasUnreads } = $props(); </script> <li class="rounded-full" class:bg-slate-400={active}> <a href="/ch/{id}"> - <span class="badge bg-primary-500">#</span> + {#if hasUnreads} + <span class="badge bg-warning-500">❦</span> + {:else} + <span class="badge bg-primary-500">¶</span> + {/if} <span class="flex-auto">{name}</span> </a> </li> diff --git a/ui/lib/components/Message.svelte b/ui/lib/components/Message.svelte index c3e6b16..aa9e3e9 100644 --- a/ui/lib/components/Message.svelte +++ b/ui/lib/components/Message.svelte @@ -1,4 +1,5 @@ <script> + import { DateTime } from 'luxon'; import { deleteMessage } from '$lib/apiServer'; function scroll(message) { @@ -7,6 +8,7 @@ let { id, at, body, renderedBody, editable = false } = $props(); let deleteArmed = $state(false); + let atFormatted = $derived(at.toLocaleString(DateTime.DATETIME_SHORT)); function onDelete(event) { event.preventDefault(); @@ -23,9 +25,15 @@ } </script> -<div class="message relative" class:bg-warning-800={deleteArmed} {onmouseleave} role="article"> +<div + class="message relative" + class:bg-warning-800={deleteArmed} + role="article" + data-at={at} + {onmouseleave} + > <div class="handle chip bg-surface-700 absolute -top-6 right-0"> - {at} + {atFormatted} {#if editable} <button onclick={onDelete}>🗑️</button> {/if} diff --git a/ui/lib/store.js b/ui/lib/store.js index 3b20e05..47ebbc2 100644 --- a/ui/lib/store.js +++ b/ui/lib/store.js @@ -1,5 +1,5 @@ import { writable } from 'svelte/store'; -import { Channels } from '$lib/store/channels'; +import { Channels } from '$lib/store/channels.svelte.js'; import { Messages } from '$lib/store/messages.svelte.js'; import { Logins } from '$lib/store/logins'; diff --git a/ui/lib/store/channels.js b/ui/lib/store/channels.js deleted file mode 100644 index 37dc673..0000000 --- a/ui/lib/store/channels.js +++ /dev/null @@ -1,36 +0,0 @@ -export class Channels { - constructor() { - this.channels = []; - } - - setChannels(channels) { - this.channels = [...channels]; - this.sort(); - return this; - } - - addChannel(id, name) { - this.channels = [...this.channels, { id, name }]; - this.sort(); - return this; - } - - deleteChannel(id) { - const channelIndex = this.channels.map((e) => e.id).indexOf(id); - if (channelIndex !== -1) { - this.channels.splice(channelIndex, 1); - } - return this; - } - - sort() { - this.channels.sort((a, b) => { - if (a.name < b.name) { - return -1; - } else if (a.name > b.name) { - return 1; - } - return 0; - }); - } -} diff --git a/ui/lib/store/channels.svelte.js b/ui/lib/store/channels.svelte.js new file mode 100644 index 0000000..8919be0 --- /dev/null +++ b/ui/lib/store/channels.svelte.js @@ -0,0 +1,62 @@ +import { DateTime } from 'luxon'; +const EPOCH_STRING = "1970-01-01T00:00:00Z"; + +// For reasons unclear to me, a straight up class definition with a constructor +// doesn't seem to work, reactively. So we resort to this. +// Owen suggests that this sentence in the Svelte docs should make the reason +// clear: +// > If $state is used with an array or a simple object, the result is a deeply +// > reactive state proxy. +// Emphasis on "simple object". +// --Kit +function makeChannelObject({ id, name, draft = '', lastReadAt = null, scrollPosition = null }) { + return { + id, + name, + lastReadAt: lastReadAt || DateTime.fromISO(EPOCH_STRING), + draft, + scrollPosition, + }; +} + +export class Channels { + channels = $state([]); + + getChannel(channelId) { + return this.channels.filter((ch) => ch.id === channelId)[0] || null; + } + + setChannels(channels) { + this.channels = channels.map(makeChannelObject); + this.sort(); + return this; + } + + addChannel(id, name) { + this.channels = [ + ...this.channels, + makeChannelObject({ id, name }), + ]; + this.sort(); + return this; + } + + deleteChannel(id) { + const channelIndex = this.channels.map((e) => e.id).indexOf(id); + if (channelIndex !== -1) { + this.channels.splice(channelIndex, 1); + } + return this; + } + + sort() { + this.channels.sort((a, b) => { + if (a.name < b.name) { + return -1; + } else if (a.name > b.name) { + return 1; + } + return 0; + }); + } +} diff --git a/ui/lib/store/messages.svelte.js b/ui/lib/store/messages.svelte.js index 0ceba54..ba4c895 100644 --- a/ui/lib/store/messages.svelte.js +++ b/ui/lib/store/messages.svelte.js @@ -1,17 +1,18 @@ +import { DateTime } from 'luxon'; import { marked } from 'marked'; import DOMPurify from 'dompurify'; const RUN_COALESCE_MAX_INTERVAL = 10 /* min */ * 60 /* sec */ * 1000; /* ms */ export class Messages { - channels = $state({}); + channels = $state({}); // Mapping<ChannelId, Message> inChannel(channel) { - return this.channels[channel]; + return this.channels[channel] || []; } addMessage(channel, id, { at, sender, body }) { - let parsedAt = new Date(at); + let parsedAt = DateTime.fromISO(at); let renderedBody = DOMPurify.sanitize(marked.parse(body, { breaks: true })); const message = { id, at: parsedAt, body, renderedBody }; |
