diff --git a/src-tauri/src/cmds/profile.rs b/src-tauri/src/cmds/profile.rs index 9278151..39f28b0 100644 --- a/src-tauri/src/cmds/profile.rs +++ b/src-tauri/src/cmds/profile.rs @@ -10,6 +10,7 @@ use crate::{ }; use std::fs::File; use std::io::Write; +use std::time::{SystemTime, UNIX_EPOCH}; use tauri::State; /// Import the profile from url @@ -38,6 +39,11 @@ pub async fn import_profile(url: String, lock: State<'_, ProfileLock>) -> Result let mut profiles = read_profiles(); let mut items = profiles.items.unwrap_or(vec![]); + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs(); + items.push(ProfileItem { name: Some(result.name), file: Some(result.file), @@ -45,6 +51,7 @@ pub async fn import_profile(url: String, lock: State<'_, ProfileLock>) -> Result url: Some(url), selected: Some(vec![]), extra: Some(result.extra), + updated: Some(now as usize), }); profiles.items = Some(items); save_profiles(&profiles) @@ -82,6 +89,11 @@ pub async fn update_profile(index: usize, lock: State<'_, ProfileLock>) -> Resul } }; + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs() as usize; + // update file let file_path = &items[index].file.as_ref().unwrap(); let file_path = app_home_dir().join("profiles").join(file_path); @@ -90,6 +102,7 @@ pub async fn update_profile(index: usize, lock: State<'_, ProfileLock>) -> Resul items[index].name = Some(result.name); items[index].extra = Some(result.extra); + items[index].updated = Some(now); profiles.items = Some(items); save_profiles(&profiles) } diff --git a/src-tauri/src/config/profiles.rs b/src-tauri/src/config/profiles.rs index 0ad49cf..e83b35a 100644 --- a/src-tauri/src/config/profiles.rs +++ b/src-tauri/src/config/profiles.rs @@ -24,6 +24,8 @@ pub struct ProfileItem { pub selected: Option>, /// user info pub extra: Option, + /// updated time + pub updated: Option, } #[derive(Default, Debug, Clone, Deserialize, Serialize)] diff --git a/src/components/profile-item.tsx b/src/components/profile-item.tsx index 5b00447..1140b62 100644 --- a/src/components/profile-item.tsx +++ b/src/components/profile-item.tsx @@ -12,6 +12,9 @@ import { import { MenuRounded } from "@mui/icons-material"; import { ProfileItem } from "../services/command"; import parseTraffic from "../utils/parse-traffic"; +import relativeTime from "dayjs/plugin/relativeTime"; + +dayjs.extend(relativeTime); const Wrapper = styled(Box)(({ theme }) => ({ width: "100%", @@ -34,11 +37,12 @@ interface Props { const ProfileItemComp: React.FC = (props) => { const { selected, itemData, onClick, onUpdate } = props; - const { name = "Profile", extra } = itemData; + const { name = "Profile", extra, updated = 0 } = itemData; const { upload = 0, download = 0, total = 0 } = extra ?? {}; const from = parseUrl(itemData.url); const expire = parseExpire(extra?.expire); const progress = Math.round(((download + upload) * 100) / (total + 0.1)); + const fromnow = updated > 0 ? dayjs(updated * 1000).fromNow() : ""; return ( = (props) => { - - {from} - + + + {from} + + + + {fromnow} + + = (props) => { justifyContent: "space-between", }} > - + {parseTraffic(upload + download)} / {parseTraffic(total)} - {expire} + {expire} diff --git a/src/services/command.ts b/src/services/command.ts index 5fcb97f..a5e9327 100644 --- a/src/services/command.ts +++ b/src/services/command.ts @@ -32,6 +32,7 @@ export interface ProfileItem { file?: string; mode?: string; url?: string; + updated?: number; selected?: { name?: string; now?: string }[]; extra?: { upload: number;