mirror of
https://github.com/System-End/hackatime-desktop.git
synced 2026-04-19 19:45:09 +00:00
fix: make autoupdater check on startup
This commit is contained in:
parent
b7516b4ccc
commit
fc9c49f7ff
2 changed files with 57 additions and 15 deletions
|
|
@ -106,17 +106,7 @@ async fn fetch_streak_with_cache(
|
|||
base_url: &str,
|
||||
access_token: &str,
|
||||
) -> Result<serde_json::Value, String> {
|
||||
let db = Database::new().await?;
|
||||
let today = chrono::Utc::now().date_naive().format("%Y-%m-%d").to_string();
|
||||
let cache_key = format!("streak:{}", today);
|
||||
|
||||
if let Ok(Some(cached_data)) = db.get_cached_data(&cache_key).await {
|
||||
push_log("debug", "backend", format!("Using cached streak data for {}", today));
|
||||
return serde_json::from_str(&cached_data)
|
||||
.map_err(|e| format!("Failed to parse cached streak data: {}", e));
|
||||
}
|
||||
|
||||
push_log("debug", "backend", format!("Fetching fresh streak data for {}", today));
|
||||
push_log("info", "backend", format!("Fetching streak data from API"));
|
||||
let response = client
|
||||
.get(&format!("{}/api/v1/authenticated/streak", base_url))
|
||||
.bearer_auth(access_token)
|
||||
|
|
@ -133,10 +123,6 @@ async fn fetch_streak_with_cache(
|
|||
.await
|
||||
.map_err(|e| format!("Failed to parse streak response: {}", e))?;
|
||||
|
||||
let data_str = serde_json::to_string(&data)
|
||||
.map_err(|e| format!("Failed to serialize streak data for caching: {}", e))?;
|
||||
db.set_cached_data(&cache_key, &data_str, 30).await.ok();
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
|
|
|
|||
56
src/App.vue
56
src/App.vue
|
|
@ -2,6 +2,8 @@
|
|||
import { ref, onMounted, onUnmounted, computed } from "vue";
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { getCurrent, onOpenUrl } from "@tauri-apps/plugin-deep-link";
|
||||
import { check } from '@tauri-apps/plugin-updater';
|
||||
import { relaunch } from '@tauri-apps/plugin-process';
|
||||
import { api } from "./api";
|
||||
import Home from "./views/Home.vue";
|
||||
import Projects from "./views/Projects.vue";
|
||||
|
|
@ -11,6 +13,18 @@ import UserProfileCard from "./components/UserProfileCard.vue";
|
|||
import CustomTitlebar from "./components/CustomTitlebar.vue";
|
||||
import WakatimeSetupModal from "./components/WakatimeSetupModal.vue";
|
||||
|
||||
if (!(window as any).__hackatimeConsoleWrapped) {
|
||||
(window as any).__hackatimeConsoleWrapped = true;
|
||||
const originalConsole = { ...console } as any;
|
||||
['debug','info','warn','error'].forEach((lvl) => {
|
||||
const orig = (originalConsole as any)[lvl] || originalConsole.log;
|
||||
(console as any)[lvl] = (...args: any[]) => {
|
||||
try { orig.apply(originalConsole, args); } catch (_) {}
|
||||
};
|
||||
});
|
||||
console.info('[CONSOLE] Console wrapper initialized - logs will be captured');
|
||||
}
|
||||
|
||||
interface AuthState {
|
||||
is_authenticated: boolean;
|
||||
access_token: string | null;
|
||||
|
|
@ -138,6 +152,9 @@ onMounted(async () => {
|
|||
await loadAuthState();
|
||||
}
|
||||
});
|
||||
|
||||
// Check for updates automatically on startup
|
||||
checkForUpdatesAndInstall();
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
|
|
@ -411,6 +428,45 @@ async function handleDirectOAuthAuth() {
|
|||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function checkForUpdatesAndInstall() {
|
||||
try {
|
||||
console.info('[AUTO-UPDATE] Checking for updates...');
|
||||
const update = await check();
|
||||
|
||||
if (update) {
|
||||
console.info(`[AUTO-UPDATE] Update available: ${update.version}`);
|
||||
console.info('[AUTO-UPDATE] Downloading and installing update...');
|
||||
|
||||
let downloaded = 0;
|
||||
let contentLength = 0;
|
||||
|
||||
await update.downloadAndInstall((event) => {
|
||||
switch (event.event) {
|
||||
case 'Started':
|
||||
contentLength = event.data.contentLength ?? 0;
|
||||
console.info(`[AUTO-UPDATE] Started downloading ${event.data.contentLength} bytes`);
|
||||
break;
|
||||
case 'Progress':
|
||||
downloaded += event.data.chunkLength;
|
||||
const percentage = contentLength > 0 ? Math.round((downloaded / contentLength) * 100) : 0;
|
||||
console.info(`[AUTO-UPDATE] Download progress: ${percentage}% (${downloaded} / ${contentLength} bytes)`);
|
||||
break;
|
||||
case 'Finished':
|
||||
console.info('[AUTO-UPDATE] Download finished');
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
console.info('[AUTO-UPDATE] Update installed successfully. Restarting app...');
|
||||
await relaunch();
|
||||
} else {
|
||||
console.info('[AUTO-UPDATE] No updates available - app is up to date');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[AUTO-UPDATE] Auto-update check failed:', error);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue