50 lines
986 B
TypeScript
50 lines
986 B
TypeScript
export interface AppConfig {
|
|
baseRoot: string;
|
|
baseDashboard: string;
|
|
baseAuth: string;
|
|
baseApi: string;
|
|
view: 'selection' | 'dashboard';
|
|
initialGuildId?: string;
|
|
isAdmin?: boolean;
|
|
userLabel?: string;
|
|
}
|
|
|
|
export interface AppState {
|
|
guildId?: string;
|
|
isAdmin?: boolean;
|
|
userLabel?: string;
|
|
}
|
|
|
|
type Listener = (state: AppState) => void;
|
|
|
|
let config: AppConfig | null = null;
|
|
let state: AppState = {};
|
|
const listeners = new Set<Listener>();
|
|
|
|
export function initConfig(next: AppConfig) {
|
|
config = next;
|
|
state = {
|
|
guildId: next.initialGuildId,
|
|
isAdmin: next.isAdmin,
|
|
userLabel: next.userLabel
|
|
};
|
|
}
|
|
|
|
export function getConfig() {
|
|
return config;
|
|
}
|
|
|
|
export function getState() {
|
|
return state;
|
|
}
|
|
|
|
export function setState(partial: Partial<AppState>) {
|
|
state = { ...state, ...partial };
|
|
listeners.forEach((l) => l(state));
|
|
}
|
|
|
|
export function subscribe(listener: Listener) {
|
|
listeners.add(listener);
|
|
return () => listeners.delete(listener);
|
|
}
|