summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/utilities/event.ts
blob: 60fbd63e1c0fbcfeb42ae5a6019ba1fc5f672053 (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
type CustomEventDetail<T> = T extends CustomEvent<infer D> ? D : never;

function createEvent<
  K extends keyof WindowEventMap,
  P extends CustomEventDetail<WindowEventMap[K]>
>(event: K, payload: P) {
  return new CustomEvent<P>(event, { bubbles: true, detail: payload });
}

export function setAuthenticated(authenticated: boolean) {
  const event = createEvent("app-auth-changed", { authenticated });
  window.dispatchEvent(event);
}

export function setCriticalError(message: string) {
  const event = createEvent("app-critical-error", { message });

  window.dispatchEvent(event);
}

export function setOnlineStatus(online: boolean) {
  const event = createEvent("app-online-status", { online });
  window.dispatchEvent(event);
}