Skip to content

Commit

Permalink
Merge pull request #282 from OpenDTU-App/fix-version-if-flashing-self…
Browse files Browse the repository at this point in the history
…-compiled

Fix version if flashing self compiled opendtu firmware
  • Loading branch information
CommanderRedYT authored Dec 18, 2024
2 parents f556a80 + 006a59b commit 7de4b96
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/hooks/useHasNewOpenDtuVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { compare } from 'compare-versions';

import useDtuState from '@/hooks/useDtuState';

import correctTag from '@/utils/correctTag';

import { useAppSelector } from '@/store';

const useHasNewOpenDtuVersion = (options?: {
Expand All @@ -21,20 +23,28 @@ const useHasNewOpenDtuVersion = (options?: {

const currentRelease = useDtuState(state => state?.systemStatus?.git_hash);

const correctedCurrentRelease = correctTag(currentRelease);

return useMemo(() => {
if (!openDtuRelease || !currentRelease) return [false, null] as const;
if (!openDtuRelease || !correctedCurrentRelease)
return [false, null] as const;

const newAppVersionAvailable = compare(
openDtuRelease?.tag_name,
currentRelease,
correctedCurrentRelease,
'>',
);

return [
!showIndicator && usedForIndicatorOnly ? false : newAppVersionAvailable,
openDtuRelease,
] as const;
}, [currentRelease, openDtuRelease, showIndicator, usedForIndicatorOnly]);
}, [
correctedCurrentRelease,
openDtuRelease,
showIndicator,
usedForIndicatorOnly,
]);
};

export default useHasNewOpenDtuVersion;
12 changes: 12 additions & 0 deletions src/utils/correctTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const correctTag = <T extends string | (string | undefined)>(tag: T): T => {
if (!tag) {
return tag;
}

const regex = /^v\d+\.\d+\.\d+/;
const match = tag.match(regex);

return (match ? match[0] : tag) as T;
};

export default correctTag;
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import SettingsSurface from '@/components/styled/SettingsSurface';

import useDtuState from '@/hooks/useDtuState';

import correctTag from '@/utils/correctTag';

import { minimumOpenDtuFirmwareVersion, spacing } from '@/constants';
import { useFetchControl } from '@/github/FetchHandler';
import { useAppSelector } from '@/store';
Expand All @@ -30,7 +32,11 @@ const FirmwareListScreen: FC<PropsWithNavigation> = ({ navigation }) => {
const { refreshLatestRelease, refreshReleases } = useFetchControl();

const releases = useAppSelector(state => state.github.releases);
const currentRelease = useDtuState(state => state?.systemStatus?.git_hash);
const rawCurrentRelease = useDtuState(state => state?.systemStatus?.git_hash);
const currentRelease = useMemo(
() => correctTag(rawCurrentRelease),
[rawCurrentRelease],
);

const newReleases = useMemo(() => {
if (!currentRelease) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import SettingsSurface, {
import useDtuState from '@/hooks/useDtuState';
import useHasNewOpenDtuVersion from '@/hooks/useHasNewOpenDtuVersion';

import correctTag from '@/utils/correctTag';
import formatBytes from '@/utils/formatBytes';
import percentage from '@/utils/percentage';

Expand Down Expand Up @@ -51,9 +52,13 @@ const SystemInformationScreen: FC<PropsWithNavigation> = ({ navigation }) => {
const openGitHub = useCallback(async () => {
if (!systemStatus) return undefined;

const corrected = correctTag(systemStatus.git_hash);

if (!corrected) return undefined;

const url = systemStatus.git_is_hash
? `https://github.com/tbnobody/OpenDTU/commits/${systemStatus.git_hash}`
: `https://github.com/tbnobody/OpenDTU/releases/tag/${systemStatus.git_hash}`;
? `https://github.com/tbnobody/OpenDTU/commits/${corrected}`
: `https://github.com/tbnobody/OpenDTU/releases/tag/${corrected}`;

if (await Linking.canOpenURL(url)) {
await Linking.openURL(url);
Expand All @@ -66,7 +71,9 @@ const SystemInformationScreen: FC<PropsWithNavigation> = ({ navigation }) => {

if (systemStatus?.git_is_hash) return systemStatus.git_hash;

const isLatest = compare(systemStatus.git_hash, latestVersion, '>=');
const corrected = correctTag(systemStatus.git_hash);

const isLatest = compare(corrected, latestVersion, '>=');

return isLatest
? `${systemStatus.git_hash} (latest)`
Expand Down Expand Up @@ -218,6 +225,7 @@ const SystemInformationScreen: FC<PropsWithNavigation> = ({ navigation }) => {
description={versionString}
right={props => <List.Icon {...props} icon="git" />}
onPress={openGitHub}
disabled={!openGitHub}
/>
<List.Item
title={t('opendtu.systemInformationScreen.pioEnvironment')}
Expand Down

0 comments on commit 7de4b96

Please sign in to comment.