From 13cb9b88a5420df9b681d9f56a097f4c766ef7cb Mon Sep 17 00:00:00 2001 From: Gustav Hansen Date: Wed, 4 Oct 2023 13:45:03 +0200 Subject: [PATCH] chore(admin): cleanup and convert useReleaseNotification to TS --- .../src/components/AuthenticatedApp/index.js | 10 ++--- .../AuthenticatedApp/tests/index.test.js | 13 +++--- .../components/AuthenticatedApp/utils/api.js | 40 +------------------ .../utils/checkLatestStrapiVersion.js | 11 ----- .../utils/checkLatestStrapiVersion.ts | 13 ++++++ .../utils/fetchStrapiLatestRelease.ts} | 5 +-- ...st.js => checkLatestStrapiVersion.test.ts} | 2 +- packages/core/admin/admin/src/hooks/index.js | 1 - .../src/hooks/useReleaseNotification/index.js | 31 -------------- .../utils/checkLatestStrapiVersion.js | 11 ----- .../admin/src/pages/Admin/tests/index.test.js | 1 - 11 files changed, 26 insertions(+), 112 deletions(-) delete mode 100644 packages/core/admin/admin/src/components/AuthenticatedApp/utils/checkLatestStrapiVersion.js create mode 100644 packages/core/admin/admin/src/components/AuthenticatedApp/utils/checkLatestStrapiVersion.ts rename packages/core/admin/admin/src/{hooks/useReleaseNotification/utils/api.js => components/AuthenticatedApp/utils/fetchStrapiLatestRelease.ts} (83%) rename packages/core/admin/admin/src/components/AuthenticatedApp/utils/tests/{checkLatestStrapiVersion.test.js => checkLatestStrapiVersion.test.ts} (93%) delete mode 100644 packages/core/admin/admin/src/hooks/useReleaseNotification/index.js delete mode 100644 packages/core/admin/admin/src/hooks/useReleaseNotification/utils/checkLatestStrapiVersion.js diff --git a/packages/core/admin/admin/src/components/AuthenticatedApp/index.js b/packages/core/admin/admin/src/components/AuthenticatedApp/index.js index 10c50732b7..6ac2f596d2 100644 --- a/packages/core/admin/admin/src/components/AuthenticatedApp/index.js +++ b/packages/core/admin/admin/src/components/AuthenticatedApp/index.js @@ -18,13 +18,9 @@ import NpsSurvey from '../NpsSurvey'; import PluginsInitializer from '../PluginsInitializer'; import RBACProvider from '../RBACProvider'; -import { - fetchAppInfo, - fetchCurrentUserPermissions, - fetchStrapiLatestRelease, - fetchUserRoles, -} from './utils/api'; -import checkLatestStrapiVersion from './utils/checkLatestStrapiVersion'; +import { fetchAppInfo, fetchCurrentUserPermissions, fetchUserRoles } from './utils/api'; +import { checkLatestStrapiVersion } from './utils/checkLatestStrapiVersion'; +import { fetchStrapiLatestRelease } from './utils/fetchStrapiLatestRelease'; const strapiVersion = packageJSON.version; diff --git a/packages/core/admin/admin/src/components/AuthenticatedApp/tests/index.test.js b/packages/core/admin/admin/src/components/AuthenticatedApp/tests/index.test.js index 5c30c2acd3..19b023bd35 100644 --- a/packages/core/admin/admin/src/components/AuthenticatedApp/tests/index.test.js +++ b/packages/core/admin/admin/src/components/AuthenticatedApp/tests/index.test.js @@ -12,12 +12,8 @@ import packageJSON from '../../../../../package.json'; import { ConfigurationsContext } from '../../../contexts'; import { Theme } from '../../Theme'; import { ThemeToggleProvider } from '../../ThemeToggleProvider'; -import { - fetchAppInfo, - fetchCurrentUserPermissions, - fetchStrapiLatestRelease, - fetchUserRoles, -} from '../utils/api'; +import { fetchAppInfo, fetchCurrentUserPermissions, fetchUserRoles } from '../utils/api'; +import { fetchStrapiLatestRelease } from '../utils/fetchStrapiLatestRelease'; const strapiVersion = packageJSON.version; @@ -35,12 +31,15 @@ jest.mock('@strapi/helper-plugin', () => ({ })); jest.mock('../utils/api', () => ({ - fetchStrapiLatestRelease: jest.fn(), fetchAppInfo: jest.fn(), fetchCurrentUserPermissions: jest.fn(), fetchUserRoles: jest.fn(), })); +jest.mock('../utils/fetchStrapiLatestRelease', () => ({ + fetchStrapiLatestRelease: jest.fn(), +})); + jest.mock('../../PluginsInitializer', () => () => { return
PluginsInitializer
; }); diff --git a/packages/core/admin/admin/src/components/AuthenticatedApp/utils/api.js b/packages/core/admin/admin/src/components/AuthenticatedApp/utils/api.js index 1b5845b421..ad5a2883c2 100644 --- a/packages/core/admin/admin/src/components/AuthenticatedApp/utils/api.js +++ b/packages/core/admin/admin/src/components/AuthenticatedApp/utils/api.js @@ -1,45 +1,7 @@ import { getFetchClient } from '@strapi/helper-plugin'; -import packageJSON from '../../../../../package.json'; - -import checkLatestStrapiVersion from './checkLatestStrapiVersion'; - -const strapiVersion = packageJSON.version; -const showUpdateNotif = !JSON.parse(localStorage.getItem('STRAPI_UPDATE_NOTIF')); const { get } = getFetchClient(); -const fetchStrapiLatestRelease = async (toggleNotification) => { - try { - const res = await fetch('https://api.github.com/repos/strapi/strapi/releases/latest'); - - if (!res.ok) { - throw new Error('Failed to fetch latest Strapi version.'); - } - const { tag_name } = await res.json(); - const shouldUpdateStrapi = checkLatestStrapiVersion(strapiVersion, tag_name); - - if (shouldUpdateStrapi && showUpdateNotif) { - toggleNotification({ - type: 'info', - message: { id: 'notification.version.update.message' }, - link: { - url: `https://github.com/strapi/strapi/releases/tag/${tag_name}`, - label: { - id: 'global.see-more', - }, - }, - blockTransition: true, - onClose: () => localStorage.setItem('STRAPI_UPDATE_NOTIF', true), - }); - } - - return tag_name; - } catch (err) { - // Don't throw an error - return strapiVersion; - } -}; - const fetchAppInfo = async () => { try { const { data, headers } = await get('/admin/information'); @@ -82,4 +44,4 @@ const fetchUserRoles = async () => { } }; -export { fetchAppInfo, fetchCurrentUserPermissions, fetchStrapiLatestRelease, fetchUserRoles }; +export { fetchAppInfo, fetchCurrentUserPermissions, fetchUserRoles }; diff --git a/packages/core/admin/admin/src/components/AuthenticatedApp/utils/checkLatestStrapiVersion.js b/packages/core/admin/admin/src/components/AuthenticatedApp/utils/checkLatestStrapiVersion.js deleted file mode 100644 index b1f91b0ac8..0000000000 --- a/packages/core/admin/admin/src/components/AuthenticatedApp/utils/checkLatestStrapiVersion.js +++ /dev/null @@ -1,11 +0,0 @@ -import semver from 'semver'; - -const checkLatestStrapiVersion = (currentPackageVersion, latestPublishedVersion) => { - if (!semver.valid(currentPackageVersion) || !semver.valid(latestPublishedVersion)) { - return false; - } - - return semver.lt(currentPackageVersion, latestPublishedVersion); -}; - -export default checkLatestStrapiVersion; diff --git a/packages/core/admin/admin/src/components/AuthenticatedApp/utils/checkLatestStrapiVersion.ts b/packages/core/admin/admin/src/components/AuthenticatedApp/utils/checkLatestStrapiVersion.ts new file mode 100644 index 0000000000..e8f13169c3 --- /dev/null +++ b/packages/core/admin/admin/src/components/AuthenticatedApp/utils/checkLatestStrapiVersion.ts @@ -0,0 +1,13 @@ +import lt from 'semver/functions/lt'; +import valid from 'semver/functions/valid'; + +export const checkLatestStrapiVersion = ( + currentPackageVersion: string, + latestPublishedVersion: string +): boolean => { + if (!valid(currentPackageVersion) || !valid(latestPublishedVersion)) { + return false; + } + + return lt(currentPackageVersion, latestPublishedVersion); +}; diff --git a/packages/core/admin/admin/src/hooks/useReleaseNotification/utils/api.js b/packages/core/admin/admin/src/components/AuthenticatedApp/utils/fetchStrapiLatestRelease.ts similarity index 83% rename from packages/core/admin/admin/src/hooks/useReleaseNotification/utils/api.js rename to packages/core/admin/admin/src/components/AuthenticatedApp/utils/fetchStrapiLatestRelease.ts index 43161cc5bc..3fe8edfa68 100644 --- a/packages/core/admin/admin/src/hooks/useReleaseNotification/utils/api.js +++ b/packages/core/admin/admin/src/components/AuthenticatedApp/utils/fetchStrapiLatestRelease.ts @@ -1,7 +1,8 @@ import packageJSON from '../../../../../package.json'; const strapiVersion = packageJSON.version; -const fetchStrapiLatestRelease = async () => { + +export const fetchStrapiLatestRelease = async () => { try { const res = await fetch('https://api.github.com/repos/strapi/strapi/releases/latest'); @@ -16,5 +17,3 @@ const fetchStrapiLatestRelease = async () => { return strapiVersion; } }; - -export default fetchStrapiLatestRelease; diff --git a/packages/core/admin/admin/src/components/AuthenticatedApp/utils/tests/checkLatestStrapiVersion.test.js b/packages/core/admin/admin/src/components/AuthenticatedApp/utils/tests/checkLatestStrapiVersion.test.ts similarity index 93% rename from packages/core/admin/admin/src/components/AuthenticatedApp/utils/tests/checkLatestStrapiVersion.test.js rename to packages/core/admin/admin/src/components/AuthenticatedApp/utils/tests/checkLatestStrapiVersion.test.ts index 089e0c65b0..facdc53c8a 100644 --- a/packages/core/admin/admin/src/components/AuthenticatedApp/utils/tests/checkLatestStrapiVersion.test.js +++ b/packages/core/admin/admin/src/components/AuthenticatedApp/utils/tests/checkLatestStrapiVersion.test.ts @@ -1,4 +1,4 @@ -import checkLatestStrapiVersion from '../checkLatestStrapiVersion'; +import { checkLatestStrapiVersion } from '../checkLatestStrapiVersion'; describe('ADMIN | utils | checkLatestStrapiVersion', () => { it('should return true if the current version is lower than the latest published version', () => { diff --git a/packages/core/admin/admin/src/hooks/index.js b/packages/core/admin/admin/src/hooks/index.js index 89b4933123..e9a11f3fb9 100644 --- a/packages/core/admin/admin/src/hooks/index.js +++ b/packages/core/admin/admin/src/hooks/index.js @@ -2,6 +2,5 @@ export { useConfigurations } from './useConfigurations'; export { useContentTypes } from './useContentTypes'; export { default as useMenu } from './useMenu'; export { default as usePermissionsDataManager } from './usePermissionsDataManager'; -export { default as useReleaseNotification } from './useReleaseNotification'; export { default as useSettingsForm } from './useSettingsForm'; export { default as useSettingsMenu } from './useSettingsMenu'; diff --git a/packages/core/admin/admin/src/hooks/useReleaseNotification/index.js b/packages/core/admin/admin/src/hooks/useReleaseNotification/index.js deleted file mode 100644 index bd3a0ffb81..0000000000 --- a/packages/core/admin/admin/src/hooks/useReleaseNotification/index.js +++ /dev/null @@ -1,31 +0,0 @@ -import { useEffect } from 'react'; - -import { useAppInfo, useNotification } from '@strapi/helper-plugin'; - -const showUpdateNotif = !JSON.parse(localStorage.getItem('STRAPI_UPDATE_NOTIF')); - -const useReleaseNotification = () => { - const { latestStrapiReleaseTag, shouldUpdateStrapi } = useAppInfo(); - const toggleNotification = useNotification(); - - useEffect(() => { - if (shouldUpdateStrapi && showUpdateNotif) { - toggleNotification({ - type: 'info', - message: { id: 'notification.version.update.message' }, - link: { - url: `https://github.com/strapi/strapi/releases/tag/${latestStrapiReleaseTag}`, - label: { - id: 'global.see-more', - }, - }, - blockTransition: true, - onClose: () => localStorage.setItem('STRAPI_UPDATE_NOTIF', true), - }); - } - }, [latestStrapiReleaseTag, shouldUpdateStrapi, toggleNotification]); - - return null; -}; - -export default useReleaseNotification; diff --git a/packages/core/admin/admin/src/hooks/useReleaseNotification/utils/checkLatestStrapiVersion.js b/packages/core/admin/admin/src/hooks/useReleaseNotification/utils/checkLatestStrapiVersion.js deleted file mode 100644 index b1f91b0ac8..0000000000 --- a/packages/core/admin/admin/src/hooks/useReleaseNotification/utils/checkLatestStrapiVersion.js +++ /dev/null @@ -1,11 +0,0 @@ -import semver from 'semver'; - -const checkLatestStrapiVersion = (currentPackageVersion, latestPublishedVersion) => { - if (!semver.valid(currentPackageVersion) || !semver.valid(latestPublishedVersion)) { - return false; - } - - return semver.lt(currentPackageVersion, latestPublishedVersion); -}; - -export default checkLatestStrapiVersion; diff --git a/packages/core/admin/admin/src/pages/Admin/tests/index.test.js b/packages/core/admin/admin/src/pages/Admin/tests/index.test.js index 7bab863979..8c7892a548 100644 --- a/packages/core/admin/admin/src/pages/Admin/tests/index.test.js +++ b/packages/core/admin/admin/src/pages/Admin/tests/index.test.js @@ -36,7 +36,6 @@ jest.mock('@strapi/helper-plugin', () => ({ jest.mock('../../../hooks', () => ({ useMenu: jest.fn(() => ({ isLoading: true, generalSectionLinks: [], pluginsSectionLinks: [] })), - useReleaseNotification: jest.fn(), useConfigurations: jest.fn(() => ({ showTutorials: false })), }));