|
| 1 | +import type { FC } from 'react'; |
| 2 | +import { useCallback } from 'react'; |
| 3 | +import type { Release } from '@octokit/webhooks-types'; |
| 4 | +import moment from 'moment/moment'; |
| 5 | +import { Badge, Divider, List, Text } from 'react-native-paper'; |
| 6 | +import useDtuState from '@/hooks/useDtuState'; |
| 7 | +import { Linking, Text as RNText, View } from 'react-native'; |
| 8 | +// import { useNavigation } from '@react-navigation/native'; |
| 9 | +// import type { NavigationProp, ParamListBase } from '@react-navigation/native'; |
| 10 | +import { useTranslation } from 'react-i18next'; |
| 11 | + |
| 12 | +import type { RenderRules } from 'react-native-markdown-display'; |
| 13 | +import Markdown from 'react-native-markdown-display'; |
| 14 | +import SettingsSurface, { |
| 15 | + settingsSurfaceBorderRadius, |
| 16 | +} from '@/components/styled/SettingsSurface'; |
| 17 | + |
| 18 | +export interface FirmwareListItemProps { |
| 19 | + release: Release; |
| 20 | + latestReleaseTag?: string; |
| 21 | +} |
| 22 | + |
| 23 | +const rules: RenderRules = { |
| 24 | + link: (node, children) => <RNText key={node.key}>{children}</RNText>, |
| 25 | +}; |
| 26 | + |
| 27 | +const FirmwareListItem: FC<FirmwareListItemProps> = ({ |
| 28 | + release, |
| 29 | + latestReleaseTag, |
| 30 | +}) => { |
| 31 | + const { t } = useTranslation(); |
| 32 | + const currentRelease = useDtuState(state => state?.systemStatus?.git_hash); |
| 33 | + // const navigation = useNavigation() as NavigationProp<ParamListBase>; |
| 34 | + |
| 35 | + const handleOpenGithub = useCallback(async () => { |
| 36 | + const url = release.html_url; |
| 37 | + |
| 38 | + if (await Linking.canOpenURL(url)) { |
| 39 | + await Linking.openURL(url); |
| 40 | + } |
| 41 | + }, [release.html_url]); |
| 42 | + |
| 43 | + /*const handleInstallFirmware = useCallback(() => { |
| 44 | +
|
| 45 | + }, []);*/ |
| 46 | + |
| 47 | + return ( |
| 48 | + <List.Accordion |
| 49 | + key={`firmware-${release.id}`} |
| 50 | + title={ |
| 51 | + <View style={{ flexDirection: 'row', gap: 8 }}> |
| 52 | + <Text variant="titleMedium" numberOfLines={1}> |
| 53 | + {release.name} |
| 54 | + </Text> |
| 55 | + {release.tag_name === latestReleaseTag ? ( |
| 56 | + <Badge style={{ alignSelf: 'center' }}> |
| 57 | + {t('firmwares.latestFirmware')} |
| 58 | + </Badge> |
| 59 | + ) : release.tag_name === currentRelease ? ( |
| 60 | + <Badge style={{ alignSelf: 'center' }}> |
| 61 | + {t('firmwares.installedFirmware')} |
| 62 | + </Badge> |
| 63 | + ) : null} |
| 64 | + </View> |
| 65 | + } |
| 66 | + description={moment(release.published_at).format('lll')} |
| 67 | + > |
| 68 | + <SettingsSurface style={{ marginHorizontal: 8, flex: 1 }}> |
| 69 | + <View style={{ padding: 8, flex: 1 }}> |
| 70 | + <Markdown |
| 71 | + style={{ link: { textDecorationLine: 'none' } }} |
| 72 | + rules={rules} |
| 73 | + > |
| 74 | + {release.body} |
| 75 | + </Markdown> |
| 76 | + </View> |
| 77 | + <Divider /> |
| 78 | + <List.Item |
| 79 | + title={t('firmwares.view_on_github')} |
| 80 | + onPress={handleOpenGithub} |
| 81 | + left={props => <List.Icon {...props} icon="github" />} |
| 82 | + /> |
| 83 | + <List.Item |
| 84 | + title={t('firmwares.install_firmware_on_device')} |
| 85 | + onPress={() => { |
| 86 | + // navigation.navigate('FirmwareDownload', { release }); |
| 87 | + }} |
| 88 | + left={props => <List.Icon {...props} icon="download" />} |
| 89 | + borderless |
| 90 | + style={{ |
| 91 | + borderBottomLeftRadius: settingsSurfaceBorderRadius, |
| 92 | + borderBottomRightRadius: settingsSurfaceBorderRadius, |
| 93 | + opacity: 0.5, |
| 94 | + }} |
| 95 | + disabled={true || release.tag_name === currentRelease} |
| 96 | + /> |
| 97 | + </SettingsSurface> |
| 98 | + </List.Accordion> |
| 99 | + ); |
| 100 | +}; |
| 101 | + |
| 102 | +export default FirmwareListItem; |
0 commit comments