summaryrefslogtreecommitdiff
path: root/ui/lib/state/remote/messages.svelte.js
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2025-03-23 14:33:07 -0400
committerOwen Jacobson <owen@grimoire.ca>2025-03-23 14:33:07 -0400
commit876472299d67f8fe3a789b7777b9d8ee44297b23 (patch)
treedb62f5d1e15d871f8a73ce20b40cd53053d12f85 /ui/lib/state/remote/messages.svelte.js
parentfa0f653f141efee3f5a01e1fa696d29140ec12c2 (diff)
parentf788ea84e25a4f7216ca0604aeb216346403b6ef (diff)
Merge branch 'prop/restartable-state'
Diffstat (limited to 'ui/lib/state/remote/messages.svelte.js')
-rw-r--r--ui/lib/state/remote/messages.svelte.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/ui/lib/state/remote/messages.svelte.js b/ui/lib/state/remote/messages.svelte.js
new file mode 100644
index 0000000..576a74e
--- /dev/null
+++ b/ui/lib/state/remote/messages.svelte.js
@@ -0,0 +1,52 @@
+import { DateTime } from 'luxon';
+import { render } from '$lib/markdown.js';
+
+class Message {
+ static boot({ id, at, channel, sender, body }) {
+ return new Message({
+ id,
+ at: DateTime.fromISO(at),
+ channel,
+ sender,
+ body,
+ renderedBody: render(body)
+ });
+ }
+
+ constructor({ id, at, channel, sender, body, renderedBody }) {
+ this.id = id;
+ this.at = at;
+ this.channel = channel;
+ this.sender = sender;
+ this.body = body;
+ this.renderedBody = renderedBody;
+ }
+
+ resolve(get) {
+ const { sender, ...rest } = this;
+ return new Message({ sender: get.sender(sender), ...rest });
+ }
+}
+
+export class Messages {
+ all = $state([]);
+
+ static boot(messages) {
+ const all = messages.map(Message.boot);
+ return new Messages({ all });
+ }
+
+ constructor({ all }) {
+ this.all = all;
+ }
+
+ add({ id, at, channel, sender, body }) {
+ const message = Message.boot({ id, at, channel, sender, body });
+ this.all.push(message);
+ }
+
+ remove(id) {
+ const index = this.all.findIndex((message) => message.id === id);
+ this.all.splice(index, 1);
+ }
+}