Files
link-free/src/components/Dash/index.tsx

72 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-12-13 00:36:38 -03:00
import { isEqual } from 'lodash'
import { useEffect, useState } from 'react'
2021-12-18 00:50:54 -03:00
import Collapsible from 'react-collapsible'
2021-12-13 00:36:38 -03:00
2021-12-13 17:12:51 -03:00
import { useData } from '../../hooks/useData'
2021-12-15 08:11:58 -03:00
import { getLocalStorageData, setLocalStorageData } from '../../utils/localStorage'
2021-12-13 00:36:38 -03:00
import { Home } from '../Home'
2021-12-15 08:11:58 -03:00
import { ColorsSettings } from './ColorsSettings'
import { DataSettings } from './DataSettings'
import { SaveChangesAlert } from './SaveChangesAlert'
2021-12-13 00:36:38 -03:00
import { Content, Preview, Wrapper } from './styles'
2021-12-18 00:50:54 -03:00
export function Dash() {
2021-12-13 17:12:51 -03:00
const { data, setData } = useData()
2021-12-13 00:36:38 -03:00
const [isLoading, setIsLoading] = useState(true)
const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false)
2021-12-15 08:11:58 -03:00
const [activeTab, setActiveTab] = useState<'data' | 'colors'>('data')
2021-12-18 00:50:54 -03:00
const initialData = data
2021-12-13 00:36:38 -03:00
useEffect(() => {
2021-12-13 17:12:51 -03:00
if (isLoading) {
const storageData = getLocalStorageData()
if (storageData) setData(storageData)
else setData(initialData)
2021-12-13 00:36:38 -03:00
2021-12-13 17:12:51 -03:00
setIsLoading(false)
2021-12-13 00:36:38 -03:00
}
2021-12-13 17:12:51 -03:00
if (!isLoading && !isEqual(initialData, data)) {
2021-12-13 00:36:38 -03:00
setHasUnsavedChanges(true)
setLocalStorageData(data)
} else {
setHasUnsavedChanges(false)
}
2021-12-13 17:12:51 -03:00
}, [data, initialData, isLoading, setData])
2021-12-13 00:36:38 -03:00
2021-12-15 08:11:58 -03:00
const tabs = [
{ label: 'Data', value: 'data' },
{ label: 'Colors', value: 'colors' }
]
const body = { data: <DataSettings />, colors: <ColorsSettings /> }
2021-12-13 00:36:38 -03:00
return (
<Wrapper>
<Content>
2021-12-15 08:11:58 -03:00
<SaveChangesAlert
hasUnsavedChanges={hasUnsavedChanges}
setHasUnsavedChanges={setHasUnsavedChanges}
initialData={initialData}
/>
<h1>Dash </h1>
2021-12-13 00:36:38 -03:00
2021-12-18 00:50:54 -03:00
{tabs.map((tab, i) => (
<button key={i} onClick={() => setActiveTab(tab.value as 'data' | 'colors')}>
2021-12-15 08:11:58 -03:00
{tab.label}
</button>
))}
2021-12-13 00:36:38 -03:00
2021-12-15 08:11:58 -03:00
{body[activeTab]}
2021-12-18 00:50:54 -03:00
<Collapsible trigger="Start here">
<p>This is the collapsible content. It can be any element or React component you like.</p>
<p>It can even be another Collapsible component. Check out the next section!</p>
</Collapsible>
2021-12-13 00:36:38 -03:00
</Content>
<Preview>
<Home />
</Preview>
</Wrapper>
)
}