diff --git a/src/app/components/Indicator/Indicator.tsx b/src/app/components/Indicator/Indicator.tsx index 6a41875..b0a37b1 100644 --- a/src/app/components/Indicator/Indicator.tsx +++ b/src/app/components/Indicator/Indicator.tsx @@ -1,31 +1,48 @@ -import * as React from "react"; -import "./indicator.css"; +import * as React from 'react' +import './indicator.css' // @ts-ignore -import check from "../../icons/check.png"; -import { launchPiecesOS } from "../../utils/launchPiecesOS"; +import check from '../../icons/check.png' +import { launchPiecesOS } from '../../utils/launchPiecesOS' +import useVersion from '../../utils/useVersion' interface IndicatorProps { - isConnected: boolean; + isConnected: boolean } -// this is your indicator badge that we will manipulate through the initial connect call. it will either -// be green or red depending on the current status. -export const Indicator = React.memo(({ isConnected }: IndicatorProps): React.JSX.Element => { - const osVersion = localStorage.getItem("version"); + +export function Indicator({ isConnected }: IndicatorProps): React.JSX.Element { + const osVersion = useVersion('version') return ( <> -
-
- ); -}); + ) +} diff --git a/src/app/utils/useVersion.ts b/src/app/utils/useVersion.ts new file mode 100644 index 0000000..fc93fb3 --- /dev/null +++ b/src/app/utils/useVersion.ts @@ -0,0 +1,24 @@ +import { useState, useEffect } from 'react' + +const useVersion = (key: string): string => { + const [version, setVersion] = useState(null) + + useEffect(() => { + if (typeof window === 'undefined') { + return + } + + try { + const storedVersion = localStorage.getItem(key) + if (storedVersion !== null) { + setVersion(storedVersion) + } + } catch (error) { + throw new Error('Error assessing the local storage') + } + }, [key]) + + return version +} + +export default useVersion