use crate::reply::providers::{Place, ProviderRow};
use crate::ui::{Model, machine, theme};
pub mod run;
pub const WORD: &str = "login";
pub const NOT_ANSWERED: &str = "waiting to hear what this workspace can sign in to";
pub const NO_PROVIDERS: &str =
"no provider here can sign in — /config brazen declares an oauth row";
pub const SIGN_IN: &str = "sign in to this provider";
pub const SIGN_IN_HELP: &str =
"starts this provider's sign-in where it is kept; its progress stands below";
pub const OFFERS: &str = "what it offers…";
pub const OFFERS_HELP: &str = "asks this provider which models it offers, listed below";
pub const NOT_OFFERED: &str = "waiting to hear what this provider offers";
pub const OFFERS_NOTHING: &str = "this provider offers no model";
pub const ELSEWHERE: &str = "this workspace lives on another box, and the sign-in \
runs there — the credential is kept there, not \
on this one";
pub fn far(machine: &str) -> String {
format!("these providers are {machine}'s own: a sign-in keeps its credential there")
}
pub fn render(ui: &mut egui::Ui, model: &mut Model) -> bool {
if model.login.is_none() {
return false;
}
if let Some(aim) = model.aim.clone() {
ui.colored_label(
theme::INK_WEAK,
format!("on {} — {}", aim.address, aim.channel),
);
}
if model.elsewhere() {
ui.colored_label(theme::tone_ink(&crate::reply::convs::Tone::Weak), ELSEWHERE);
}
match model.catalog.clone() {
None => {
ui.label(NOT_ANSWERED);
}
Some(rows) if rows.is_empty() => {
ui.label(NO_PROVIDERS);
}
Some(rows) => {
let on = |row: &ProviderRow| Place::machine(row.on.as_ref());
let mut machines: Vec<Option<String>> = Vec::new();
for row in &rows {
let on = on(row);
if !machines.contains(&on) {
machines.push(on);
}
}
for client in machines {
theme::paint::section(ui, machine::word(client.as_deref()));
if let Some(there) = &client {
ui.colored_label(theme::INK_WEAK, far(there));
}
for row in rows.iter().filter(|row| on(row) == client) {
provider(ui, model, row);
}
}
}
}
true
}
fn provider(ui: &mut egui::Ui, model: &mut Model, row: &ProviderRow) {
ui.separator();
ui.label(Place::shown(row.on.as_ref(), &row.name));
let weak = theme::tone_ink(&crate::reply::convs::Tone::Weak);
ui.colored_label(weak, row.fact.clone());
if let Some(takes) = row.takes() {
ui.colored_label(weak, takes);
}
if let Some(why) = &row.blocked {
ui.colored_label(theme::NOTICE, why.clone());
}
ui.horizontal_wrapped(|ui| {
let start = theme::paint::button(ui, SIGN_IN, row.signable(), SIGN_IN_HELP);
crate::ui::act::tag(
&start,
&[crate::verbs::LOGIN.word, crate::verbs::LOGIN_TAIL.word],
);
if start.clicked() {
model.post_signin(&row.name);
}
let offers = theme::paint::button(ui, OFFERS, true, OFFERS_HELP);
crate::ui::act::tag(&offers, &[crate::verbs::MODELS.word]);
if offers.clicked() {
model.post_offering(&row.name);
}
});
offering(ui, model, &row.name);
run::render(ui, model, &row.name);
}
fn offering(ui: &mut egui::Ui, model: &Model, name: &str) {
if model.login.as_ref().and_then(|pane| pane.asking.as_deref()) != Some(name) {
return;
}
match model.offered.as_deref() {
None => {
ui.label(NOT_OFFERED);
}
Some([]) => {
ui.label(OFFERS_NOTHING);
}
Some(ids) => {
ui.label(ids.join(" "));
}
}
}
#[cfg(test)]
mod tests;