import React, { FunctionComponent, useCallback, useEffect, useState } from 'react'; import { Box, Button, CircularProgress, Link, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Typography, } from '@material-ui/core'; import { Launch, CloudDownload } from '@material-ui/icons'; import Axios from 'axios'; import { isEqual } from 'lodash'; import { useTranslation } from 'react-i18next'; import { ProfilesEntity } from '../../../types/overview'; import { Profile, TitlesEntity } from '../../../types/profile'; import config from '../../../config'; type Props = { selectedVersion: string; selectedProfile: ProfilesEntity; }; const getTitle = (title: TitlesEntity) => { return title.title || `${title.vendor} ${title.model}`; }; const profilesData: { [key: string]: Profile } = {}; const ProfileDetails: FunctionComponent = ({ selectedVersion, selectedProfile }) => { const [profile, setProfileData] = useState(); const [working, toggleWorking] = useState(true); const { t } = useTranslation(); const getHelpKey = (type: string) => { const lc = type.toLowerCase(); if (lc.includes('sysupgrade')) { return 'sysupgrade-help'; } if (lc.includes('factory') || lc === 'trx' || lc === 'chk') { return 'factory-help'; } if (lc.includes('kernel') || lc.includes('zimage') || lc.includes('uimage')) { return 'kernel-help'; } if (lc.includes('root')) { return 'rootfs-help'; } if (lc.includes('sdcard')) { return 'sdcard-help'; } if (lc.includes('tftp')) { return 'tftp-help'; } return 'other-help'; }; const getProfileData = useCallback(async () => { let profileData = profilesData[selectedProfile.id]; toggleWorking(true); if (!profileData) { const response = await Axios.get( `${process.env.PUBLIC_URL}/data/${selectedVersion}/${selectedProfile.target}/${selectedProfile.id}.json` ); profileData = response.data; profilesData[selectedProfile.id] = profileData; } toggleWorking(false); return profileData; }, [selectedVersion, selectedProfile]); useEffect(() => { if (selectedVersion && selectedProfile) { getProfileData().then((_profileData) => { if (!isEqual(profile, _profileData)) setProfileData(_profileData); }); } }, [selectedVersion, selectedProfile, getProfileData, profile]); if (working || !profile) return ; const buildAt = new Date(profile.build_at); return ( <> {t('tr-version-build')} {t('tr-model')} {profile.titles?.map((title) => getTitle(title)).join(', ')} {t('tr-target')} {profile.target} {t('tr-version')} {profile.version_number} ({profile.version_code}) {t('tr-date')} {buildAt.toLocaleString()} Info {profile.titles && ( {profile.titles .map((title) => { const titleString = getTitle(title); const infoUrl = config.info_url .replace('{title}', encodeURI(titleString)) .replace('{target}', profile.target) .replace('{id}', profile.id) .replace('{version}', profile.version_number); return ( {/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */} {profile.titles!.length > 1 && ( {titleString} )} ); }) .reduce((prev, curr) => [ prev, , curr, ])} )}
{t('tr-downloads')} Download link Help Text {profile.images?.map((i) => { const downloadURL = `${config.image_url .replace('{target}', profile.target) .replace('{version}', profile.version_number)}/${i.name}`; return ( {t(`tr-${getHelpKey(i.type)}`)} sha256sum: {i.sha256} ); })}
); }; export default ProfileDetails;