-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathalerts.ts
More file actions
100 lines (91 loc) · 2.95 KB
/
Copy pathalerts.ts
File metadata and controls
100 lines (91 loc) · 2.95 KB
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2022-2025 The Pybricks Authors
import { ToastProps } from '@blueprintjs/core';
import alerts from './alerts/alerts';
import app from './app/alerts';
import ble from './ble/alerts';
import explorer from './explorer/alerts';
import firmware from './firmware/alerts';
import hub from './hub/alerts';
import mpy from './mpy/alerts';
import sponsor from './sponsor/alerts';
import type { CreateToast } from './toasterTypes';
import usb from './usb/alerts';
/** This collects alerts from all of the subsystems of the app */
const alertDomains = {
alerts,
app,
ble,
explorer,
firmware,
hub,
mpy,
sponsor,
usb,
};
/** Gets the type of available alert domains. */
export type AlertDomain = keyof typeof alertDomains;
/**
* Gets the type of available specific alerts for a domain.
* @template D The domain.
*/
export type AlertSpecific<D extends AlertDomain> = keyof (typeof alertDomains)[D];
/**
* Gets the instance type of the object in the lookup table.
* @template D The domain.
* @template S The specific instance name in the domain.
*/
type AlertInstance<
D extends AlertDomain,
S extends AlertSpecific<D>,
> = (typeof alertDomains)[D][S] extends CreateToast<infer P, infer A>
? CreateToast<P, A>
: never;
/**
* Gets the type of the `onAlert` callback for a specific instance in the lookup table.
* @template D The domain.
* @template S The specific instance name in the domain.
*/
export type AlertCallback<
D extends AlertDomain,
S extends AlertSpecific<D>,
> = Parameters<AlertInstance<D, S>>[0];
/**
* Gets the type of available actions for a specific instance in the lookup table.
* @template D The domain.
* @template S The specific instance name in the domain.
*/
export type AlertActions<
D extends AlertDomain,
S extends AlertSpecific<D>,
> = Parameters<Parameters<AlertInstance<D, S>>[0]>[0];
/**
* Gets the type of the properties for a specific instance in the lookup table.
* @template D The domain.
* @template S The specific instance name in the domain.
*/
export type AlertProps<D extends AlertDomain, S extends AlertSpecific<D>> = Parameters<
AlertInstance<D, S>
>[1];
/**
* Gets the alert creation function from the lookup table and uses it to create
* a new alert (toast).
*
* @param domain The alert domain (app subsystem).
* @param specific The specific alert for the domain.
* @param onAlert The callback that will be called when the alert is dismissed.
* @param props Any additional properties required by this specific alert.
* @returns The newly created alert properties.
*/
export function getAlertProps<D extends AlertDomain, S extends AlertSpecific<D>>(
domain: D,
specific: S,
onAlert: AlertCallback<D, S>,
props: AlertProps<D, S>,
): ToastProps {
const create = alertDomains[domain][specific] as unknown as CreateToast<
Record<string, unknown> | never,
string
>;
return create(onAlert, props);
}