feat: profile item support display updated time

This commit is contained in:
GyDi 2021-12-20 01:10:06 +08:00
parent aa29e185e4
commit 9d62462a96
4 changed files with 38 additions and 6 deletions

View File

@ -10,6 +10,7 @@ use crate::{
}; };
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::time::{SystemTime, UNIX_EPOCH};
use tauri::State; use tauri::State;
/// Import the profile from url /// 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 profiles = read_profiles();
let mut items = profiles.items.unwrap_or(vec![]); let mut items = profiles.items.unwrap_or(vec![]);
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
items.push(ProfileItem { items.push(ProfileItem {
name: Some(result.name), name: Some(result.name),
file: Some(result.file), file: Some(result.file),
@ -45,6 +51,7 @@ pub async fn import_profile(url: String, lock: State<'_, ProfileLock>) -> Result
url: Some(url), url: Some(url),
selected: Some(vec![]), selected: Some(vec![]),
extra: Some(result.extra), extra: Some(result.extra),
updated: Some(now as usize),
}); });
profiles.items = Some(items); profiles.items = Some(items);
save_profiles(&profiles) 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 // update file
let file_path = &items[index].file.as_ref().unwrap(); let file_path = &items[index].file.as_ref().unwrap();
let file_path = app_home_dir().join("profiles").join(file_path); 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].name = Some(result.name);
items[index].extra = Some(result.extra); items[index].extra = Some(result.extra);
items[index].updated = Some(now);
profiles.items = Some(items); profiles.items = Some(items);
save_profiles(&profiles) save_profiles(&profiles)
} }

View File

@ -24,6 +24,8 @@ pub struct ProfileItem {
pub selected: Option<Vec<ProfileSelected>>, pub selected: Option<Vec<ProfileSelected>>,
/// user info /// user info
pub extra: Option<ProfileExtra>, pub extra: Option<ProfileExtra>,
/// updated time
pub updated: Option<usize>,
} }
#[derive(Default, Debug, Clone, Deserialize, Serialize)] #[derive(Default, Debug, Clone, Deserialize, Serialize)]

View File

@ -12,6 +12,9 @@ import {
import { MenuRounded } from "@mui/icons-material"; import { MenuRounded } from "@mui/icons-material";
import { ProfileItem } from "../services/command"; import { ProfileItem } from "../services/command";
import parseTraffic from "../utils/parse-traffic"; import parseTraffic from "../utils/parse-traffic";
import relativeTime from "dayjs/plugin/relativeTime";
dayjs.extend(relativeTime);
const Wrapper = styled(Box)(({ theme }) => ({ const Wrapper = styled(Box)(({ theme }) => ({
width: "100%", width: "100%",
@ -34,11 +37,12 @@ interface Props {
const ProfileItemComp: React.FC<Props> = (props) => { const ProfileItemComp: React.FC<Props> = (props) => {
const { selected, itemData, onClick, onUpdate } = 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 { upload = 0, download = 0, total = 0 } = extra ?? {};
const from = parseUrl(itemData.url); const from = parseUrl(itemData.url);
const expire = parseExpire(extra?.expire); const expire = parseExpire(extra?.expire);
const progress = Math.round(((download + upload) * 100) / (total + 0.1)); const progress = Math.round(((download + upload) * 100) / (total + 0.1));
const fromnow = updated > 0 ? dayjs(updated * 1000).fromNow() : "";
return ( return (
<Wrapper <Wrapper
@ -95,9 +99,21 @@ const ProfileItemComp: React.FC<Props> = (props) => {
</IconButton> </IconButton>
</Box> </Box>
<Typography noWrap title={from}> <Box display="flex" justifyContent="space-between" alignItems="center">
{from} <Typography noWrap title={`From: ${from}`}>
</Typography> {from}
</Typography>
<Typography
noWrap
flex="1 0 auto"
fontSize={14}
textAlign="right"
title="updated time"
>
{fromnow}
</Typography>
</Box>
<Box <Box
sx={{ sx={{
@ -107,10 +123,10 @@ const ProfileItemComp: React.FC<Props> = (props) => {
justifyContent: "space-between", justifyContent: "space-between",
}} }}
> >
<span> <span title="used / total">
{parseTraffic(upload + download)} / {parseTraffic(total)} {parseTraffic(upload + download)} / {parseTraffic(total)}
</span> </span>
<span>{expire}</span> <span title="expire time">{expire}</span>
</Box> </Box>
<LinearProgress variant="determinate" value={progress} color="inherit" /> <LinearProgress variant="determinate" value={progress} color="inherit" />

View File

@ -32,6 +32,7 @@ export interface ProfileItem {
file?: string; file?: string;
mode?: string; mode?: string;
url?: string; url?: string;
updated?: number;
selected?: { name?: string; now?: string }[]; selected?: { name?: string; now?: string }[];
extra?: { extra?: {
upload: number; upload: number;