Skip to content

Commit

Permalink
fix(ui): handle being logged out gracefully (#2052)
Browse files Browse the repository at this point in the history
* fix(ui): handle being logged out gracefully

* use settings.logged_in
  • Loading branch information
ellie authored May 28, 2024
1 parent 1bb63d0 commit b49c73d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
8 changes: 4 additions & 4 deletions ui/backend/Cargo.lock

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

30 changes: 16 additions & 14 deletions ui/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ use dotfiles::aliases::aliases;

#[derive(Debug, serde::Serialize)]
struct HomeInfo {
pub username: String,
pub record_count: u64,
pub history_count: u64,
pub last_sync: String,
pub username: Option<String>,
pub last_sync: Option<String>,
}

#[tauri::command]
Expand Down Expand Up @@ -88,38 +88,40 @@ async fn home_info() -> Result<HomeInfo, String> {
.await
.map_err(|e| e.to_string())?;

let client = atuin_client::api_client::Client::new(
&settings.sync_address,
settings.session_token().map_err(|e|e.to_string())?.as_str(),
settings.network_connect_timeout,
settings.network_timeout,
)
.map_err(|e| e.to_string())?;

let session_path = settings.session_path.as_str();
let last_sync = Settings::last_sync()
.map_err(|e| e.to_string())?
.format(&Rfc3339)
.map_err(|e| e.to_string())?;

let record_count = sqlite_store.len_all().await.map_err(|e| e.to_string())?;
let history_count = sqlite_store
.len_tag(HISTORY_TAG)
.await
.map_err(|e| e.to_string())?;

let info = if !PathBuf::from(session_path).exists() {
let info = if settings.logged_in() {
HomeInfo {
username: String::from(""),
last_sync: last_sync.to_string(),
username: None,
last_sync: None,
record_count,
history_count,
}
} else {
let client = atuin_client::api_client::Client::new(
&settings.sync_address,
settings.session_token().map_err(|e|e.to_string())?.as_str(),
settings.network_connect_timeout,
settings.network_timeout,
)
.map_err(|e| e.to_string())?;

let me = client.me().await.map_err(|e| e.to_string())?;

HomeInfo {
username: me.username,
last_sync: last_sync.to_string(),
username: Some(me.username),
last_sync: Some(last_sync.to_string()),
record_count,
history_count,
}
Expand Down
5 changes: 4 additions & 1 deletion ui/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ export default function Home() {
stats={[
{
name: "Last Sync",
stat: formatRelative(homeInfo.lastSyncTime, new Date()),
stat:
(homeInfo.lastSyncTime &&
formatRelative(homeInfo.lastSyncTime, new Date())) ||
"Never",
},
{
name: "Total history records",
Expand Down
2 changes: 1 addition & 1 deletion ui/src/state/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const DefaultUser: User = {
export interface HomeInfo {
historyCount: number;
recordCount: number;
lastSyncTime: Date;
lastSyncTime: Date | null;
}

export const DefaultHomeInfo: HomeInfo = {
Expand Down
11 changes: 9 additions & 2 deletions ui/src/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const useStore = create<AtuinState>()((set, get) => ({
homeInfo: {
historyCount: res.history_count,
recordCount: res.record_count,
lastSyncTime: parseISO(res.last_sync),
lastSyncTime: (res.last_sync && parseISO(res.last_sync)) || null,
},
});
})
Expand All @@ -88,7 +88,14 @@ export const useStore = create<AtuinState>()((set, get) => ({

refreshUser: async () => {
let config = await settings();
let session = await sessionToken();
let session;

try {
session = await sessionToken();
} catch (e) {
console.log("Not logged in, so not refreshing user");
return;
}
let url = config.sync_address + "/api/v0/me";

let res = await fetch(url, {
Expand Down

0 comments on commit b49c73d

Please sign in to comment.