Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

client: Persist last used organization (#52505)

Neel3b66f9076e56
diff --git a/Cargo.lock b/Cargo.lock
index dd421f80592..d24b5173abb 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2972,6 +2972,7 @@ dependencies = [
"cloud_llm_client",
"collections",
"credentials_provider",
+ "db",
"derive_more",
"feature_flags",
"fs",
diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml
index d9ef5505604..1edbb3399e4 100644
--- a/crates/client/Cargo.toml
+++ b/crates/client/Cargo.toml
@@ -25,6 +25,7 @@ cloud_api_client.workspace = true
cloud_llm_client.workspace = true
collections.workspace = true
credentials_provider.workspace = true
+db.workspace = true
derive_more.workspace = true
feature_flags.workspace = true
fs.workspace = true
diff --git a/crates/client/src/user.rs b/crates/client/src/user.rs
index df0c00d8663..e9b9acf6857 100644
--- a/crates/client/src/user.rs
+++ b/crates/client/src/user.rs
@@ -9,6 +9,7 @@ use cloud_llm_client::{
EDIT_PREDICTIONS_USAGE_AMOUNT_HEADER_NAME, EDIT_PREDICTIONS_USAGE_LIMIT_HEADER_NAME, UsageLimit,
};
use collections::{HashMap, HashSet, hash_map::Entry};
+use db::kvp::KeyValueStore;
use derive_more::Deref;
use feature_flags::FeatureFlagAppExt;
use futures::{Future, StreamExt, channel::mpsc};
@@ -25,6 +26,8 @@ use std::{
use text::ReplicaId;
use util::{ResultExt, TryFutureExt as _};
+const CURRENT_ORGANIZATION_ID_KEY: &str = "current_organization_id";
+
pub type UserId = u64;
#[derive(
@@ -706,9 +709,16 @@ impl UserStore {
.is_some_and(|current| current.id == organization.id);
if !is_same_organization {
+ let organization_id = organization.id.0.to_string();
self.current_organization.replace(organization);
cx.emit(Event::OrganizationChanged);
cx.notify();
+
+ let kvp = KeyValueStore::global(cx);
+ db::write_and_log(cx, move || async move {
+ kvp.write_kvp(CURRENT_ORGANIZATION_ID_KEY.into(), organization_id)
+ .await
+ });
}
}
@@ -816,14 +826,29 @@ impl UserStore {
}
self.organizations = response.organizations.into_iter().map(Arc::new).collect();
- self.current_organization = response
- .default_organization_id
- .and_then(|default_organization_id| {
+ let persisted_org_id = KeyValueStore::global(cx)
+ .read_kvp(CURRENT_ORGANIZATION_ID_KEY)
+ .log_err()
+ .flatten()
+ .map(|id| OrganizationId(Arc::from(id)));
+
+ self.current_organization = persisted_org_id
+ .and_then(|persisted_id| {
self.organizations
.iter()
- .find(|organization| organization.id == default_organization_id)
+ .find(|org| org.id == persisted_id)
.cloned()
})
+ .or_else(|| {
+ response
+ .default_organization_id
+ .and_then(|default_organization_id| {
+ self.organizations
+ .iter()
+ .find(|organization| organization.id == default_organization_id)
+ .cloned()
+ })
+ })
.or_else(|| self.organizations.first().cloned());
self.plans_by_organization = response
.plans_by_organization