refactor(cli-rs): share OAuth status and copy handling

The upsell and integration dialogs receive the same API status. Decode
pending, complete, failure and expiry in one place; each caller still
owns its timer, cancellation guard and post-connect action. Use the same
device code or fallback URL for copy, leaving the error wording local.
This commit is contained in:
Simon Klee
2026-11-18 12:11:08 +01:00
Unverified
parent 974eaa5689
commit 350035f00b
2 changed files with 60 additions and 83 deletions
@@ -1,15 +1,15 @@
//! TypeScript: `component/dialog-go-upsell.tsx`, `routes/session/index.tsx:633-758`.
use opencode_api::generated::{
ConnectionInfo, IntegrationAttempt, IntegrationAttemptID, IntegrationAttemptStatus, IntegrationID,
IntegrationMethod, IntegrationMethodID, IntegrationOauthCancelInput, IntegrationOauthConnectInput,
IntegrationOauthStatusInput, LocationQueryInput, ModelInfoStatus,
ConnectionInfo, IntegrationAttempt, IntegrationAttemptID, IntegrationID, IntegrationMethod, IntegrationMethodID,
IntegrationOauthCancelInput, IntegrationOauthConnectInput, IntegrationOauthStatusInput, LocationQueryInput,
ModelInfoStatus,
};
use opencode_local::model_pref::Model;
use opencode_store::Catalog;
use opencode_ui::dialog::{DIALOG_INSET, DialogCx, FooterAction, FooterProps, footer, layout_view};
use super::go_upsell_art;
use super::{go_upsell_art, integration};
use crate::local::Local;
use crate::prelude::*;
@@ -436,11 +436,7 @@ impl Connect {
let primary = match &attempt {
Some(attempt) => vec![
action(
if super::integration::oauth_code(&attempt.instructions).is_some() {
"copy code"
} else {
"copy link"
},
if integration::oauth_code(&attempt.instructions).is_some() { "copy code" } else { "copy link" },
"c",
Connect::copy,
),
@@ -553,42 +549,26 @@ impl Connect {
};
let client = self.local.client.clone();
self.local.app.spawn(
async move { client.integration_oauth_status(&input).await },
integration::oauth_complete(client, input),
bind!(self, |state, cx, result| {
if state.closed.get() {
return;
}
let status = match result {
Ok(output) => output.data,
Err(error) => {
state.settled.set(true);
state.fail(error.to_string());
return;
}
};
match status {
IntegrationAttemptStatus::Pending(_) => {
state.timer.set(Some(
cx.set_timeout(Duration::from_millis(500), bind!(state, |state, _| state.poll(attempt))),
));
}
IntegrationAttemptStatus::Complete(_) => {
state.settled.set(true);
state.sync(
true,
bind!(state, |state, outcome| if outcome.is_none() && !state.closed.get() {
state.local.dialogs.clear();
}),
);
}
IntegrationAttemptStatus::Failed(failed) => {
state.settled.set(true);
state.fail(failed.message);
}
_ => {
state.settled.set(true);
state.fail("Authorization expired".into());
}
if matches!(result, Ok(false)) {
state.timer.set(Some(
cx.set_timeout(Duration::from_millis(500), bind!(state, |state, _| state.poll(attempt))),
));
return;
}
state.settled.set(true);
match result {
Ok(_) => state.sync(
true,
bind!(state, |state, outcome| if outcome.is_none() && !state.closed.get() {
state.local.dialogs.clear();
}),
),
Err(error) => state.fail(error),
}
}),
);
@@ -690,12 +670,7 @@ impl Connect {
fn copy(&self) {
let Some(attempt) = self.attempt.borrow().clone() else { return };
let value = super::integration::oauth_code(&attempt.instructions).unwrap_or(attempt.url);
if crate::features::selection::put(&self.local.app, &value) {
self.local.toasts.show(ToastOptions::new(Variant::Info, "Copied to clipboard"));
return;
}
self.local.toasts.error("Could not copy to the clipboard");
integration::copy_oauth(&self.local, &attempt, "Could not copy to the clipboard");
}
}
@@ -847,6 +847,28 @@ struct Oauth {
timer: Cell<Option<TimerId>>,
}
/// A pending device flow keeps polling; an unrecognized status expires it.
pub(super) async fn oauth_complete(
client: opencode_api::client::Client,
input: IntegrationOauthStatusInput,
) -> Result<bool, String> {
match client.integration_oauth_status(&input).await.map_err(|error| error.to_string())?.data {
IntegrationAttemptStatus::Pending(_) => Ok(false),
IntegrationAttemptStatus::Complete(_) => Ok(true),
IntegrationAttemptStatus::Failed(failed) => Err(failed.message),
_ => Err("Authorization expired".into()),
}
}
pub(super) fn copy_oauth(local: &Local, attempt: &IntegrationAttempt, failure: &str) {
let value = oauth_code(&attempt.instructions).unwrap_or_else(|| attempt.url.clone());
if crate::features::selection::put(&local.app, &value) {
local.toasts.show(ToastOptions::new(Variant::Info, "Copied to clipboard"));
} else {
local.toasts.error(failure);
}
}
impl Oauth {
fn status_input(&self) -> IntegrationOauthStatusInput {
IntegrationOauthStatusInput {
@@ -902,40 +924,25 @@ impl Oauth {
let client = self.flow.local.client.clone();
let input = self.status_input();
self.flow.local.app.spawn(
async move { client.integration_oauth_status(&input).await },
oauth_complete(client, input),
bind!(self, |me, cx, result| {
if me.settled.get() {
return;
}
let status = match result {
Ok(output) => output.data,
Err(error) => {
me.settled.set(true);
return me.flow.fail(error.to_string());
}
};
match status {
IntegrationAttemptStatus::Pending(_) => {
me.timer.set(Some(cx.set_timeout(
POLL,
bind!(me, |me, _| {
me.timer.set(None);
me.poll();
}),
)));
}
IntegrationAttemptStatus::Complete(_) => {
me.settled.set(true);
connected(&me.flow, &me.integration);
}
IntegrationAttemptStatus::Failed(failed) => {
me.settled.set(true);
me.flow.fail(failed.message);
}
_ => {
me.settled.set(true);
me.flow.fail("Authorization expired".into());
}
if matches!(result, Ok(false)) {
me.timer.set(Some(cx.set_timeout(
POLL,
bind!(me, |me, _| {
me.timer.set(None);
me.poll();
}),
)));
return;
}
me.settled.set(true);
match result {
Ok(_) => connected(&me.flow, &me.integration),
Err(error) => me.flow.fail(error),
}
}),
);
@@ -1089,12 +1096,7 @@ impl Oauth {
}
fn copy(&self) {
let value = oauth_code(&self.attempt.instructions).unwrap_or(self.attempt.url.clone());
if crate::features::selection::put(&self.flow.local.app, &value) {
self.flow.local.toasts.show(ToastOptions::new(Variant::Info, "Copied to clipboard"));
return;
}
self.flow.local.toasts.error("Failed to copy to clipboard");
copy_oauth(&self.flow.local, &self.attempt, "Failed to copy to clipboard");
}
}