-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: notify about new version (#1846)
- Loading branch information
1 parent
acda6c6
commit 3d39c71
Showing
16 changed files
with
178 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
/tmp | ||
/out-tsc | ||
.env | ||
/public/version.json | ||
|
||
# Sentry Config File | ||
.env.sentry-build-plugin | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/clients/api/queries/getLatestAppVersion/__tests__/index.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Vi from 'vitest'; | ||
|
||
import { PUBLIC_VERSION_FILE_URL, getLatestAppVersion } from '..'; | ||
|
||
describe('getLatestAppVersion', () => { | ||
test('returns the latest app version on success', async () => { | ||
const fakeVersion = '9.9.9'; | ||
|
||
(fetch as Vi.Mock).mockImplementationOnce(() => ({ | ||
json: () => ({ | ||
version: fakeVersion, | ||
}), | ||
})); | ||
|
||
const response = await getLatestAppVersion(); | ||
|
||
expect(fetch).toHaveBeenCalledTimes(1); | ||
expect(fetch).toHaveBeenCalledWith(PUBLIC_VERSION_FILE_URL); | ||
expect(response).toEqual({ | ||
version: fakeVersion, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type GetLatestAppVersionOutput = { | ||
version?: string; | ||
}; | ||
|
||
export const PUBLIC_VERSION_FILE_URL = `${window.location.origin}/version.json`; | ||
|
||
export const getLatestAppVersion = async (): Promise<GetLatestAppVersionOutput> => { | ||
const data = await fetch(PUBLIC_VERSION_FILE_URL); | ||
const { version } = await data.json(); | ||
|
||
return { | ||
version, | ||
}; | ||
}; |
27 changes: 27 additions & 0 deletions
27
src/clients/api/queries/getLatestAppVersion/useGetLatestAppVersion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { QueryObserverOptions, useQuery } from 'react-query'; | ||
|
||
import { | ||
GetLatestAppVersionOutput, | ||
getLatestAppVersion, | ||
} from 'clients/api/queries/getLatestAppVersion'; | ||
import FunctionKey from 'constants/functionKey'; | ||
|
||
const REFETCH_INTERVAL_MS = 1000 * 60 * 60; // One hour in milliseconds | ||
|
||
type Options = QueryObserverOptions< | ||
GetLatestAppVersionOutput, | ||
Error, | ||
GetLatestAppVersionOutput, | ||
GetLatestAppVersionOutput, | ||
FunctionKey.GET_LATEST_APP_VERSION | ||
>; | ||
|
||
const useGetLatestAppVersion = (options?: Options) => | ||
useQuery(FunctionKey.GET_LATEST_APP_VERSION, getLatestAppVersion, { | ||
staleTime: 0, | ||
cacheTime: 0, | ||
refetchInterval: REFETCH_INTERVAL_MS, | ||
...options, | ||
}); | ||
|
||
export default useGetLatestAppVersion; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { compareVersions } from 'compare-versions'; | ||
import { displayNotification } from 'packages/notifications'; | ||
import { useTranslation } from 'packages/translations'; | ||
import React, { useEffect } from 'react'; | ||
|
||
import { useGetLatestAppVersion } from 'clients/api'; | ||
import { version as APP_VERSION } from 'constants/version'; | ||
|
||
export const AppVersionChecker: React.FC = () => { | ||
const { t } = useTranslation(); | ||
const { data } = useGetLatestAppVersion(); | ||
const latestAppVersion = data?.version; | ||
|
||
useEffect(() => { | ||
if (latestAppVersion && compareVersions(latestAppVersion, APP_VERSION)) { | ||
displayNotification({ | ||
description: t('appVersionChecker.newVersionMessage'), | ||
autoClose: false, | ||
}); | ||
} | ||
}, [latestAppVersion, t]); | ||
|
||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env tsx | ||
import * as path from 'path'; | ||
import { searchForWorkspaceRoot } from 'vite'; | ||
|
||
import { version } from 'constants/version'; | ||
import writeFile from 'utilities/writeFile'; | ||
|
||
const generatePublicVersionFile = async () => { | ||
const content = JSON.stringify({ | ||
version, | ||
}); | ||
|
||
console.log(searchForWorkspaceRoot(__dirname)); | ||
|
||
// Generate file | ||
const outputPath = path.join(searchForWorkspaceRoot(__dirname), './public/version.json'); | ||
|
||
writeFile({ | ||
outputPath, | ||
content, | ||
}); | ||
|
||
return outputPath; | ||
}; | ||
|
||
console.log('Generating public version file...'); | ||
|
||
generatePublicVersionFile() | ||
.then(outputPath => console.log(`Finished generating public version file at: ${outputPath}`)) | ||
.catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters