summaryrefslogtreecommitdiff
path: root/frontend/src/components/DeckyRouterState.tsx
blob: 426ed731c95f223fc9c1d16a1872e00ab5198789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { ComponentType, FC, ReactNode, createContext, useContext, useEffect, useState } from 'react';
import type { RouteProps } from 'react-router';

export interface RouterEntry {
  props: Omit<RouteProps, 'path' | 'children'>;
  component: ComponentType;
}

export type RoutePatch = (route: RouteProps) => RouteProps;

interface PublicDeckyRouterState {
  routes: Map<string, RouterEntry>;
  routePatches: Map<string, Set<RoutePatch>>;
}

export class DeckyRouterState {
  private _routes = new Map<string, RouterEntry>();
  private _routePatches = new Map<string, Set<RoutePatch>>();

  public eventBus = new EventTarget();

  publicState(): PublicDeckyRouterState {
    return { routes: this._routes, routePatches: this._routePatches };
  }

  addRoute(path: string, component: RouterEntry['component'], props: RouterEntry['props'] = {}) {
    this._routes.set(path, { props, component });
    this.notifyUpdate();
  }

  addPatch(path: string, patch: RoutePatch) {
    let patchList = this._routePatches.get(path);
    if (!patchList) {
      patchList = new Set();
      this._routePatches.set(path, patchList);
    }
    patchList.add(patch);
    this.notifyUpdate();
    return patch;
  }

  removePatch(path: string, patch: RoutePatch) {
    const patchList = this._routePatches.get(path);
    patchList?.delete(patch);
    if (patchList?.size == 0) {
      this._routePatches.delete(path);
    }
    this.notifyUpdate();
  }

  removeRoute(path: string) {
    this._routes.delete(path);
    this.notifyUpdate();
  }

  private notifyUpdate() {
    this.eventBus.dispatchEvent(new Event('update'));
  }
}

interface DeckyRouterStateContext extends PublicDeckyRouterState {
  addRoute(path: string, component: RouterEntry['component'], props: RouterEntry['props']): void;
  addPatch(path: string, patch: RoutePatch): RoutePatch;
  removePatch(path: string, patch: RoutePatch): void;
  removeRoute(path: string): void;
}

const DeckyRouterStateContext = createContext<DeckyRouterStateContext>(null as any);

export const useDeckyRouterState = () => useContext(DeckyRouterStateContext);

interface Props {
  deckyRouterState: DeckyRouterState;
  children: ReactNode;
}

export const DeckyRouterStateContextProvider: FC<Props> = ({ children, deckyRouterState }) => {
  const [publicDeckyRouterState, setPublicDeckyRouterState] = useState<PublicDeckyRouterState>({
    ...deckyRouterState.publicState(),
  });

  useEffect(() => {
    function onUpdate() {
      setPublicDeckyRouterState({ ...deckyRouterState.publicState() });
    }

    deckyRouterState.eventBus.addEventListener('update', onUpdate);

    return () => deckyRouterState.eventBus.removeEventListener('update', onUpdate);
  }, []);

  const addRoute = deckyRouterState.addRoute.bind(deckyRouterState);
  const addPatch = deckyRouterState.addPatch.bind(deckyRouterState);
  const removePatch = deckyRouterState.removePatch.bind(deckyRouterState);
  const removeRoute = deckyRouterState.removeRoute.bind(deckyRouterState);

  return (
    <DeckyRouterStateContext.Provider
      value={{ ...publicDeckyRouterState, addRoute, addPatch, removePatch, removeRoute }}
    >
      {children}
    </DeckyRouterStateContext.Provider>
  );
};