From 2e2e3980ab78052be74f4007c343e69a583648fe Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Thu, 10 Oct 2024 22:30:18 -0400 Subject: Compute the active channel from the current routing state, not from a store. --- ui/lib/apiServer.js | 3 +-- ui/lib/components/ActiveChannel.svelte | 7 ++++--- ui/lib/components/Channel.svelte | 8 +------- ui/lib/components/ChannelList.svelte | 8 +++----- ui/lib/components/MessageInput.svelte | 30 ++++++++++++++--------------- ui/lib/store.js | 3 +-- ui/lib/store/channels.js | 35 ---------------------------------- 7 files changed, 24 insertions(+), 70 deletions(-) (limited to 'ui/lib') diff --git a/ui/lib/apiServer.js b/ui/lib/apiServer.js index f6d6148..538fa85 100644 --- a/ui/lib/apiServer.js +++ b/ui/lib/apiServer.js @@ -1,5 +1,5 @@ import axios from 'axios'; -import { activeChannel, channelsList, logins, messages } from '$lib/store'; +import { channelsList, logins, messages } from '$lib/store'; export const apiServer = axios.create({ baseURL: '/api/', @@ -82,7 +82,6 @@ function onChannelEvent(data) { channelsList.update((value) => value.addChannel(data.id, data.name)) break; case 'deleted': - activeChannel.update((value) => value.deleteChannel(data.id)); channelsList.update((value) => value.deleteChannel(data.id)); messages.update((value) => value.deleteChannel(data.id)); break; diff --git a/ui/lib/components/ActiveChannel.svelte b/ui/lib/components/ActiveChannel.svelte index 978e952..ece9f55 100644 --- a/ui/lib/components/ActiveChannel.svelte +++ b/ui/lib/components/ActiveChannel.svelte @@ -1,10 +1,11 @@
  • { - channels = value.channels; - }); + $: channels = $channelsList.channels; diff --git a/ui/lib/components/MessageInput.svelte b/ui/lib/components/MessageInput.svelte index b33574b..0da78d4 100644 --- a/ui/lib/components/MessageInput.svelte +++ b/ui/lib/components/MessageInput.svelte @@ -1,30 +1,28 @@
    - +
    diff --git a/ui/lib/store.js b/ui/lib/store.js index b964b4b..ae17ffa 100644 --- a/ui/lib/store.js +++ b/ui/lib/store.js @@ -1,10 +1,9 @@ import { writable } from 'svelte/store'; -import { ActiveChannel, Channels } from '$lib/store/channels'; +import { Channels } from '$lib/store/channels'; import { Messages } from '$lib/store/messages'; import { Logins } from '$lib/store/logins'; export const currentUser = writable(null); -export const activeChannel = writable(new ActiveChannel()); export const logins = writable(new Logins()); export const channelsList = writable(new Channels()); export const messages = writable(new Messages()); diff --git a/ui/lib/store/channels.js b/ui/lib/store/channels.js index bb6c86c..b57ca7e 100644 --- a/ui/lib/store/channels.js +++ b/ui/lib/store/channels.js @@ -34,38 +34,3 @@ export class Channels { }); } } - -export class ActiveChannel { - constructor() { - this.channel = null; - } - - isSet() { - return this.channel !== null; - } - - get() { - return this.channel; - } - - is(id) { - return this.channel === id; - } - - set(id) { - this.channel = id; - return this; - } - - deleteChannel(id) { - if (this.is(id)) { - return this.clear(); - } - return this; - } - - clear() { - this.channel = null; - return this; - } -} -- cgit v1.2.3 From 72f3d8c5ab3e2a42cf1a76d0c08815dbe46e50a1 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Fri, 11 Oct 2024 01:18:18 -0400 Subject: Move login to its own route. This - in passing - fixes the problem where the client failed to subscribe after logging in, by causing the whole subscription process to be re-run when returning to the main interface. --- src/ui.rs | 46 +++++++++++++++++++++++++---------- ui/lib/apiServer.js | 2 ++ ui/lib/components/LogIn.svelte | 4 ++- ui/lib/components/LogOut.svelte | 8 ++++-- ui/lib/components/MessageInput.svelte | 2 +- ui/routes/(app)/+layout.svelte | 24 +++++++++--------- ui/routes/(login)/login/+page.svelte | 5 ++++ 7 files changed, 63 insertions(+), 28 deletions(-) create mode 100644 ui/routes/(login)/login/+page.svelte (limited to 'ui/lib') diff --git a/src/ui.rs b/src/ui.rs index b296325..34eb6f1 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -14,25 +14,45 @@ use crate::{app::App, channel, error::Internal, login::Login}; #[folder = "target/ui"] struct Assets; +impl Assets { + fn load(path: impl AsRef) -> Result> { + let path = path.as_ref(); + let mime = mime_guess::from_path(path).first_or_octet_stream(); + + Self::get(path) + .map(|file| Asset(mime, file)) + .ok_or(NotFound(format!("not found: {path}"))) + } + + fn index() -> Result { + // "not found" in this case really is an internal error, as it should + // never happen. `index.html` is a known-valid path. + Ok(Self::load("index.html")?) + } +} + pub fn router() -> Router { Router::new() .route("/*path", get(asset)) .route("/", get(root)) + .route("/login", get(login)) .route("/ch/:channel", get(channel)) } async fn asset(Path(path): Path) -> Result> { - let mime = mime_guess::from_path(&path).first_or_octet_stream(); + Assets::load(path) +} - Assets::get(&path) - .map(|file| Asset(mime, file)) - .ok_or(NotFound(format!("not found: {path}"))) +async fn root(login: Option) -> Result { + if login.is_none() { + Ok(Redirect::temporary("/login").into_response()) + } else { + Ok(Assets::index()?.into_response()) + } } -async fn root() -> Result { - // "not found" in this case really is an internal error, as it should - // never happen. `index.html` is a known-valid path. - Ok(asset(Path(String::from("index.html"))).await?) +async fn login() -> Result { + Assets::index() } async fn channel( @@ -40,13 +60,13 @@ async fn channel( login: Option, Path(channel): Path, ) -> Result { - Ok(if login.is_none() { - Redirect::temporary("/").into_response() + if login.is_none() { + Ok(Redirect::temporary("/").into_response()) } else if app.channels().get(&channel).await?.is_none() { - NotFound(root().await?).into_response() + Ok(NotFound(Assets::index()?).into_response()) } else { - root().await?.into_response() - }) + Ok(Assets::index()?.into_response()) + } } struct Asset(Mime, EmbeddedFile); diff --git a/ui/lib/apiServer.js b/ui/lib/apiServer.js index 538fa85..ccd6e66 100644 --- a/ui/lib/apiServer.js +++ b/ui/lib/apiServer.js @@ -66,6 +66,8 @@ export function subscribeToEvents(resume_point) { break; } } + + return evtSource; } function onLoginEvent(data) { diff --git a/ui/lib/components/LogIn.svelte b/ui/lib/components/LogIn.svelte index 2836e6d..e1cda8a 100644 --- a/ui/lib/components/LogIn.svelte +++ b/ui/lib/components/LogIn.svelte @@ -1,4 +1,5 @@
    - @{$currentUser.username} + {#if $currentUser} + @{$currentUser.username} + {/if}