From 15278302f3dd685fc2028eb1b1cb53a680c248a9 Mon Sep 17 00:00:00 2001 From: Darcy Ye Date: Wed, 29 Nov 2023 15:37:50 +0800 Subject: [PATCH] fix(console): fix xml file reader size calculation and add dark mode file icon (#4991) --- .../src/assets/icons/file-icon-dark.svg | 7 ++++++ .../Connection/SamlMetadataForm/utils.ts | 2 +- .../Connection/XmlFileReader/index.tsx | 23 +++++++++---------- 3 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 packages/console/src/assets/icons/file-icon-dark.svg diff --git a/packages/console/src/assets/icons/file-icon-dark.svg b/packages/console/src/assets/icons/file-icon-dark.svg new file mode 100644 index 000000000..ffb4be770 --- /dev/null +++ b/packages/console/src/assets/icons/file-icon-dark.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/packages/console/src/pages/EnterpriseSsoDetails/Connection/SamlMetadataForm/utils.ts b/packages/console/src/pages/EnterpriseSsoDetails/Connection/SamlMetadataForm/utils.ts index 5142b8bc8..b6bffdcc1 100644 --- a/packages/console/src/pages/EnterpriseSsoDetails/Connection/SamlMetadataForm/utils.ts +++ b/packages/console/src/pages/EnterpriseSsoDetails/Connection/SamlMetadataForm/utils.ts @@ -1,5 +1,5 @@ // In Bytes. -export const getXmlFileSize = (xmlContent: string): number => { +export const calculateXmlFileSize = (xmlContent: string): number => { const blob = new Blob([xmlContent], { type: 'application/xml' }); const file = new File([blob], 'identity provider metadata.xml'); return file.size; diff --git a/packages/console/src/pages/EnterpriseSsoDetails/Connection/XmlFileReader/index.tsx b/packages/console/src/pages/EnterpriseSsoDetails/Connection/XmlFileReader/index.tsx index 04f710c6c..34ddc518e 100644 --- a/packages/console/src/pages/EnterpriseSsoDetails/Connection/XmlFileReader/index.tsx +++ b/packages/console/src/pages/EnterpriseSsoDetails/Connection/XmlFileReader/index.tsx @@ -1,16 +1,18 @@ -import { conditional } from '@silverhand/essentials'; -import { useCallback, useState } from 'react'; +import { Theme } from '@logto/schemas'; +import { useCallback } from 'react'; import { useDropzone, type FileRejection } from 'react-dropzone'; import { useFormContext } from 'react-hook-form'; import Delete from '@/assets/icons/delete.svg'; +import FileIconDark from '@/assets/icons/file-icon-dark.svg'; import FileIcon from '@/assets/icons/file-icon.svg'; import UploaderIcon from '@/assets/icons/upload.svg'; import Button from '@/ds-components/Button'; import IconButton from '@/ds-components/IconButton'; +import useTheme from '@/hooks/use-theme'; import { type SamlGuideFormType } from '../../../EnterpriseSso/types'; -import { getXmlFileSize } from '../SamlMetadataForm/utils'; +import { calculateXmlFileSize } from '../SamlMetadataForm/utils'; import * as styles from './index.module.scss'; @@ -24,12 +26,7 @@ type Props = { }; function XmlFileReader({ onChange, value }: Props) { - /** - * It's ok to use 0 as a fallback file size because it will not be displayed if the value is empty. - */ - const [fileSize, setFileSize] = useState( - conditional(value && getXmlFileSize(value)) ?? 0 - ); + const theme = useTheme(); const { setError, @@ -58,7 +55,6 @@ function XmlFileReader({ onChange, value }: Props) { if (!acceptedFile) { return; } - setFileSize(acceptedFile.size); const xmlContent = await acceptedFile.text(); onChange(xmlContent); @@ -83,10 +79,13 @@ function XmlFileReader({ onChange, value }: Props) {
{value ? (
- + {theme === Theme.Dark ? : }
{xmlFileName} - {`${(fileSize / 1024).toFixed(2)} KB`} + {/* Not using `File.size` since the file content (variable `value` in this case) is stored in DB in string type */} + {`${(calculateXmlFileSize(value) / 1024).toFixed( + 2 + )} KB`}