mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
refactor: Simplify version display by removing API endpoint and using constant
This commit is contained in:
parent
612b00179b
commit
5e3e6b3edd
2 changed files with 2 additions and 60 deletions
|
|
@ -1,31 +1,8 @@
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { Text } from '@umami/react-zen';
|
import { Text } from '@umami/react-zen';
|
||||||
import { useEffect, useState } from 'react';
|
import { CURRENT_VERSION } from '@/lib/constants';
|
||||||
|
|
||||||
export function VersionSetting() {
|
export function VersionSetting() {
|
||||||
const [version, setVersion] = useState<string>('');
|
return <Text>{CURRENT_VERSION}</Text>;
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const fetchVersion = async () => {
|
|
||||||
try {
|
|
||||||
const response = await fetch('/api/version');
|
|
||||||
const data = await response.json();
|
|
||||||
setVersion(data.version || 'unknown');
|
|
||||||
} catch (error) {
|
|
||||||
setVersion('unknown');
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
fetchVersion();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
if (loading) {
|
|
||||||
return <Text>Loading...</Text>;
|
|
||||||
}
|
|
||||||
|
|
||||||
return <Text>{version}</Text>;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
import { readFile } from 'fs/promises';
|
|
||||||
import { join } from 'path';
|
|
||||||
import { parseRequest } from '@/lib/request';
|
|
||||||
import { json } from '@/lib/response';
|
|
||||||
|
|
||||||
let cachedVersion: string | null = null;
|
|
||||||
|
|
||||||
async function getVersion(): Promise<string> {
|
|
||||||
if (cachedVersion) {
|
|
||||||
return cachedVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const packageJsonPath = join(process.cwd(), 'package.json');
|
|
||||||
const data = await readFile(packageJsonPath, 'utf-8');
|
|
||||||
const packageJson = JSON.parse(data);
|
|
||||||
cachedVersion = packageJson.version || 'unknown';
|
|
||||||
} catch (error) {
|
|
||||||
cachedVersion = 'unknown';
|
|
||||||
}
|
|
||||||
|
|
||||||
return cachedVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
const { error } = await parseRequest(request, null, { skipAuth: true });
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
return error();
|
|
||||||
}
|
|
||||||
|
|
||||||
const version = await getVersion();
|
|
||||||
|
|
||||||
return json({ version });
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue