-
-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(console): implement custom ui assets upload component (#6217)
- Loading branch information
1 parent
6963192
commit 17d7be3
Showing
21 changed files
with
471 additions
and
69 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions
62
packages/console/src/components/CustomUiAssetsUploader/index.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
@use '@/scss/underscore' as _; | ||
|
||
.placeholder { | ||
display: flex; | ||
align-items: center; | ||
padding: _.unit(5) _.unit(5) _.unit(5) _.unit(4); | ||
border: 1px solid var(--color-border); | ||
border-radius: 12px; | ||
gap: _.unit(4); | ||
position: relative; | ||
overflow: hidden; | ||
|
||
.main { | ||
display: flex; | ||
flex-direction: column; | ||
gap: _.unit(0.5); | ||
flex: 1; | ||
|
||
.name { | ||
font: var(--font-label-2); | ||
color: var(--color-text-primary); | ||
} | ||
|
||
.secondaryInfo { | ||
display: flex; | ||
align-items: center; | ||
gap: _.unit(3); | ||
} | ||
|
||
.info { | ||
font: var(--font-body-3); | ||
color: var(--color-text-secondary); | ||
} | ||
|
||
.error { | ||
font: var(--font-body-3); | ||
color: var(--color-error); | ||
} | ||
} | ||
|
||
.icon { | ||
width: 40px; | ||
height: 40px; | ||
} | ||
|
||
// display a fake progress bar on the bottom with animations | ||
.progressBar { | ||
position: absolute; | ||
bottom: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 4px; | ||
background-color: var(--color-primary); | ||
transform: scaleX(0); | ||
transform-origin: left; | ||
transition: transform 0.3s; | ||
} | ||
|
||
&.hasError { | ||
border-color: var(--color-error); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
packages/console/src/components/CustomUiAssetsUploader/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { type CustomUiAssets, maxUploadFileSize, type AllowedUploadMimeType } from '@logto/schemas'; | ||
import { format } from 'date-fns/fp'; | ||
import { useCallback, useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import DeleteIcon from '@/assets/icons/delete.svg'; | ||
import IconButton from '@/ds-components/IconButton'; | ||
import FileUploader from '@/ds-components/Uploader/FileUploader'; | ||
import { formatBytes } from '@/utils/uploader'; | ||
|
||
import FileIcon from '../FileIcon'; | ||
|
||
import * as styles from './index.module.scss'; | ||
|
||
type Props = { | ||
readonly value?: CustomUiAssets; | ||
readonly onChange: (value: CustomUiAssets) => void; | ||
}; | ||
|
||
const allowedMimeTypes: AllowedUploadMimeType[] = ['application/zip']; | ||
|
||
function CustomUiAssetsUploader({ value, onChange }: Props) { | ||
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); | ||
const [file, setFile] = useState<File>(); | ||
const [error, setError] = useState<string>(); | ||
const showUploader = !value?.id && !file && !error; | ||
|
||
const onComplete = useCallback( | ||
(id: string) => { | ||
setFile(undefined); | ||
onChange({ id, createdAt: Date.now() }); | ||
}, | ||
[onChange] | ||
); | ||
|
||
const onErrorChange = useCallback( | ||
(errorMessage?: string, files?: File[]) => { | ||
if (errorMessage) { | ||
setError(errorMessage); | ||
} | ||
if (files?.length) { | ||
setFile(files[0]); | ||
} | ||
}, | ||
[setError, setFile] | ||
); | ||
|
||
if (showUploader) { | ||
return ( | ||
<FileUploader<{ customUiAssetId: string }> | ||
allowedMimeTypes={allowedMimeTypes} | ||
maxSize={maxUploadFileSize} | ||
uploadUrl="api/sign-in-exp/default/custom-ui-assets" | ||
onCompleted={({ customUiAssetId }) => { | ||
onComplete(customUiAssetId); | ||
}} | ||
onUploadErrorChange={onErrorChange} | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<div className={styles.placeholder}> | ||
<FileIcon /> | ||
<div className={styles.main}> | ||
<div className={styles.name}>{file?.name ?? t('sign_in_exp.custom_ui.title')}</div> | ||
<div className={styles.secondaryInfo}> | ||
{!!value?.createdAt && ( | ||
<span className={styles.info}>{format('yyyy/MM/dd HH:mm')(value.createdAt)}</span> | ||
)} | ||
{file && <span className={styles.info}>{formatBytes(file.size)}</span>} | ||
{error && <span className={styles.error}>{error}</span>} | ||
</div> | ||
</div> | ||
<IconButton | ||
onClick={() => { | ||
setFile(undefined); | ||
setError(undefined); | ||
onChange({ id: '', createdAt: 0 }); | ||
}} | ||
> | ||
<DeleteIcon /> | ||
</IconButton> | ||
{file && <div className={styles.progressBar} />} | ||
</div> | ||
); | ||
} | ||
|
||
export default CustomUiAssetsUploader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Theme } from '@logto/schemas'; | ||
import { type ReactNode } from 'react'; | ||
|
||
import FileIconDark from '@/assets/icons/file-icon-dark.svg'; | ||
import FileIconLight from '@/assets/icons/file-icon.svg'; | ||
import useTheme from '@/hooks/use-theme'; | ||
|
||
const themeToRoleIcon = Object.freeze({ | ||
[Theme.Light]: <FileIconLight />, | ||
[Theme.Dark]: <FileIconDark />, | ||
} satisfies Record<Theme, ReactNode>); | ||
|
||
/** Render a role icon according to the current theme. */ | ||
const FileIcon = () => { | ||
const theme = useTheme(); | ||
|
||
return themeToRoleIcon[theme]; | ||
}; | ||
|
||
export default FileIcon; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.