Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

abstract localstorage useage #130

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions src/app/components/Indicator/Indicator.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
<div className="center-container">
<button className="custom-button">
<div id="indicator" className={isConnected ? 'indicator green' : 'indicator red'} onClick={launchPiecesOS}>
<span id="indicator_text" className={`indicator-text ${isConnected ? 'connected' : 'disconnected'}`}>
{isConnected ? "You're Connected" : "You're Not Connected"} v {osVersion}
<div className='center-container'>
<button className='custom-button'>
<div
id='indicator'
className={isConnected ? 'indicator green' : 'indicator red'}
onClick={launchPiecesOS}
>
<span
id='indicator_text'
className={`indicator-text ${
isConnected ? 'connected' : 'disconnected'
}`}
>
{isConnected ? "You're Connected" : "You're Not Connected"} v{' '}
{osVersion}
</span>
<img id="checkmark" src={check} alt="checkmark" className= {`checkmark ${isConnected ? 'connected' : 'disconnected'}`} />
<img
id='checkmark'
src={check}
alt='checkmark'
className={`checkmark ${
isConnected ? 'connected' : 'disconnected'
}`}
/>
</div>
</button>
</div>
</>
);
});
)
}
24 changes: 24 additions & 0 deletions src/app/utils/useVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useState, useEffect } from 'react'

const useVersion = (key: string): string => {
const [version, setVersion] = useState<string | null>(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