split notedeck into crates

This splits notedeck into crates, separating the browser chrome and
individual apps:

* notedeck: binary file, browser chrome
* notedeck_columns: our columns app
* enostr: same as before

We still need to do more work to cleanly separate the chrome apis
from the app apis. Soon I will create notedeck-notebook to see what
makes sense to be shared between the apps.

Some obvious ones that come to mind:

1. ImageCache

We will likely want to move this to the notedeck crate, as most apps
will want some kind of image cache. In web browsers, web pages do not
need to worry about this, so we will likely have to do something similar

2. Ndb

Since NdbRef is threadsafe and Ndb is an Arc<NdbRef>, it can be safely
copied to each app. This will simplify things. In the future we might
want to create an abstraction over this? Maybe each app shouldn't have
access to the same database... we assume the data in DBs are all public
anyways, but if we have unwrapped giftwraps that could be a problem.

3. RelayPool / Subscription Manager

The browser should probably maintain these. Then apps can use ken's
high level subscription manager api and not have to worry about
connection pool details

4. Accounts

Accounts and key management should be handled by the chrome. Apps should
only have a simple signer interface.

That's all for now, just something to think about!

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-12-11 02:53:05 -08:00
parent 10cbdf15f0
commit 74c5f0c748
156 changed files with 194 additions and 252 deletions

View File

@@ -0,0 +1,214 @@
use enostr::{NoteId, Pubkey};
use std::{
borrow::Cow,
fmt::{self},
};
use crate::{
accounts::AccountsRoute,
column::Columns,
timeline::{TimelineId, TimelineRoute},
ui::add_column::AddColumnRoute,
};
/// App routing. These describe different places you can go inside Notedeck.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum Route {
Timeline(TimelineRoute),
Accounts(AccountsRoute),
Relays,
ComposeNote,
AddColumn(AddColumnRoute),
Support,
NewDeck,
EditDeck(usize),
}
impl Route {
pub fn timeline(timeline_id: TimelineId) -> Self {
Route::Timeline(TimelineRoute::Timeline(timeline_id))
}
pub fn timeline_id(&self) -> Option<&TimelineId> {
if let Route::Timeline(TimelineRoute::Timeline(tid)) = self {
Some(tid)
} else {
None
}
}
pub fn relays() -> Self {
Route::Relays
}
pub fn thread(thread_root: NoteId) -> Self {
Route::Timeline(TimelineRoute::Thread(thread_root))
}
pub fn profile(pubkey: Pubkey) -> Self {
Route::Timeline(TimelineRoute::Profile(pubkey))
}
pub fn reply(replying_to: NoteId) -> Self {
Route::Timeline(TimelineRoute::Reply(replying_to))
}
pub fn quote(quoting: NoteId) -> Self {
Route::Timeline(TimelineRoute::Quote(quoting))
}
pub fn accounts() -> Self {
Route::Accounts(AccountsRoute::Accounts)
}
pub fn add_account() -> Self {
Route::Accounts(AccountsRoute::AddAccount)
}
pub fn title(&self, columns: &Columns) -> Cow<'static, str> {
match self {
Route::Timeline(tlr) => match tlr {
TimelineRoute::Timeline(id) => {
let timeline = columns
.find_timeline(*id)
.expect("expected to find timeline");
timeline.kind.to_title()
}
TimelineRoute::Thread(_id) => Cow::Borrowed("Thread"),
TimelineRoute::Reply(_id) => Cow::Borrowed("Reply"),
TimelineRoute::Quote(_id) => Cow::Borrowed("Quote"),
TimelineRoute::Profile(_pubkey) => Cow::Borrowed("Profile"),
},
Route::Relays => Cow::Borrowed("Relays"),
Route::Accounts(amr) => match amr {
AccountsRoute::Accounts => Cow::Borrowed("Accounts"),
AccountsRoute::AddAccount => Cow::Borrowed("Add Account"),
},
Route::ComposeNote => Cow::Borrowed("Compose Note"),
Route::AddColumn(c) => match c {
AddColumnRoute::Base => Cow::Borrowed("Add Column"),
AddColumnRoute::UndecidedNotification => Cow::Borrowed("Add Notifications Column"),
AddColumnRoute::ExternalNotification => {
Cow::Borrowed("Add External Notifications Column")
}
AddColumnRoute::Hashtag => Cow::Borrowed("Add Hashtag Column"),
},
Route::Support => Cow::Borrowed("Damus Support"),
Route::NewDeck => Cow::Borrowed("Add Deck"),
Route::EditDeck(_) => Cow::Borrowed("Edit Deck"),
}
}
}
// TODO: add this to egui-nav so we don't have to deal with returning
// and navigating headaches
#[derive(Clone)]
pub struct Router<R: Clone> {
routes: Vec<R>,
pub returning: bool,
pub navigating: bool,
replacing: bool,
}
impl<R: Clone> Router<R> {
pub fn new(routes: Vec<R>) -> Self {
if routes.is_empty() {
panic!("routes can't be empty")
}
let returning = false;
let navigating = false;
let replacing = false;
Router {
routes,
returning,
navigating,
replacing,
}
}
pub fn route_to(&mut self, route: R) {
self.navigating = true;
self.routes.push(route);
}
// Route to R. Then when it is successfully placed, should call `remove_previous_routes` to remove all previous routes
pub fn route_to_replaced(&mut self, route: R) {
self.navigating = true;
self.replacing = true;
self.routes.push(route);
}
/// Go back, start the returning process
pub fn go_back(&mut self) -> Option<R> {
if self.returning || self.routes.len() == 1 {
return None;
}
self.returning = true;
self.prev().cloned()
}
/// Pop a route, should only be called on a NavRespose::Returned reseponse
pub fn pop(&mut self) -> Option<R> {
if self.routes.len() == 1 {
return None;
}
self.returning = false;
self.routes.pop()
}
pub fn remove_previous_routes(&mut self) {
let num_routes = self.routes.len();
if num_routes <= 1 {
return;
}
self.returning = false;
self.replacing = false;
self.routes.drain(..num_routes - 1);
}
pub fn is_replacing(&self) -> bool {
self.replacing
}
pub fn top(&self) -> &R {
self.routes.last().expect("routes can't be empty")
}
pub fn prev(&self) -> Option<&R> {
self.routes.get(self.routes.len() - 2)
}
pub fn routes(&self) -> &Vec<R> {
&self.routes
}
}
impl fmt::Display for Route {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Route::Timeline(tlr) => match tlr {
TimelineRoute::Timeline(name) => write!(f, "{}", name),
TimelineRoute::Thread(_id) => write!(f, "Thread"),
TimelineRoute::Profile(_id) => write!(f, "Profile"),
TimelineRoute::Reply(_id) => write!(f, "Reply"),
TimelineRoute::Quote(_id) => write!(f, "Quote"),
},
Route::Relays => write!(f, "Relays"),
Route::Accounts(amr) => match amr {
AccountsRoute::Accounts => write!(f, "Accounts"),
AccountsRoute::AddAccount => write!(f, "Add Account"),
},
Route::ComposeNote => write!(f, "Compose Note"),
Route::AddColumn(_) => write!(f, "Add Column"),
Route::Support => write!(f, "Support"),
Route::NewDeck => write!(f, "Add Deck"),
Route::EditDeck(_) => write!(f, "Edit Deck"),
}
}
}