From 794f5522b5dbbd064f0f8f044ddb63cf1fccf46b Mon Sep 17 00:00:00 2001 From: Simone Date: Wed, 13 Mar 2024 22:54:00 +0100 Subject: [PATCH] chore: remove getFileExtension and replace it with its code where used (#19762) * chore: remove getFileExtension and replace it with its code where used * chore: create a getFileExtension util in upload with its test --- .../pages/ListView/components/TableCells/Media.tsx | 4 +++- packages/core/helper-plugin/MIGRATION_GUIDE.md | 4 ++++ packages/core/helper-plugin/src/index.ts | 1 - packages/core/helper-plugin/src/utils/getFileExtension.ts | 6 ------ .../upload/admin/src/components/AssetCard/AssetCard.jsx | 4 ++-- .../upload/admin/src/components/EditAssetDialog/index.jsx | 4 ++-- .../upload/admin/src/components/TableList/CellContent.jsx | 3 +-- .../upload/admin/src/components/TableList/PreviewCell.jsx | 4 ++-- packages/core/upload/admin/src/utils/getFileExtension.js | 3 +++ packages/core/upload/admin/src/utils/index.js | 1 + .../admin/src/utils/tests/getFileExtension.test.js} | 4 ++-- 11 files changed, 20 insertions(+), 18 deletions(-) delete mode 100644 packages/core/helper-plugin/src/utils/getFileExtension.ts create mode 100644 packages/core/upload/admin/src/utils/getFileExtension.js rename packages/core/{helper-plugin/src/utils/tests/getFileExtension.test.ts => upload/admin/src/utils/tests/getFileExtension.test.js} (88%) diff --git a/packages/core/admin/admin/src/content-manager/pages/ListView/components/TableCells/Media.tsx b/packages/core/admin/admin/src/content-manager/pages/ListView/components/TableCells/Media.tsx index be41398100..fc3388a42b 100644 --- a/packages/core/admin/admin/src/content-manager/pages/ListView/components/TableCells/Media.tsx +++ b/packages/core/admin/admin/src/content-manager/pages/ListView/components/TableCells/Media.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import { Avatar, AvatarGroup, Flex, Tooltip, Typography } from '@strapi/design-system'; -import { getFileExtension, prefixFileUrlWithBackendUrl } from '@strapi/helper-plugin'; +import { prefixFileUrlWithBackendUrl } from '@strapi/helper-plugin'; import styled from 'styled-components'; import type { Entity } from '@strapi/types'; @@ -26,6 +26,8 @@ interface MediaFile { interface MediaSingleProps extends MediaFile {} +const getFileExtension = (ext: string) => (ext && ext[0] === '.' ? ext.substring(1) : ext); + const MediaSingle = ({ url, mime, alternativeText, name, ext, formats }: MediaSingleProps) => { const fileURL = prefixFileUrlWithBackendUrl(url)!; diff --git a/packages/core/helper-plugin/MIGRATION_GUIDE.md b/packages/core/helper-plugin/MIGRATION_GUIDE.md index 26840cd89e..5ea0a94e78 100644 --- a/packages/core/helper-plugin/MIGRATION_GUIDE.md +++ b/packages/core/helper-plugin/MIGRATION_GUIDE.md @@ -437,6 +437,10 @@ This util has been removed and not replaced, use async / await with try / catch This util has been removed and not replaced. If you feel like you need this util, please open an issue on the Strapi repository to discuss your usecase. +### getFileExtension + +This util has been removed and not replaced. If you feel like you need this util, please open an issue on the Strapi repository to discuss your usecase. + ### setHexOpacity This util has been removed and not replaced, use the native CSS opacity property instead. If you feel like you need this util, please open an issue on the Strapi repository to discuss your usecase. diff --git a/packages/core/helper-plugin/src/index.ts b/packages/core/helper-plugin/src/index.ts index 625e8bbfd5..3193e7c608 100644 --- a/packages/core/helper-plugin/src/index.ts +++ b/packages/core/helper-plugin/src/index.ts @@ -48,7 +48,6 @@ export * from './hooks/useSelectionState'; export * from './utils/auth'; export * from './utils/getAPIInnerErrors'; export * from './utils/getFetchClient'; -export * from './utils/getFileExtension'; export * from './utils/getYupInnerErrors'; export * from './utils/hasPermissions'; export * from './utils/normalizeAPIError'; diff --git a/packages/core/helper-plugin/src/utils/getFileExtension.ts b/packages/core/helper-plugin/src/utils/getFileExtension.ts deleted file mode 100644 index b076509fc2..0000000000 --- a/packages/core/helper-plugin/src/utils/getFileExtension.ts +++ /dev/null @@ -1,6 +0,0 @@ -/** - * Get the file extension of value passed in. - * - * This hook is used to create URL aware of the backend. Practical to resolve assets. - */ -export const getFileExtension = (ext: string) => (ext && ext[0] === '.' ? ext.substr(1) : ext); diff --git a/packages/core/upload/admin/src/components/AssetCard/AssetCard.jsx b/packages/core/upload/admin/src/components/AssetCard/AssetCard.jsx index 1b7e4817fa..4a20ed5c55 100644 --- a/packages/core/upload/admin/src/components/AssetCard/AssetCard.jsx +++ b/packages/core/upload/admin/src/components/AssetCard/AssetCard.jsx @@ -1,10 +1,10 @@ import React from 'react'; -import { getFileExtension, prefixFileUrlWithBackendUrl } from '@strapi/helper-plugin'; +import { prefixFileUrlWithBackendUrl } from '@strapi/helper-plugin'; import PropTypes from 'prop-types'; import { AssetDefinition, AssetType } from '../../constants'; -import { createAssetUrl } from '../../utils'; +import { createAssetUrl, getFileExtension } from '../../utils'; import { AudioAssetCard } from './AudioAssetCard'; import { DocAssetCard } from './DocAssetCard'; diff --git a/packages/core/upload/admin/src/components/EditAssetDialog/index.jsx b/packages/core/upload/admin/src/components/EditAssetDialog/index.jsx index 9d5abdf99d..1d93fd458f 100644 --- a/packages/core/upload/admin/src/components/EditAssetDialog/index.jsx +++ b/packages/core/upload/admin/src/components/EditAssetDialog/index.jsx @@ -19,7 +19,7 @@ import { TextInput, VisuallyHidden, } from '@strapi/design-system'; -import { getFileExtension, pxToRem, useTracking } from '@strapi/helper-plugin'; +import { pxToRem, useTracking } from '@strapi/helper-plugin'; import { Form, Formik } from 'formik'; import isEqual from 'lodash/isEqual'; import PropTypes from 'prop-types'; @@ -30,7 +30,7 @@ import * as yup from 'yup'; import { AssetDefinition } from '../../constants'; import { useEditAsset } from '../../hooks/useEditAsset'; import { useFolderStructure } from '../../hooks/useFolderStructure'; -import { findRecursiveFolderByValue, getTrad } from '../../utils'; +import { findRecursiveFolderByValue, getTrad, getFileExtension } from '../../utils'; import formatBytes from '../../utils/formatBytes'; import { ContextInfo } from '../ContextInfo'; import SelectTree from '../SelectTree'; diff --git a/packages/core/upload/admin/src/components/TableList/CellContent.jsx b/packages/core/upload/admin/src/components/TableList/CellContent.jsx index 89e6b96f0d..3d24ba7e0c 100644 --- a/packages/core/upload/admin/src/components/TableList/CellContent.jsx +++ b/packages/core/upload/admin/src/components/TableList/CellContent.jsx @@ -1,12 +1,11 @@ import React from 'react'; import { Typography } from '@strapi/design-system'; -import { getFileExtension } from '@strapi/helper-plugin'; import parseISO from 'date-fns/parseISO'; import PropTypes from 'prop-types'; import { useIntl } from 'react-intl'; -import { formatBytes } from '../../utils'; +import { formatBytes, getFileExtension } from '../../utils'; import { PreviewCell } from './PreviewCell'; diff --git a/packages/core/upload/admin/src/components/TableList/PreviewCell.jsx b/packages/core/upload/admin/src/components/TableList/PreviewCell.jsx index 73fd50e335..ff689714c2 100644 --- a/packages/core/upload/admin/src/components/TableList/PreviewCell.jsx +++ b/packages/core/upload/admin/src/components/TableList/PreviewCell.jsx @@ -1,13 +1,13 @@ import React from 'react'; import { Avatar, Box, Icon, Initials } from '@strapi/design-system'; -import { getFileExtension, prefixFileUrlWithBackendUrl, pxToRem } from '@strapi/helper-plugin'; +import { prefixFileUrlWithBackendUrl, pxToRem } from '@strapi/helper-plugin'; import { Folder } from '@strapi/icons'; import PropTypes from 'prop-types'; import styled from 'styled-components'; import { AssetType } from '../../constants'; -import { createAssetUrl } from '../../utils'; +import { createAssetUrl, getFileExtension } from '../../utils'; import { VideoPreview } from '../AssetCard/VideoPreview'; const VideoPreviewWrapper = styled(Box)` diff --git a/packages/core/upload/admin/src/utils/getFileExtension.js b/packages/core/upload/admin/src/utils/getFileExtension.js new file mode 100644 index 0000000000..6c0c2db671 --- /dev/null +++ b/packages/core/upload/admin/src/utils/getFileExtension.js @@ -0,0 +1,3 @@ +const getFileExtension = (ext) => (ext && ext[0] === '.' ? ext.substring(1) : ext); + +export default getFileExtension; diff --git a/packages/core/upload/admin/src/utils/index.js b/packages/core/upload/admin/src/utils/index.js index 18d6f346a6..7d76e557f2 100644 --- a/packages/core/upload/admin/src/utils/index.js +++ b/packages/core/upload/admin/src/utils/index.js @@ -11,3 +11,4 @@ export { default as getFolderParents } from './getFolderParents'; export { default as getFolderURL } from './getFolderURL'; export { default as getTrad } from './getTrad'; export { default as toSingularTypes } from './toSingularTypes'; +export { default as getFileExtension } from './getFileExtension'; diff --git a/packages/core/helper-plugin/src/utils/tests/getFileExtension.test.ts b/packages/core/upload/admin/src/utils/tests/getFileExtension.test.js similarity index 88% rename from packages/core/helper-plugin/src/utils/tests/getFileExtension.test.ts rename to packages/core/upload/admin/src/utils/tests/getFileExtension.test.js index 8657ea3a8d..e770afdc92 100644 --- a/packages/core/helper-plugin/src/utils/tests/getFileExtension.test.ts +++ b/packages/core/upload/admin/src/utils/tests/getFileExtension.test.js @@ -1,6 +1,6 @@ -import { getFileExtension } from '../getFileExtension'; +import getFileExtension from '../getFileExtension'; -describe('HELPER PLUGIN | utils | getFileExtension', () => { +describe('getFileExtension', () => { it('should return undefined if ext does not exits', () => { const ext = null; const expected = null;