Use AwaitableComponent for "CreateThemeDialog"
This commit is contained in:
@@ -10,7 +10,7 @@ import { useTranslation } from 'react-i18next';
|
|||||||
import Stack from '@mui/material/Stack';
|
import Stack from '@mui/material/Stack';
|
||||||
import Button from '@mui/material/Button';
|
import Button from '@mui/material/Button';
|
||||||
import AddCircleIcon from '@mui/icons-material/AddCircle';
|
import AddCircleIcon from '@mui/icons-material/AddCircle';
|
||||||
import { bindDialog, bindTrigger, usePopupState } from 'material-ui-popup-state/hooks';
|
import { AwaitableComponent } from 'awaitable-component';
|
||||||
import { CustomTooltip } from '@/base/components/CustomTooltip.tsx';
|
import { CustomTooltip } from '@/base/components/CustomTooltip.tsx';
|
||||||
import { ThemeCreationDialog } from '@/features/theme/components/CreateThemeDialog.tsx';
|
import { ThemeCreationDialog } from '@/features/theme/components/CreateThemeDialog.tsx';
|
||||||
import { TypographyMaxLines } from '@/base/components/texts/TypographyMaxLines.tsx';
|
import { TypographyMaxLines } from '@/base/components/texts/TypographyMaxLines.tsx';
|
||||||
@@ -18,10 +18,7 @@ import { TypographyMaxLines } from '@/base/components/texts/TypographyMaxLines.t
|
|||||||
export const CreateThemeButton = () => {
|
export const CreateThemeButton = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
const popupState = usePopupState({ variant: 'popover', popupId: 'theme-creation-dialog' });
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
|
||||||
<Stack sx={{ alignItems: 'center', gap: 1, minWidth: '150px', maxWidth: '150px' }}>
|
<Stack sx={{ alignItems: 'center', gap: 1, minWidth: '150px', maxWidth: '150px' }}>
|
||||||
<Button
|
<Button
|
||||||
sx={{
|
sx={{
|
||||||
@@ -30,7 +27,9 @@ export const CreateThemeButton = () => {
|
|||||||
}}
|
}}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
size="large"
|
size="large"
|
||||||
{...bindTrigger(popupState)}
|
onClick={() => {
|
||||||
|
AwaitableComponent.show(ThemeCreationDialog, { mode: 'create' });
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<AddCircleIcon fontSize="large" />
|
<AddCircleIcon fontSize="large" />
|
||||||
</Button>
|
</Button>
|
||||||
@@ -40,7 +39,5 @@ export const CreateThemeButton = () => {
|
|||||||
</TypographyMaxLines>
|
</TypographyMaxLines>
|
||||||
</CustomTooltip>
|
</CustomTooltip>
|
||||||
</Stack>
|
</Stack>
|
||||||
{popupState.isOpen && <ThemeCreationDialog bindDialogProps={bindDialog(popupState)} mode="create" />}
|
|
||||||
</>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import { useState } from 'react';
|
|||||||
import Typography from '@mui/material/Typography';
|
import Typography from '@mui/material/Typography';
|
||||||
import Stack from '@mui/material/Stack';
|
import Stack from '@mui/material/Stack';
|
||||||
import Button from '@mui/material/Button';
|
import Button from '@mui/material/Button';
|
||||||
import { bindDialog } from 'material-ui-popup-state/hooks';
|
|
||||||
import Dialog from '@mui/material/Dialog';
|
import Dialog from '@mui/material/Dialog';
|
||||||
import DialogTitle from '@mui/material/DialogTitle';
|
import DialogTitle from '@mui/material/DialogTitle';
|
||||||
import DialogContent from '@mui/material/DialogContent';
|
import DialogContent from '@mui/material/DialogContent';
|
||||||
@@ -19,6 +18,7 @@ import DialogActions from '@mui/material/DialogActions';
|
|||||||
import TextField from '@mui/material/TextField';
|
import TextField from '@mui/material/TextField';
|
||||||
import Link from '@mui/material/Link';
|
import Link from '@mui/material/Link';
|
||||||
import { jsonrepair } from 'jsonrepair';
|
import { jsonrepair } from 'jsonrepair';
|
||||||
|
import { AwaitableComponentProps } from 'awaitable-component';
|
||||||
import { getErrorMessage, jsonSaveParse } from '@/lib/HelperFunctions.ts';
|
import { getErrorMessage, jsonSaveParse } from '@/lib/HelperFunctions.ts';
|
||||||
import { AppTheme, isThemeNameUnique } from '@/features/theme/services/AppThemes.ts';
|
import { AppTheme, isThemeNameUnique } from '@/features/theme/services/AppThemes.ts';
|
||||||
import {
|
import {
|
||||||
@@ -91,11 +91,13 @@ const dialogModeToTranslationKey: Record<
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const ThemeCreationDialog = ({
|
export const ThemeCreationDialog = ({
|
||||||
bindDialogProps,
|
|
||||||
mode,
|
mode,
|
||||||
appTheme = baseCustomTheme,
|
appTheme = baseCustomTheme,
|
||||||
}: {
|
isVisible,
|
||||||
bindDialogProps: ReturnType<typeof bindDialog>;
|
onExitComplete,
|
||||||
|
onSubmit,
|
||||||
|
onDismiss,
|
||||||
|
}: AwaitableComponentProps<void> & {
|
||||||
mode: DialogMode;
|
mode: DialogMode;
|
||||||
appTheme?: AppTheme;
|
appTheme?: AppTheme;
|
||||||
}) => {
|
}) => {
|
||||||
@@ -117,7 +119,7 @@ export const ThemeCreationDialog = ({
|
|||||||
const [didThemeChange, setDidThemeChange] = useState(mode === 'create');
|
const [didThemeChange, setDidThemeChange] = useState(mode === 'create');
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog {...bindDialogProps} fullWidth maxWidth="md">
|
<Dialog open={isVisible} onTransitionExited={onExitComplete} fullWidth maxWidth="md">
|
||||||
<DialogTitle>{t(dialogModeToTranslationKey[mode].title)}</DialogTitle>
|
<DialogTitle>{t(dialogModeToTranslationKey[mode].title)}</DialogTitle>
|
||||||
<DialogContent>
|
<DialogContent>
|
||||||
{loading && <LoadingPlaceholder />}
|
{loading && <LoadingPlaceholder />}
|
||||||
@@ -212,12 +214,12 @@ export const ThemeCreationDialog = ({
|
|||||||
)}
|
)}
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
<DialogActions>
|
<DialogActions>
|
||||||
<Button autoFocus onClick={bindDialogProps.onClose} color="primary">
|
<Button autoFocus onClick={() => onDismiss()} color="primary">
|
||||||
{t('global.button.cancel')}
|
{t('global.button.cancel')}
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
disabled={invalidTheme || invalidName || isCreating || !didThemeChange || !theme.id.length}
|
disabled={invalidTheme || invalidName || isCreating || !didThemeChange || !theme.id.length}
|
||||||
onClick={(e) => {
|
onClick={() => {
|
||||||
makeToast(t(dialogModeToTranslationKey[mode].action, { theme: theme.getName() }), 'info');
|
makeToast(t(dialogModeToTranslationKey[mode].action, { theme: theme.getName() }), 'info');
|
||||||
|
|
||||||
setIsCreating(true);
|
setIsCreating(true);
|
||||||
@@ -227,7 +229,7 @@ export const ThemeCreationDialog = ({
|
|||||||
t(dialogModeToTranslationKey[mode].success, { theme: theme.getName() }),
|
t(dialogModeToTranslationKey[mode].success, { theme: theme.getName() }),
|
||||||
'success',
|
'success',
|
||||||
);
|
);
|
||||||
bindDialogProps.onClose(e);
|
onSubmit();
|
||||||
})
|
})
|
||||||
.catch((updateError) =>
|
.catch((updateError) =>
|
||||||
makeToast(
|
makeToast(
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import CardActionArea from '@mui/material/CardActionArea';
|
|||||||
import DeleteIcon from '@mui/icons-material/Delete';
|
import DeleteIcon from '@mui/icons-material/Delete';
|
||||||
import IconButton from '@mui/material/IconButton';
|
import IconButton from '@mui/material/IconButton';
|
||||||
import EditIcon from '@mui/icons-material/Edit';
|
import EditIcon from '@mui/icons-material/Edit';
|
||||||
import { bindDialog, usePopupState } from 'material-ui-popup-state/hooks';
|
import { AwaitableComponent } from 'awaitable-component';
|
||||||
import { CustomTooltip } from '@/base/components/CustomTooltip.tsx';
|
import { CustomTooltip } from '@/base/components/CustomTooltip.tsx';
|
||||||
import { useAppThemeContext } from '@/features/theme/AppThemeContext.tsx';
|
import { useAppThemeContext } from '@/features/theme/AppThemeContext.tsx';
|
||||||
import { AppTheme } from '@/features/theme/services/AppThemes.ts';
|
import { AppTheme } from '@/features/theme/services/AppThemes.ts';
|
||||||
@@ -40,8 +40,6 @@ export const ThemePreview = ({ appTheme, onDelete }: { appTheme: AppTheme; onDel
|
|||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const { themeMode, setAppTheme, appTheme: activeAppTheme, shouldUsePureBlackMode } = useAppThemeContext();
|
const { themeMode, setAppTheme, appTheme: activeAppTheme, shouldUsePureBlackMode } = useAppThemeContext();
|
||||||
|
|
||||||
const popupState = usePopupState({ variant: 'popover', popupId: `theme-edit-dialog-${appTheme.id}` });
|
|
||||||
|
|
||||||
const isSelected = appTheme.id === activeAppTheme;
|
const isSelected = appTheme.id === activeAppTheme;
|
||||||
|
|
||||||
const muiTheme = useMemo(
|
const muiTheme = useMemo(
|
||||||
@@ -50,7 +48,6 @@ export const ThemePreview = ({ appTheme, onDelete }: { appTheme: AppTheme; onDel
|
|||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
|
||||||
<Stack
|
<Stack
|
||||||
sx={{
|
sx={{
|
||||||
justifyContent: 'center',
|
justifyContent: 'center',
|
||||||
@@ -81,9 +78,7 @@ export const ThemePreview = ({ appTheme, onDelete }: { appTheme: AppTheme; onDel
|
|||||||
pointerEvents: 'none',
|
pointerEvents: 'none',
|
||||||
borderRadius: 1,
|
borderRadius: 1,
|
||||||
...theme.applyStyles('dark', {
|
...theme.applyStyles('dark', {
|
||||||
borderColor: isSelected
|
borderColor: isSelected ? muiTheme.palette.primary.light : muiTheme.palette.grey['900'],
|
||||||
? muiTheme.palette.primary.light
|
|
||||||
: muiTheme.palette.grey['900'],
|
|
||||||
}),
|
}),
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@@ -165,7 +160,11 @@ export const ThemePreview = ({ appTheme, onDelete }: { appTheme: AppTheme; onDel
|
|||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
popupState.open();
|
|
||||||
|
AwaitableComponent.show(ThemeCreationDialog, {
|
||||||
|
mode: 'edit',
|
||||||
|
appTheme,
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
component="div"
|
component="div"
|
||||||
size="small"
|
size="small"
|
||||||
@@ -234,7 +233,5 @@ export const ThemePreview = ({ appTheme, onDelete }: { appTheme: AppTheme; onDel
|
|||||||
<TypographyMaxLines sx={{ maxWidth: '100%' }}>{getName()}</TypographyMaxLines>
|
<TypographyMaxLines sx={{ maxWidth: '100%' }}>{getName()}</TypographyMaxLines>
|
||||||
</CustomTooltip>
|
</CustomTooltip>
|
||||||
</Stack>
|
</Stack>
|
||||||
<ThemeCreationDialog bindDialogProps={bindDialog(popupState)} mode="edit" appTheme={appTheme} />
|
|
||||||
</>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user