Refactor UI's props

This commit is contained in:
Eric NGUYEN 2022-10-17 19:02:36 +02:00
parent b4eba6bb9b
commit 28b0965626
2 changed files with 44 additions and 54 deletions

View file

@ -279,14 +279,11 @@ export function Editor(props: IEditorProps): JSX.Element {
return ( return (
<div ref={editorRef} className="Editor font-sans h-full"> <div ref={editorRef} className="Editor font-sans h-full">
<UI <UI
selectedContainer={selected} editorState={{
current={current} configuration: props.configuration,
history={history} history,
historyCurrentStep={historyCurrentStep} historyCurrentStep
availableContainers={configuration.AvailableContainers} }}
availableSymbols={configuration.AvailableSymbols}
categories={configuration.Categories}
apiConfiguration={configuration.APIConfiguration}
selectContainer={(container) => setNewHistory( selectContainer={(container) => setNewHistory(
SelectContainer( SelectContainer(
container, container,

View file

@ -1,16 +1,11 @@
import * as React from 'react'; import * as React from 'react';
import { ElementsList } from '../ElementsList/ElementsList'; import { ElementsList } from '../ElementsList/ElementsList';
import { History } from '../History/History'; import { History } from '../History/History';
import { IAvailableContainer } from '../../Interfaces/IAvailableContainer';
import { IContainerModel } from '../../Interfaces/IContainerModel';
import { IHistoryState } from '../../Interfaces/IHistoryState';
import { Bar } from '../Bar/Bar'; import { Bar } from '../Bar/Bar';
import { IAvailableSymbol } from '../../Interfaces/IAvailableSymbol';
import { Symbols } from '../Symbols/Symbols'; import { Symbols } from '../Symbols/Symbols';
import { SymbolsSidebar } from '../SymbolsList/SymbolsList'; import { SymbolsSidebar } from '../SymbolsList/SymbolsList';
import { PropertyType } from '../../Enums/PropertyType'; import { PropertyType } from '../../Enums/PropertyType';
import { Messages } from '../Messages/Messages'; import { Messages } from '../Messages/Messages';
import { ICategory } from '../../Interfaces/ICategory';
import { Sidebar } from '../Sidebar/Sidebar'; import { Sidebar } from '../Sidebar/Sidebar';
import { Components } from '../Components/Components'; import { Components } from '../Components/Components';
import { Viewer } from '../Viewer/Viewer'; import { Viewer } from '../Viewer/Viewer';
@ -19,17 +14,11 @@ import { IMessage } from '../../Interfaces/IMessage';
import { DISABLE_API } from '../../utils/default'; import { DISABLE_API } from '../../utils/default';
import { UseWorker, UseAsync } from './UseWorker'; import { UseWorker, UseAsync } from './UseWorker';
import { FindContainerById } from '../../utils/itertools'; import { FindContainerById } from '../../utils/itertools';
import { IAPIConfiguration } from '../../Interfaces/IAPIConfiguration'; import { IEditorState } from '../../Interfaces/IEditorState';
import { GetCurrentHistoryState } from '../Editor/Editor';
export interface IUIProps { export interface IUIProps {
selectedContainer: IContainerModel | undefined editorState: IEditorState
current: IHistoryState
history: IHistoryState[]
historyCurrentStep: number
availableContainers: IAvailableContainer[]
availableSymbols: IAvailableSymbol[]
categories: ICategory[]
apiConfiguration: IAPIConfiguration | undefined
selectContainer: (containerId: string) => void selectContainer: (containerId: string) => void
deleteContainer: (containerId: string) => void deleteContainer: (containerId: string) => void
onPropertyChange: (key: string, value: string | number | boolean | number[], type?: PropertyType) => void onPropertyChange: (key: string, value: string | number | boolean | number[], type?: PropertyType) => void
@ -67,21 +56,23 @@ function UseSetOrToggleSidebar(
}; };
} }
export function UI(props: IUIProps): JSX.Element { export function UI({ editorState, ...methods }: IUIProps): JSX.Element {
const [selectedSidebar, setSelectedSidebar] = React.useState<SidebarType>(SidebarType.Components); const [selectedSidebar, setSelectedSidebar] = React.useState<SidebarType>(SidebarType.Components);
const [messages, setMessages] = React.useState<IMessage[]>([]); const [messages, setMessages] = React.useState<IMessage[]>([]);
const current = GetCurrentHistoryState(editorState.history, editorState.historyCurrentStep);
const configuration = editorState.configuration;
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (window.Worker && !DISABLE_API) { if (window.Worker && !DISABLE_API) {
UseWorker( UseWorker(
props.current, current,
props.apiConfiguration?.apiGetFeedbackUrl, configuration.APIConfiguration?.apiGetFeedbackUrl,
setMessages setMessages
); );
} else if (!DISABLE_API) { } else if (!DISABLE_API) {
UseAsync( UseAsync(
props.current, current,
props.apiConfiguration?.apiGetFeedbackUrl, configuration.APIConfiguration?.apiGetFeedbackUrl,
setMessages setMessages
); );
} }
@ -94,61 +85,63 @@ export function UI(props: IUIProps): JSX.Element {
let leftChildren: JSX.Element = (<></>); let leftChildren: JSX.Element = (<></>);
let rightChildren: JSX.Element = (<></>); let rightChildren: JSX.Element = (<></>);
const mainContainer = FindContainerById(props.current.containers, props.current.mainContainer) const mainContainer = FindContainerById(current.containers, current.mainContainer);
if (mainContainer === undefined) { if (mainContainer === undefined) {
throw new Error('Tried to initialized UI but there is no main container!'); throw new Error('Tried to initialized UI but there is no main container!');
} }
const selectedContainer = FindContainerById(current.containers, current.selectedContainerId);
switch (selectedSidebar) { switch (selectedSidebar) {
case SidebarType.Components: case SidebarType.Components:
leftSidebarTitle = 'Components'; leftSidebarTitle = 'Components';
leftChildren = <Components leftChildren = <Components
selectedContainer={props.selectedContainer} selectedContainer={selectedContainer}
componentOptions={props.availableContainers} componentOptions={configuration.AvailableContainers}
categories={props.categories} categories={configuration.Categories}
buttonOnClick={props.addContainer} buttonOnClick={methods.addContainer}
/>; />;
rightSidebarTitle = 'Elements'; rightSidebarTitle = 'Elements';
rightChildren = <ElementsList rightChildren = <ElementsList
containers={props.current.containers} containers={current.containers}
mainContainer={mainContainer} mainContainer={mainContainer}
symbols={props.current.symbols} symbols={current.symbols}
selectedContainer={props.selectedContainer} selectedContainer={selectedContainer}
onPropertyChange={props.onPropertyChange} onPropertyChange={methods.onPropertyChange}
selectContainer={props.selectContainer} selectContainer={methods.selectContainer}
addContainer={props.addContainerAt} addContainer={methods.addContainerAt}
/>; />;
break; break;
case SidebarType.Symbols: case SidebarType.Symbols:
leftSidebarTitle = 'Symbols'; leftSidebarTitle = 'Symbols';
leftChildren = <Symbols leftChildren = <Symbols
componentOptions={props.availableSymbols} componentOptions={configuration.AvailableSymbols}
buttonOnClick={props.addSymbol} buttonOnClick={methods.addSymbol}
/>; />;
rightSidebarTitle = 'Symbols'; rightSidebarTitle = 'Symbols';
rightChildren = <SymbolsSidebar rightChildren = <SymbolsSidebar
selectedSymbolId={props.current.selectedSymbolId} selectedSymbolId={current.selectedSymbolId}
symbols={props.current.symbols} symbols={current.symbols}
onPropertyChange={props.onSymbolPropertyChange} onPropertyChange={methods.onSymbolPropertyChange}
selectSymbol={props.selectSymbol} selectSymbol={methods.selectSymbol}
/>; />;
break; break;
case SidebarType.History: case SidebarType.History:
leftSidebarTitle = 'Timeline'; leftSidebarTitle = 'Timeline';
leftChildren = <History leftChildren = <History
history={props.history} history={editorState.history}
historyCurrentStep={props.historyCurrentStep} historyCurrentStep={editorState.historyCurrentStep}
jumpTo={props.loadState} jumpTo={methods.loadState}
/>; />;
break; break;
case SidebarType.Messages: case SidebarType.Messages:
leftSidebarTitle = 'Messages'; leftSidebarTitle = 'Messages';
leftChildren = <Messages leftChildren = <Messages
historyState={props.current} historyState={current}
messages={messages} messages={messages}
clearMessage={() => setMessages([])} clearMessage={() => setMessages([])}
/>; />;
@ -157,8 +150,8 @@ export function UI(props: IUIProps): JSX.Element {
case SidebarType.Settings: case SidebarType.Settings:
leftSidebarTitle = 'Settings'; leftSidebarTitle = 'Settings';
leftChildren = <Settings leftChildren = <Settings
saveEditorAsJSON={props.saveEditorAsJSON} saveEditorAsJSON={methods.saveEditorAsJSON}
saveEditorAsSVG={props.saveEditorAsSVG} saveEditorAsSVG={methods.saveEditorAsSVG}
/>; />;
break; break;
} }
@ -166,7 +159,7 @@ export function UI(props: IUIProps): JSX.Element {
const isLeftSidebarOpen = selectedSidebar !== SidebarType.None; const isLeftSidebarOpen = selectedSidebar !== SidebarType.None;
const isRightSidebarOpen = selectedSidebar === SidebarType.Components || selectedSidebar === SidebarType.Symbols; const isRightSidebarOpen = selectedSidebar === SidebarType.Components || selectedSidebar === SidebarType.Symbols;
let isLeftSidebarOpenClasses = new Set<string>([ const isLeftSidebarOpenClasses = new Set<string>([
'left-sidebar', 'left-sidebar',
'left-16', 'left-16',
'-bottom-full', '-bottom-full',
@ -222,9 +215,9 @@ export function UI(props: IUIProps): JSX.Element {
<Viewer <Viewer
isLeftSidebarOpen={isLeftSidebarOpen} isLeftSidebarOpen={isLeftSidebarOpen}
isRightSidebarOpen={isRightSidebarOpen} isRightSidebarOpen={isRightSidebarOpen}
current={props.current} current={current}
selectedContainer={props.selectedContainer} selectedContainer={selectedContainer}
selectContainer={props.selectContainer} selectContainer={methods.selectContainer}
/> />
<Sidebar <Sidebar
className={`right-sidebar ${isRightSidebarOpenClasses}`} className={`right-sidebar ${isRightSidebarOpenClasses}`}