woodpecker/web/src/compositions/useVersion.ts

85 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-11-04 13:20:50 +00:00
import { onMounted, ref } from 'vue';
import useAuthentication from './useAuthentication';
import useConfig from './useConfig';
type VersionInfo = {
latest: string;
next: string;
};
const version = ref<{
latest: string | undefined;
current: string;
currentShort: string;
needsUpdate: boolean;
}>();
async function fetchVersion(): Promise<VersionInfo | undefined> {
try {
const resp = await fetch('https://woodpecker-ci.org/version.json');
const json = await resp.json();
return json;
} catch (error) {
// eslint-disable-next-line no-console
console.error('Failed to fetch version info', error);
return undefined;
}
}
const isInitialised = ref(false);
export function useVersion() {
if (isInitialised.value) {
return version;
}
isInitialised.value = true;
const config = useConfig();
const current = config.version as string;
const usesNext = config.version?.startsWith('next');
const { user } = useAuthentication();
if (!user?.admin) {
version.value = {
latest: undefined,
current,
currentShort: usesNext ? 'next' : current,
needsUpdate: false,
};
return version;
}
if (current === 'dev') {
version.value = {
latest: undefined,
current,
currentShort: current,
needsUpdate: false,
};
return version;
}
onMounted(async () => {
const versionInfo = await fetchVersion();
let needsUpdate = false;
if (versionInfo) {
if (usesNext) {
needsUpdate = versionInfo.next !== current;
} else {
needsUpdate = versionInfo.latest !== current;
}
}
version.value = {
latest: usesNext ? versionInfo?.next : versionInfo?.latest,
current,
currentShort: usesNext ? 'next' : current,
needsUpdate,
};
});
return version;
}