feat: use tag as version number

This commit is contained in:
Yifei Zhang 2023-03-30 16:46:17 +00:00
parent 1b140a1ed3
commit 7783545bff
5 changed files with 18 additions and 17 deletions

View File

@ -23,7 +23,7 @@ import {
import { Avatar, PromptHints } from "./home"; import { Avatar, PromptHints } from "./home";
import Locale, { AllLangs, changeLang, getLang } from "../locales"; import Locale, { AllLangs, changeLang, getLang } from "../locales";
import { getCurrentCommitId } from "../utils"; import { getCurrentVersion } from "../utils";
import Link from "next/link"; import Link from "next/link";
import { UPDATE_URL } from "../constant"; import { UPDATE_URL } from "../constant";
import { SearchService, usePromptStore } from "../store/prompt"; import { SearchService, usePromptStore } from "../store/prompt";
@ -60,7 +60,7 @@ export function Settings(props: { closeSettings: () => void }) {
const updateStore = useUpdateStore(); const updateStore = useUpdateStore();
const [checkingUpdate, setCheckingUpdate] = useState(false); const [checkingUpdate, setCheckingUpdate] = useState(false);
const currentId = getCurrentCommitId(); const currentId = getCurrentVersion();
const remoteId = updateStore.remoteId; const remoteId = updateStore.remoteId;
const hasNewVersion = currentId !== remoteId; const hasNewVersion = currentId !== remoteId;

View File

@ -3,3 +3,4 @@ export const REPO = "ChatGPT-Next-Web";
export const REPO_URL = `https://github.com/${OWNER}/${REPO}`; export const REPO_URL = `https://github.com/${OWNER}/${REPO}`;
export const UPDATE_URL = `${REPO_URL}#%E4%BF%9D%E6%8C%81%E6%9B%B4%E6%96%B0-keep-updated`; export const UPDATE_URL = `${REPO_URL}#%E4%BF%9D%E6%8C%81%E6%9B%B4%E6%96%B0-keep-updated`;
export const FETCH_COMMIT_URL = `https://api.github.com/repos/${OWNER}/${REPO}/commits?per_page=1`; export const FETCH_COMMIT_URL = `https://api.github.com/repos/${OWNER}/${REPO}/commits?per_page=1`;
export const FETCH_TAG_URL = `https://api.github.com/repos/${OWNER}/${REPO}/tags?per_page=1`;

View File

@ -8,11 +8,11 @@ import { ACCESS_CODES, IS_IN_DOCKER } from "./api/access";
let COMMIT_ID: string | undefined; let COMMIT_ID: string | undefined;
try { try {
COMMIT_ID = process COMMIT_ID = process
.execSync("git rev-parse --short HEAD") .execSync("git describe --tags --abbrev=0")
.toString() .toString()
.trim(); .trim();
} catch (e) { } catch (e) {
console.error("No git or not from git repo.") console.error("No git or not from git repo.");
} }
export const metadata = { export const metadata = {
@ -22,13 +22,13 @@ export const metadata = {
title: "ChatGPT Next Web", title: "ChatGPT Next Web",
statusBarStyle: "black-translucent", statusBarStyle: "black-translucent",
}, },
themeColor: "#fafafa" themeColor: "#fafafa",
}; };
function Meta() { function Meta() {
const metas = { const metas = {
version: COMMIT_ID ?? "unknown", version: COMMIT_ID ?? "unknown",
access: (ACCESS_CODES.size > 0 || IS_IN_DOCKER) ? "enabled" : "disabled", access: ACCESS_CODES.size > 0 || IS_IN_DOCKER ? "enabled" : "disabled",
}; };
return ( return (

View File

@ -1,7 +1,7 @@
import { create } from "zustand"; import { create } from "zustand";
import { persist } from "zustand/middleware"; import { persist } from "zustand/middleware";
import { FETCH_COMMIT_URL } from "../constant"; import { FETCH_COMMIT_URL, FETCH_TAG_URL } from "../constant";
import { getCurrentCommitId } from "../utils"; import { getCurrentVersion } from "../utils";
export interface UpdateStore { export interface UpdateStore {
lastUpdate: number; lastUpdate: number;
@ -19,15 +19,15 @@ export const useUpdateStore = create<UpdateStore>()(
remoteId: "", remoteId: "",
async getLatestCommitId(force = false) { async getLatestCommitId(force = false) {
const overOneHour = Date.now() - get().lastUpdate > 3600 * 1000; const overTenMins = Date.now() - get().lastUpdate > 10 * 60 * 1000;
const shouldFetch = force || overOneHour; const shouldFetch = force || overTenMins;
if (!shouldFetch) { if (!shouldFetch) {
return getCurrentCommitId(); return getCurrentVersion();
} }
try { try {
const data = await (await fetch(FETCH_COMMIT_URL)).json(); const data = await (await fetch(FETCH_TAG_URL)).json();
const sha = data[0].sha as string; const sha = data[0].name as string;
const remoteId = sha.substring(0, 7); const remoteId = sha.substring(0, 7);
set(() => ({ set(() => ({
lastUpdate: Date.now(), lastUpdate: Date.now(),
@ -37,13 +37,13 @@ export const useUpdateStore = create<UpdateStore>()(
return remoteId; return remoteId;
} catch (error) { } catch (error) {
console.error("[Fetch Upstream Commit Id]", error); console.error("[Fetch Upstream Commit Id]", error);
return getCurrentCommitId(); return getCurrentVersion();
} }
}, },
}), }),
{ {
name: UPDATE_KEY, name: UPDATE_KEY,
version: 1, version: 1,
} },
) ),
); );

View File

@ -76,7 +76,7 @@ export function queryMeta(key: string, defaultValue?: string): string {
} }
let currentId: string; let currentId: string;
export function getCurrentCommitId() { export function getCurrentVersion() {
if (currentId) { if (currentId) {
return currentId; return currentId;
} }