lr_boilerplate/web/src/App.tsx

67 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-01-02 10:50:35 +07:00
import React, { createContext, useEffect, useState } from 'react';
2023-12-26 22:48:59 +07:00
import { useSelector } from 'react-redux';
import { AppActions, RootState } from './store';
2023-12-11 23:38:52 +07:00
import { ToastContainer } from 'react-toastify';
import { Box } from 'lr-components';
2023-12-11 23:41:33 +07:00
import AppActionHook from './components/AppActionHook';
2023-12-13 00:21:40 +07:00
import { isEnvBrowser } from './utils/misc';
import { fetchNui } from './utils/fetchNui';
2024-01-02 10:50:35 +07:00
import { DefaultUISetting, ISettingContext, UISetting } from './types';
const SettingContext = createContext<ISettingContext>(DefaultUISetting);
2023-12-11 23:38:52 +07:00
function App() {
2023-12-26 22:48:59 +07:00
const show = useSelector((state: RootState) => state.main.show);
2024-01-02 10:50:35 +07:00
const [setting, setSetting] = useState<UISetting>({ locale: {} });
const L = (key: string) => setting.locale[key] || key;
2023-12-13 00:21:40 +07:00
useEffect(() => {
if (!isEnvBrowser()) {
2024-01-02 10:50:35 +07:00
setTimeout(async () => {
const UISetting = await fetchNui<UISetting>('AppReady');
setSetting(UISetting);
2023-12-13 00:21:40 +07:00
}, 2000);
}
2024-01-02 10:50:35 +07:00
}, [setSetting]);
2023-12-11 23:38:52 +07:00
return (
2024-01-02 10:50:35 +07:00
<SettingContext.Provider value={{ setting, setSetting, L }}>
2024-01-02 10:57:38 +07:00
<Box
display='flex'
position='absolute'
flexWrap='wrap'
justifyContent='center'
alignItems='center'
flexDirection='column'
width={'50%'}
height={'50%'}
rGap={10}
>
{Object.keys(AppActions).map((action) => {
return (
<AppActionHook
2024-01-17 17:17:58 +07:00
key={action}
2024-01-02 10:57:38 +07:00
action={action as keyof typeof AppActions}
></AppActionHook>
);
})}
</Box>
{show && (
2023-12-26 22:48:59 +07:00
<Box
2024-01-17 17:17:58 +07:00
width={'100vw'}
height={'100vh'}
position='absolute'
top={0}
left={0}
2023-12-26 22:48:59 +07:00
display='flex'
justifyContent='center'
alignItems='center'
2024-01-02 10:57:38 +07:00
className='prose'
pointerEvents='none'
></Box>
)}
<ToastContainer pauseOnFocusLoss={false} hideProgressBar={true} />
2024-01-02 10:50:35 +07:00
</SettingContext.Provider>
2023-12-11 23:38:52 +07:00
);
}
export default App;