// SPDX-License-Identifier: MIT // Copyright (c) 2022 The Pybricks Authors import { ToastProps } from '@blueprintjs/core'; import alerts from './alerts/alerts'; import ble from './ble/alerts'; import explorer from './explorer/alerts'; import firmware from './firmware/alerts'; import type { CreateToast } from './toasterTypes'; /** This collects alerts from all of the subsystems of the app */ const alertDomains = { alerts, ble, explorer, firmware, }; /** 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 = 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, > = typeof alertDomains[D][S] extends CreateToast ? CreateToast : 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, > = Parameters>[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> = | Parameters>[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> = Parameters< AlertInstance >[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>( domain: D, specific: S, onAlert: AlertCallback, props: AlertProps, ): ToastProps { const create = alertDomains[domain][specific] as unknown as CreateToast< Record | never, string >; return create(onAlert, props); }