From 021901a50506976129c02a3e163a1979061e8dbb Mon Sep 17 00:00:00 2001 From: Olava Faksdal <38139899+olavis@users.noreply.github.com> Date: Mon, 27 Feb 2023 09:41:34 +0100 Subject: [PATCH 1/5] refactor: separate responsibilities by moving useEffect to a custom hook --- web/src/common/components/VersionText.tsx | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/web/src/common/components/VersionText.tsx b/web/src/common/components/VersionText.tsx index 2cb6edc8..07a5df8f 100644 --- a/web/src/common/components/VersionText.tsx +++ b/web/src/common/components/VersionText.tsx @@ -1,8 +1,8 @@ import { Typography } from '@equinor/eds-core-react' -import { useEffect, useState } from 'react' import axios, { AxiosResponse } from 'axios' +import { useEffect, useState } from 'react' -export const VersionText = (): JSX.Element => { +const useCommitInfo = () => { const [commitInfo, setCommitInfo] = useState<{ [key: string]: string }>({ hash: '', date: '', @@ -14,15 +14,19 @@ export const VersionText = (): JSX.Element => { .get('version.txt') .then((response: AxiosResponse) => { const versionFile: { [key: string]: string } = Object.fromEntries( - response.data.split('\n').map((line) => { - return line.split(': ') - }) + response.data.split('\n').map((line) => line.split(': ')) ) setCommitInfo(versionFile) }) .catch(() => null) }, []) + return commitInfo +} + +export const VersionText = (): JSX.Element => { + const commitInfo = useCommitInfo() + return (

Version:{' '} From 82534b3ddcc87b70cf748a26758ea500e258f4de Mon Sep 17 00:00:00 2001 From: Olava Faksdal <38139899+olavis@users.noreply.github.com> Date: Mon, 27 Feb 2023 09:43:40 +0100 Subject: [PATCH 2/5] chore: remove empty catch --- web/src/common/components/VersionText.tsx | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/web/src/common/components/VersionText.tsx b/web/src/common/components/VersionText.tsx index 07a5df8f..adce2997 100644 --- a/web/src/common/components/VersionText.tsx +++ b/web/src/common/components/VersionText.tsx @@ -10,15 +10,12 @@ const useCommitInfo = () => { }) useEffect(() => { - axios - .get('version.txt') - .then((response: AxiosResponse) => { - const versionFile: { [key: string]: string } = Object.fromEntries( - response.data.split('\n').map((line) => line.split(': ')) - ) - setCommitInfo(versionFile) - }) - .catch(() => null) + axios.get('version.txt').then((response: AxiosResponse) => { + const versionFile: { [key: string]: string } = Object.fromEntries( + response.data.split('\n').map((line) => line.split(': ')) + ) + setCommitInfo(versionFile) + }) }, []) return commitInfo From 951129ac1d1fc3beb115f0b1cd09db7103366c4b Mon Sep 17 00:00:00 2001 From: Olava Faksdal <38139899+olavis@users.noreply.github.com> Date: Mon, 27 Feb 2023 13:05:08 +0100 Subject: [PATCH 3/5] chore: type CommitInfo --- web/src/common/components/VersionText.tsx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/web/src/common/components/VersionText.tsx b/web/src/common/components/VersionText.tsx index adce2997..1acf1e13 100644 --- a/web/src/common/components/VersionText.tsx +++ b/web/src/common/components/VersionText.tsx @@ -2,8 +2,14 @@ import { Typography } from '@equinor/eds-core-react' import axios, { AxiosResponse } from 'axios' import { useEffect, useState } from 'react' +type CommitInfo = { + hash: string + date: string + refs: string +} + const useCommitInfo = () => { - const [commitInfo, setCommitInfo] = useState<{ [key: string]: string }>({ + const [commitInfo, setCommitInfo] = useState({ hash: '', date: '', refs: '', @@ -14,7 +20,7 @@ const useCommitInfo = () => { const versionFile: { [key: string]: string } = Object.fromEntries( response.data.split('\n').map((line) => line.split(': ')) ) - setCommitInfo(versionFile) + setCommitInfo(versionFile as CommitInfo) }) }, []) From c7f4a8c4fecc239404f8ef7a555afb91cd939c21 Mon Sep 17 00:00:00 2001 From: Olava Faksdal <38139899+olavis@users.noreply.github.com> Date: Mon, 27 Feb 2023 13:08:27 +0100 Subject: [PATCH 4/5] fix: add error handling of version file fetch --- web/src/common/components/VersionText.tsx | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/web/src/common/components/VersionText.tsx b/web/src/common/components/VersionText.tsx index 1acf1e13..829e8f05 100644 --- a/web/src/common/components/VersionText.tsx +++ b/web/src/common/components/VersionText.tsx @@ -1,5 +1,5 @@ import { Typography } from '@equinor/eds-core-react' -import axios, { AxiosResponse } from 'axios' +import axios, { AxiosError, AxiosResponse } from 'axios' import { useEffect, useState } from 'react' type CommitInfo = { @@ -16,12 +16,20 @@ const useCommitInfo = () => { }) useEffect(() => { - axios.get('version.txt').then((response: AxiosResponse) => { - const versionFile: { [key: string]: string } = Object.fromEntries( - response.data.split('\n').map((line) => line.split(': ')) - ) - setCommitInfo(versionFile as CommitInfo) - }) + const fetchVersionFile = () => + axios + .get('version.txt') + .then((res: AxiosResponse) => + Object.fromEntries( + res.data.split('\n').map((line) => line.split(': ')) + ) + ) + .catch((error: AxiosError) => { + throw new Error( + `Could not read version file, ${error.response?.data ?? error}` + ) + }) + fetchVersionFile().then((commitInfo) => setCommitInfo(commitInfo)) }, []) return commitInfo From eb5c11b245be5bd28a79b0a074fc39be45cdcc16 Mon Sep 17 00:00:00 2001 From: Olava Faksdal <38139899+olavis@users.noreply.github.com> Date: Mon, 27 Feb 2023 13:23:07 +0100 Subject: [PATCH 5/5] chore: remove redundant conditional --- web/src/common/components/VersionText.tsx | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/web/src/common/components/VersionText.tsx b/web/src/common/components/VersionText.tsx index 829e8f05..3e14da88 100644 --- a/web/src/common/components/VersionText.tsx +++ b/web/src/common/components/VersionText.tsx @@ -35,23 +35,21 @@ const useCommitInfo = () => { return commitInfo } -export const VersionText = (): JSX.Element => { +export const VersionText = () => { const commitInfo = useCommitInfo() return (

Version:{' '} - {commitInfo.hash !== '' && commitInfo.date !== '' && ( - <> - - {commitInfo.refs === '' ? commitInfo.hash : commitInfo.refs} - {' '} - ({commitInfo.date}) - - )} + <> + + {commitInfo.refs === '' ? commitInfo.hash : commitInfo.refs} + {' '} + {commitInfo.date} +

) }