use subspace_account::Error;
use super::{Retry, Status};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Session {
Unknown,
Usable,
Unchecked,
None,
}
pub(super) fn after(session: Session, retry: Retry, result: Result<bool, Error>) -> Session {
match (retry, result) {
(_, Ok(true)) => Session::Usable,
(_, Ok(false)) => Session::None,
(
Retry::Restore | Retry::Reconnect,
Err(Error::Offline | Error::Keychain | Error::Service),
) if session != Session::None => Session::Unchecked,
(Retry::Restore | Retry::Reconnect, Err(Error::SignInRequired | Error::InvalidReply)) => {
Session::None
}
(Retry::Restore, Err(_)) if session == Session::Unknown => Session::None,
_ => session,
}
}
pub(super) fn asks(session: Session, status: Status) -> bool {
session == Session::None && status != Status::Unavailable
}
pub(super) fn holds(session: Session, status: Status) -> bool {
matches!(session, Session::Unknown | Session::None) && status != Status::Unavailable
}
pub(crate) fn account_label(organization: &str, provider: &str) -> String {
match (organization.is_empty(), provider.is_empty()) {
(false, _) => format!("{organization} account"),
(true, false) => format!("{provider} account"),
(true, true) => "Account".into(),
}
}
pub(crate) fn status_text(
status: Status,
signed_in: bool,
provider: &str,
code: Option<&str>,
) -> Option<String> {
match status {
Status::Starting => Some(format!("Opening {provider} sign-in…")),
Status::Waiting if code.is_some() => code.map(|code| {
format!("Check that your browser shows the code {code}, then confirm it there.")
}),
Status::Waiting if signed_in => Some("Complete sign-in in your browser. Your current account stays connected until this finishes.".into()),
Status::Waiting => Some("Complete sign-in in your browser.".into()),
Status::Refreshing => Some(format!("Reconnecting to your {provider} account…")),
Status::Completing => Some(format!("Saving your {provider} sign-in securely…")),
Status::SigningOut => Some("Signing out…".into()),
Status::Failed(error) => Some(error.to_string()),
_ => None,
}
}
pub(crate) fn sign_in_label(status: Status, signed_in: bool, provider: &str) -> String {
if matches!(
status,
Status::Failed(Error::SignInRequired | Error::InvalidReply)
) {
"Sign in again".into()
} else if signed_in {
"Switch account".into()
} else if provider.is_empty() {
"Sign in".into()
} else {
format!("Sign in with {provider}")
}
}