mirror of
https://github.com/wanderer-industries/wanderer
synced 2025-11-25 02:26:29 +00:00
110 lines
3.1 KiB
TypeScript
110 lines
3.1 KiB
TypeScript
import { ContextStoreDataUpdate, useContextStore } from '@/hooks/Mapper/utils';
|
|
import { createContext, Dispatch, ForwardedRef, forwardRef, RefObject, SetStateAction, useContext } from 'react';
|
|
import { MapHandlers, MapUnionTypes, OutCommandHandler, SolarSystemConnection } from '@/hooks/Mapper/types';
|
|
import { useMapRootHandlers } from '@/hooks/Mapper/mapRootProvider/hooks';
|
|
import { WithChildren } from '@/hooks/Mapper/types/common.ts';
|
|
import useLocalStorageState from 'use-local-storage-state';
|
|
|
|
export type MapRootData = MapUnionTypes & {
|
|
selectedSystems: string[];
|
|
selectedConnections: Pick<SolarSystemConnection, 'source' | 'target'>[];
|
|
};
|
|
|
|
const INITIAL_DATA: MapRootData = {
|
|
wormholesData: {},
|
|
wormholes: [],
|
|
effects: {},
|
|
characters: [],
|
|
userCharacters: [],
|
|
presentCharacters: [],
|
|
systems: [],
|
|
hubs: [],
|
|
routes: undefined,
|
|
kills: [],
|
|
connections: [],
|
|
|
|
selectedSystems: [],
|
|
selectedConnections: [],
|
|
};
|
|
|
|
export enum InterfaceStoredSettingsProps {
|
|
isShowMenu = 'isShowMenu',
|
|
isShowMinimap = 'isShowMinimap',
|
|
isShowKSpace = 'isShowKSpace',
|
|
isThickConnections = 'isThickConnections',
|
|
}
|
|
|
|
export type InterfaceStoredSettings = {
|
|
isShowMenu: boolean;
|
|
isShowMinimap: boolean;
|
|
isShowKSpace: boolean;
|
|
isThickConnections: boolean;
|
|
};
|
|
|
|
export const STORED_INTERFACE_DEFAULT_VALUES: InterfaceStoredSettings = {
|
|
isShowMenu: false,
|
|
isShowMinimap: true,
|
|
isShowKSpace: false,
|
|
isThickConnections: false,
|
|
};
|
|
|
|
export interface MapRootContextProps {
|
|
update: ContextStoreDataUpdate<MapRootData>;
|
|
data: MapRootData;
|
|
outCommand: OutCommandHandler;
|
|
interfaceSettings: InterfaceStoredSettings;
|
|
setInterfaceSettings: Dispatch<SetStateAction<InterfaceStoredSettings>>;
|
|
}
|
|
|
|
const MapRootContext = createContext<MapRootContextProps>({
|
|
update: () => {},
|
|
data: { ...INITIAL_DATA },
|
|
// @ts-ignore
|
|
outCommand: async () => void 0,
|
|
interfaceSettings: STORED_INTERFACE_DEFAULT_VALUES,
|
|
setInterfaceSettings: () => null,
|
|
});
|
|
|
|
type MapRootProviderProps = {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
fwdRef: ForwardedRef<any>;
|
|
outCommand: OutCommandHandler;
|
|
} & WithChildren;
|
|
|
|
// eslint-disable-next-line react/display-name
|
|
const MapRootHandlers = forwardRef(({ children }: WithChildren, fwdRef: ForwardedRef<any>) => {
|
|
useMapRootHandlers(fwdRef);
|
|
return <>{children}</>;
|
|
});
|
|
|
|
// eslint-disable-next-line react/display-name
|
|
export const MapRootProvider = ({ children, fwdRef, outCommand }: MapRootProviderProps) => {
|
|
const { update, ref } = useContextStore<MapRootData>({ ...INITIAL_DATA });
|
|
|
|
const [interfaceSettings, setInterfaceSettings] = useLocalStorageState<InterfaceStoredSettings>(
|
|
'window:interface:settings',
|
|
{
|
|
defaultValue: STORED_INTERFACE_DEFAULT_VALUES,
|
|
},
|
|
);
|
|
|
|
return (
|
|
<MapRootContext.Provider
|
|
value={{
|
|
update,
|
|
data: ref,
|
|
outCommand: outCommand,
|
|
setInterfaceSettings,
|
|
interfaceSettings,
|
|
}}
|
|
>
|
|
<MapRootHandlers ref={fwdRef}>{children}</MapRootHandlers>
|
|
</MapRootContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useMapRootState = () => {
|
|
const context = useContext<MapRootContextProps>(MapRootContext);
|
|
return context;
|
|
};
|