chore(admin): cleanup and convert useReleaseNotification to TS

This commit is contained in:
Gustav Hansen 2023-10-04 13:45:03 +02:00
parent 0608d433f0
commit 13cb9b88a5
11 changed files with 26 additions and 112 deletions

View File

@ -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;

View File

@ -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 <div>PluginsInitializer</div>;
});

View File

@ -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 };

View File

@ -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;

View File

@ -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);
};

View File

@ -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;

View File

@ -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', () => {

View File

@ -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';

View File

@ -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;

View File

@ -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;

View File

@ -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 })),
}));