Skip to content

Commit

Permalink
检查更新 v1
Browse files Browse the repository at this point in the history
  • Loading branch information
solidSpoon committed Feb 2, 2024
1 parent 3096f0b commit 85457b3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"ag-grid-react": "^31.0.0",
"axios": "^1.5.0",
"clsx": "^2.0.0",
"compare-versions": "^6.1.0",
"drizzle-orm": "^0.29.1",
"electron-debug": "^3.2.0",
"electron-log": "^4.4.8",
Expand Down
28 changes: 13 additions & 15 deletions src/main/controllers/CheckUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import axios from 'axios';
import { app } from 'electron';
import { compareVersions } from 'compare-versions';

export interface Release {
url: string;
version: string;
content: string;
}

let cache: Release | null = null;
let cache: Release[] = [];
let cacheUpdateTime = 0;

export const checkUpdate = async (): Promise<Release | null> => {
export const checkUpdate = async (): Promise<Release[]> => {
const now = Date.now();
// 5分钟内不重复检查更新
if (cache && now - cacheUpdateTime < 5 * 60 * 1000) {
Expand All @@ -21,30 +22,27 @@ export const checkUpdate = async (): Promise<Release | null> => {

const result = await axios
.get(
'https://api.github.com/repos/solidSpoon/DashPlayer/releases/latest'
'https://api.github.com/repos/solidSpoon/DashPlayer/releases'
)
.catch((err) => {
console.error(err);
return null;
});

if (result?.status !== 200) {
return null;
return [];
}

if (result.data.tag_name === `v${currentVersion}`) {
return null;
}

cache = {
url: result.data.html_url,
version: result.data.tag_name,
content: result.data.body,
};
const releases: Release[] = result.data.map((release: any) => ({
url: release.html_url,
version: release.tag_name,
content: release.body,
}));
cache = releases
.filter(release => compareVersions(release.version, `v${currentVersion}`) > 0)
.sort((a, b) => compareVersions(b.version, a.version));
cacheUpdateTime = now;
return cache;

//
};

export const appVersion = (): string => {
Expand Down
2 changes: 1 addition & 1 deletion src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ const electronHandler = {
return (await invoke('query-progress', videoId)) as WatchProjectVideo;
},
checkUpdate: async () => {
return (await invoke('check-update')) as Release | undefined;
return (await invoke('check-update')) as Release[];
},
markWordLevel: async (word: string, familiar: boolean) => {
return (await invoke('mark-word-level', word, familiar)) as void;
Expand Down
29 changes: 13 additions & 16 deletions src/renderer/windows/player/pages/setting/CheckUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import { cn } from '../../../../../common/utils/Util';
const api = window.electron;

const CheckUpdate = () => {
const [newRelease, setNewRelease] = useState<Release | undefined>(
undefined
);
const [newRelease, setNewRelease] = useState<Release[]>([]);

const [checking, setChecking] = useState<boolean>(true);

Expand All @@ -29,29 +27,28 @@ const CheckUpdate = () => {
<Header title="检查更新" />
<ItemWrapper>
{checking && <div>检查更新中...</div>}
{!checking && !newRelease && (
{!checking && newRelease.length === 0 && (
<div className="text-lg">已经是最新版本</div>
)}
{!checking && newRelease && (
{!checking && newRelease.length > 0 && (
<div className="mr-6">
<h1 className="text-lg">
最新版本: {newRelease.version}
</h1>
<pre
className={cn(
'mt-4 border rounded-lg border-gray-300 p-2 select-text bg-white text-sm'
)}
>
{newRelease.content}
</pre>
{newRelease.map((release) => (
<pre key={release.version}>
<h2 className="text-base mt-2">
{release.version}
</h2>
<p className="text-sm mt-2">
{release.content}
</p>
</pre>
))}
</div>
)}
</ItemWrapper>
<FooterWrapper>
<SettingButton
handleSubmit={() => {
api.openUrl(
newRelease?.url ??
'https://github.com/solidSpoon/DashPlayer/releases/latest'
);
}}
Expand Down

0 comments on commit 85457b3

Please sign in to comment.