Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

choose whether to merge/replace #93

Merged
merged 7 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## 0.5.2 (Unreleased)
## 0.5.2 (2025-01-28)

- Logging in no longer replaces your document. Instead, it asks if you'd like to merge or replace.

This release is available as a container image at `ghcr.io/bytes-zone/beeps:v0.5.2`.

## 0.5.1 (2025-01-28)

Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion beeps-server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "beeps-server"
version = "0.5.1"
version = "0.5.2"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion beeps/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "beeps"
version = "0.5.1"
version = "0.5.2"
edition = "2021"

[lints]
Expand Down
44 changes: 35 additions & 9 deletions beeps/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use popover::{AuthIntent, Popover};
use crate::config::Config;
use beeps_core::{
sync::{login, register, Client},
NodeId, Replica,
Document, NodeId, Replica,
};
use chrono::{DateTime, Local, Utc};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind};
Expand All @@ -42,7 +42,12 @@ pub struct App {

/// If we're replacing the entire replica on the next sync (for example when
/// initially logging in.)
replace_on_next_sync: bool,
in_first_sync: bool,

/// The document we got on our first sync. We keep this separate to decide
/// whether to replace our current document with this or merge them
/// together.
first_sync_document: Option<Document>,

/// State of the pings table
table_state: TableState,
Expand Down Expand Up @@ -89,7 +94,8 @@ impl App {
Ok(Self {
status_line: Some("Loaded replica".to_string()),
replica,
replace_on_next_sync: false,
in_first_sync: false,
first_sync_document: None,
client: auth,
last_sync: None,
table_state: TableState::new().with_selected(0),
Expand All @@ -104,7 +110,8 @@ impl App {
status_line: None,
replica: Replica::new(NodeId::random()),
client: auth,
replace_on_next_sync: false,
in_first_sync: false,
first_sync_document: None,
last_sync: None,
table_state: TableState::new().with_selected(0),
popover: None,
Expand Down Expand Up @@ -253,7 +260,7 @@ impl App {
}
Action::LoggedIn(client) => {
self.client = Some(client.clone());
self.replace_on_next_sync = true;
self.in_first_sync = true;

vec![
Effect::SaveSyncClientAuth(client.clone()),
Expand All @@ -273,12 +280,12 @@ impl App {
Action::Pulled(resp) => {
self.status_line = Some("Got a new doc from the server".to_string());

if self.replace_on_next_sync {
self.replica.replace_doc(resp.document);
self.replace_on_next_sync = false;
if self.in_first_sync {
self.first_sync_document = Some(resp.document);
self.popover = Some(Popover::ConfirmReplaceOrMerge);
} else {
self.replica.merge(resp.document);
}
};

vec![]
}
Expand Down Expand Up @@ -355,6 +362,25 @@ impl App {
}
_ => auth.handle_event(key),
},
Some(Popover::ConfirmReplaceOrMerge) => match key.code {
KeyCode::Char('r') => {
self.dismiss_popover();
self.in_first_sync = false;
if let Some(document) = self.first_sync_document.take() {
self.replica.replace_doc(document);
effects.push(Effect::SaveReplica(self.replica.clone()));
}
}
KeyCode::Char('m') => {
self.dismiss_popover();
self.in_first_sync = false;
if let Some(document) = self.first_sync_document.take() {
self.replica.merge(document);
effects.push(Effect::SaveReplica(self.replica.clone()));
}
}
_ => (),
},
}

effects
Expand Down
25 changes: 25 additions & 0 deletions beeps/src/app/popover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub enum Popover {

/// Register with the server
Authenticating(auth_form::AuthForm, AuthIntent),

/// Confirm whether or not we want to replace the full sync information or merge it.
ConfirmReplaceOrMerge,
}

/// When we're working with an Authenticating popover, what do we intend to do
Expand Down Expand Up @@ -103,6 +106,28 @@ impl Popover {
));
}
Popover::Authenticating(auth, _) => auth.render(body_area, frame),
Popover::ConfirmReplaceOrMerge => {
let popup_vert = Layout::vertical([Constraint::Percentage(50)]).flex(Flex::Center);
let popup_horiz =
Layout::horizontal([Constraint::Percentage(50)]).flex(Flex::Center);

let [popup_area] = popup_vert.areas(body_area);
let [popup_area] = popup_horiz.areas(popup_area);

let popup = Paragraph::new(
"You've successfully logged in! Do you want to replace your local data with the server's data, or merge local and remote data?\n\nPress 'r' to replace, or 'm' to merge.",
)
.block(
Block::default()
.borders(Borders::ALL)
.title("Replace or Merge?")
.padding(Padding::horizontal(1))
.border_style(Style::new().blue()),
);

frame.render_widget(Clear, popup_area);
frame.render_widget(popup, popup_area);
}
}
}
}
2 changes: 1 addition & 1 deletion beeps_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "beeps_core"
version = "0.5.1"
version = "0.5.2"
edition = "2021"

[lints]
Expand Down
2 changes: 1 addition & 1 deletion browser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "browser"
version = "0.5.1"
version = "0.5.2"
edition = "2021"

[lib]
Expand Down