import { action, makeObservable, observable } from 'mobx'; import { getCollectionComponentUniqueId } from '../utils/getCollectionComponentUniqueId'; import { ICollectionComponentState } from './ICollectionComponentState'; import { NavigateOptions, WixPatternsContainer } from '@wix/bex-core'; import { NavigateFunction } from 'react-router-dom'; interface WixPatternsRouterStateParams { container: WixPatternsContainer; navigate: NavigateFunction; } export type RouteState = { _createdEntity?: any; _updatedEntity?: any; _chosenEntity?: any; } | null; export class WixPatternsRouterState { readonly stateRefs: Map = new Map(); readonly container: WixPatternsContainer; readonly _navigate; currentState: RouteState = null; isUsingCache = false; isCollectionStale = false; constructor(params: WixPatternsRouterStateParams) { this.container = params.container; this._navigate = params.navigate; makeObservable(this, { currentState: observable.ref, setCurrentState: action, isCollectionStale: observable, }); } setCurrentState(state: RouteState) { this.currentState = state; } saveState(state: S) { const uniqueId = getCollectionComponentUniqueId(state); this.stateRefs.set(uniqueId, state); } navigate(to: string, options: NavigateOptions): void { this.setCurrentState(options.state); this._navigate?.(to, options); } }