mirror of
https://github.com/strapi/strapi.git
synced 2025-09-25 16:29:34 +00:00
chore(admin): cleanup and convert useReleaseNotification to TS
This commit is contained in:
parent
0608d433f0
commit
13cb9b88a5
@ -18,13 +18,9 @@ import NpsSurvey from '../NpsSurvey';
|
|||||||
import PluginsInitializer from '../PluginsInitializer';
|
import PluginsInitializer from '../PluginsInitializer';
|
||||||
import RBACProvider from '../RBACProvider';
|
import RBACProvider from '../RBACProvider';
|
||||||
|
|
||||||
import {
|
import { fetchAppInfo, fetchCurrentUserPermissions, fetchUserRoles } from './utils/api';
|
||||||
fetchAppInfo,
|
import { checkLatestStrapiVersion } from './utils/checkLatestStrapiVersion';
|
||||||
fetchCurrentUserPermissions,
|
import { fetchStrapiLatestRelease } from './utils/fetchStrapiLatestRelease';
|
||||||
fetchStrapiLatestRelease,
|
|
||||||
fetchUserRoles,
|
|
||||||
} from './utils/api';
|
|
||||||
import checkLatestStrapiVersion from './utils/checkLatestStrapiVersion';
|
|
||||||
|
|
||||||
const strapiVersion = packageJSON.version;
|
const strapiVersion = packageJSON.version;
|
||||||
|
|
||||||
|
@ -12,12 +12,8 @@ import packageJSON from '../../../../../package.json';
|
|||||||
import { ConfigurationsContext } from '../../../contexts';
|
import { ConfigurationsContext } from '../../../contexts';
|
||||||
import { Theme } from '../../Theme';
|
import { Theme } from '../../Theme';
|
||||||
import { ThemeToggleProvider } from '../../ThemeToggleProvider';
|
import { ThemeToggleProvider } from '../../ThemeToggleProvider';
|
||||||
import {
|
import { fetchAppInfo, fetchCurrentUserPermissions, fetchUserRoles } from '../utils/api';
|
||||||
fetchAppInfo,
|
import { fetchStrapiLatestRelease } from '../utils/fetchStrapiLatestRelease';
|
||||||
fetchCurrentUserPermissions,
|
|
||||||
fetchStrapiLatestRelease,
|
|
||||||
fetchUserRoles,
|
|
||||||
} from '../utils/api';
|
|
||||||
|
|
||||||
const strapiVersion = packageJSON.version;
|
const strapiVersion = packageJSON.version;
|
||||||
|
|
||||||
@ -35,12 +31,15 @@ jest.mock('@strapi/helper-plugin', () => ({
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
jest.mock('../utils/api', () => ({
|
jest.mock('../utils/api', () => ({
|
||||||
fetchStrapiLatestRelease: jest.fn(),
|
|
||||||
fetchAppInfo: jest.fn(),
|
fetchAppInfo: jest.fn(),
|
||||||
fetchCurrentUserPermissions: jest.fn(),
|
fetchCurrentUserPermissions: jest.fn(),
|
||||||
fetchUserRoles: jest.fn(),
|
fetchUserRoles: jest.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
jest.mock('../utils/fetchStrapiLatestRelease', () => ({
|
||||||
|
fetchStrapiLatestRelease: jest.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
jest.mock('../../PluginsInitializer', () => () => {
|
jest.mock('../../PluginsInitializer', () => () => {
|
||||||
return <div>PluginsInitializer</div>;
|
return <div>PluginsInitializer</div>;
|
||||||
});
|
});
|
||||||
|
@ -1,45 +1,7 @@
|
|||||||
import { getFetchClient } from '@strapi/helper-plugin';
|
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 { 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 () => {
|
const fetchAppInfo = async () => {
|
||||||
try {
|
try {
|
||||||
const { data, headers } = await get('/admin/information');
|
const { data, headers } = await get('/admin/information');
|
||||||
@ -82,4 +44,4 @@ const fetchUserRoles = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export { fetchAppInfo, fetchCurrentUserPermissions, fetchStrapiLatestRelease, fetchUserRoles };
|
export { fetchAppInfo, fetchCurrentUserPermissions, fetchUserRoles };
|
||||||
|
@ -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;
|
|
@ -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);
|
||||||
|
};
|
@ -1,7 +1,8 @@
|
|||||||
import packageJSON from '../../../../../package.json';
|
import packageJSON from '../../../../../package.json';
|
||||||
|
|
||||||
const strapiVersion = packageJSON.version;
|
const strapiVersion = packageJSON.version;
|
||||||
const fetchStrapiLatestRelease = async () => {
|
|
||||||
|
export const fetchStrapiLatestRelease = async () => {
|
||||||
try {
|
try {
|
||||||
const res = await fetch('https://api.github.com/repos/strapi/strapi/releases/latest');
|
const res = await fetch('https://api.github.com/repos/strapi/strapi/releases/latest');
|
||||||
|
|
||||||
@ -16,5 +17,3 @@ const fetchStrapiLatestRelease = async () => {
|
|||||||
return strapiVersion;
|
return strapiVersion;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default fetchStrapiLatestRelease;
|
|
@ -1,4 +1,4 @@
|
|||||||
import checkLatestStrapiVersion from '../checkLatestStrapiVersion';
|
import { checkLatestStrapiVersion } from '../checkLatestStrapiVersion';
|
||||||
|
|
||||||
describe('ADMIN | utils | checkLatestStrapiVersion', () => {
|
describe('ADMIN | utils | checkLatestStrapiVersion', () => {
|
||||||
it('should return true if the current version is lower than the latest published version', () => {
|
it('should return true if the current version is lower than the latest published version', () => {
|
@ -2,6 +2,5 @@ export { useConfigurations } from './useConfigurations';
|
|||||||
export { useContentTypes } from './useContentTypes';
|
export { useContentTypes } from './useContentTypes';
|
||||||
export { default as useMenu } from './useMenu';
|
export { default as useMenu } from './useMenu';
|
||||||
export { default as usePermissionsDataManager } from './usePermissionsDataManager';
|
export { default as usePermissionsDataManager } from './usePermissionsDataManager';
|
||||||
export { default as useReleaseNotification } from './useReleaseNotification';
|
|
||||||
export { default as useSettingsForm } from './useSettingsForm';
|
export { default as useSettingsForm } from './useSettingsForm';
|
||||||
export { default as useSettingsMenu } from './useSettingsMenu';
|
export { default as useSettingsMenu } from './useSettingsMenu';
|
||||||
|
@ -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;
|
|
@ -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;
|
|
@ -36,7 +36,6 @@ jest.mock('@strapi/helper-plugin', () => ({
|
|||||||
|
|
||||||
jest.mock('../../../hooks', () => ({
|
jest.mock('../../../hooks', () => ({
|
||||||
useMenu: jest.fn(() => ({ isLoading: true, generalSectionLinks: [], pluginsSectionLinks: [] })),
|
useMenu: jest.fn(() => ({ isLoading: true, generalSectionLinks: [], pluginsSectionLinks: [] })),
|
||||||
useReleaseNotification: jest.fn(),
|
|
||||||
useConfigurations: jest.fn(() => ({ showTutorials: false })),
|
useConfigurations: jest.fn(() => ({ showTutorials: false })),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user