import { useTranslation } from "react-i18next";
import { Box, Button, DialogContent, Skeleton, styled, Tab, Tabs, useTheme } from "@mui/material";
import { useAppDispatch, useAppSelector } from "../../../redux/hooks.ts";
import { useCallback, useEffect, useMemo, useState } from "react";
import DraggableDialog from "../../Dialogs/DraggableDialog.tsx";
import { closeChangeIconDialog } from "../../../redux/globalStateSlice.ts";
import { LoadingButton } from "@mui/lab";
import { loadSiteConfig } from "../../../redux/thunks/site.ts";
import AutoHeight from "../../Common/AutoHeight.tsx";
import { ConfigLoadState } from "../../../redux/siteConfigSlice.ts";
import { applyIcon } from "../../../redux/thunks/file.ts";
import { FileManagerIndex } from "../FileManager.tsx";
interface EmojiSetting {
[key: string]: string[];
}
interface TabPanelProps {
children?: React.ReactNode;
index: number;
value: number;
loading?: boolean;
}
function CustomTabPanel(props: TabPanelProps) {
const { children, value, index, ...other } = props;
return (
{value === index && children}
);
}
const StyledTab = styled(Tab)(({ theme }) => ({
minWidth: 0,
minHeight: 0,
fontSize: theme.typography.h6.fontSize,
padding: "8px 10px",
}));
const EmojiButton = styled(Button)(({ theme }) => ({
minWidth: 0,
padding: "0px 4px",
fontSize: theme.typography.h6.fontSize,
}));
const SelectorBox = styled(Box)({
display: "flex",
flexWrap: "wrap",
});
const ChangeIcon = () => {
const { t } = useTranslation();
const dispatch = useAppDispatch();
const theme = useTheme();
const [tabValue, setTabValue] = useState(0);
const [loading, setLoading] = useState(false);
const open = useAppSelector((state) => state.globalState.changeIconDialogOpen);
const targets = useAppSelector((state) => state.globalState.changeIconDialogFile);
const emojiStr = useAppSelector((state) => state.siteConfig.emojis.config.emoji_preset);
const emojiStrLoaded = useAppSelector((state) => state.siteConfig.emojis.loaded);
const emojiSetting = useMemo((): EmojiSetting => {
if (!emojiStr) return {};
try {
return JSON.parse(emojiStr) as EmojiSetting;
} catch (e) {
console.warn("failed to parse emoji setting", e);
}
return {};
}, [emojiStr]);
const handleTabChange = (_event: React.SyntheticEvent, newValue: number) => {
setTabValue(newValue);
};
useEffect(() => {
if (open && emojiStrLoaded != ConfigLoadState.Loaded) {
dispatch(loadSiteConfig("emojis"));
}
}, [open]);
const onClose = useCallback(() => {
if (!loading) {
dispatch(closeChangeIconDialog());
}
}, [dispatch, loading]);
const onAccept = useCallback(
(icon?: string) => async (e?: React.MouseEvent) => {
if (e) {
e.preventDefault();
}
if (!targets) return;
setLoading(true);
try {
await dispatch(applyIcon(FileManagerIndex.main, targets, icon));
} catch (e) {
} finally {
setLoading(false);
dispatch(closeChangeIconDialog());
}
},
[dispatch, targets, setLoading],
);
return (
{t("application:modals.resetToDefault")}
}
>
{emojiStrLoaded ? (
Object.keys(emojiSetting).map((key) => )
) : (
} />
)}
{emojiStrLoaded ? (
Object.keys(emojiSetting).map((key, index) => (
{emojiSetting[key].map((emoji) => (
{emoji}
))}
))
) : (
{[...Array(50).keys()].map(() => (
))}
)}
);
};
export default ChangeIcon;