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

70 lines
1.9 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-13 17:12:51 -03:00
import { useData } from '../../hooks/useData'
import type { Data } from '../../types/Data'
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-13 17:12:51 -03:00
type DashProps = {
initialData: Data
}
export function Dash({ initialData }: DashProps) {
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-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-15 08:11:58 -03:00
{tabs.map(tab => (
<button key={tab.value} onClick={() => setActiveTab(tab.value as 'data' | 'colors')}>
{tab.label}
</button>
))}
2021-12-13 00:36:38 -03:00
2021-12-15 08:11:58 -03:00
{body[activeTab]}
2021-12-13 00:36:38 -03:00
</Content>
<Preview>
<Home />
</Preview>
</Wrapper>
)
}