import { memo, useContext, useState } from 'react'; import type { ReactElement } from 'react'; import { toast } from 'react-toastify'; import CommentIcon from '@mui/icons-material/CommentTwoTone'; import DownloadIcon from '@mui/icons-material/GetApp'; import GitHubIcon from '@mui/icons-material/GitHub'; import MenuBookIcon from '@mui/icons-material/MenuBookTwoTone'; import { Avatar, Box, Button, Divider, Grid, Link, List, ListItem, ListItemAvatar, ListItemButton, ListItemText, Stack, Typography } from '@mui/material'; import type { SxProps, Theme } from '@mui/material/styles'; import { useRequest } from 'alova/client'; import { SectionContent, useLayoutTitle } from 'components'; import { AuthenticatedContext } from 'contexts/authentication'; import { useI18nContext } from 'i18n/i18n-react'; import { saveFile } from 'utils'; import { API, callAction } from '../../api/app'; import type { APIcall } from './types'; interface HelpLink { href: string; icon: ReactElement; label: () => string; } interface CustomSupport { img_url: string | null; html: string | null; } const DEFAULT_IMAGE_URL = 'https://emsesp.org/media/images/installer.jpeg'; const SUPPORT_BOX_STYLES: SxProps = { borderRadius: 3, border: '1px solid lightblue', justifyContent: 'space-evenly', alignItems: 'center' }; const IMAGE_STYLES: SxProps = { maxHeight: { xs: 100, md: 250 } }; const AVATAR_STYLES: SxProps = { bgcolor: '#72caf9' }; const SYSTEM_INFO_API: APIcall = { device: 'system', cmd: 'info', id: 0 }; const HelpComponent = () => { const { LL } = useI18nContext(); useLayoutTitle(LL.HELP()); const { me } = useContext(AuthenticatedContext); const [customSupport, setCustomSupport] = useState({ img_url: null, html: null }); const [imgError, setImgError] = useState(false); useRequest(callAction({ action: 'getCustomSupport' })).onSuccess((event) => { if (event?.data && Object.keys(event.data).length !== 0) { const { Support } = event.data as { Support: { img_url?: string; html?: string[] }; }; setCustomSupport({ img_url: Support.img_url || null, html: Support.html?.join('
') || null }); } }); const { send: sendAPI } = useRequest((data: APIcall) => API(data), { immediate: false }) .onSuccess((event) => { saveFile(event.data, 'system_info', '.json'); toast.info(LL.DOWNLOAD_SUCCESSFUL()); }) .onError((error) => { toast.error(String(error.error?.message || 'An error occurred')); }); const helpLinks: HelpLink[] = [ { href: 'https://emsesp.org', icon: , label: () => LL.HELP_INFORMATION_1() }, { href: 'https://discord.gg/GP9DPSgeJq', icon: , label: () => LL.HELP_INFORMATION_2() }, { href: 'https://github.com/emsesp/EMS-ESP32/issues/new/choose', icon: , label: () => LL.HELP_INFORMATION_3() } ]; const imageSrc = imgError || !customSupport.img_url ? DEFAULT_IMAGE_URL : customSupport.img_url; return ( {customSupport.html && ( } sx={{ padding: 1, mb: 2, ...SUPPORT_BOX_STYLES }} >
setImgError(true)} src={imageSrc} /> )} {me?.admin && ( {helpLinks.map(({ href, icon, label }) => ( {icon} ))} )} {LL.HELP_INFORMATION_4()}: ©  emsesp.org ); }; const Help = memo(HelpComponent); export default Help;