jan/web/hooks/useGetLatestRelease.ts
Faisal Amir b0c6779015
feat: app updater with changelog (#4631)
* feat: ui modal app updater with changelog

* chore: update action when click update now

* chore: update handler actions

* chore: fix linter
2025-02-17 12:08:08 +07:00

34 lines
971 B
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import useSWR from 'swr'
const fetchLatestRelease = async (includeBeta: boolean) => {
const res = await fetch('https://api.github.com/repos/janhq/jan/releases')
if (!res.ok) throw new Error('Failed to fetch releases')
const releases = await res.json()
// Filter stable and beta releases
const stableRelease = releases.find(
(release: { prerelease: any; draft: any }) =>
!release.prerelease && !release.draft
)
const betaRelease = releases.find(
(release: { prerelease: any }) => release.prerelease
)
return includeBeta ? (betaRelease ?? stableRelease) : stableRelease
}
export function useGetLatestRelease(includeBeta = false) {
const { data, error, mutate } = useSWR(
['latestRelease', includeBeta],
() => fetchLatestRelease(includeBeta),
{
revalidateOnFocus: false,
revalidateOnReconnect: true,
}
)
return { release: data, error, mutate }
}