Files
Psychotoxical-psysonic/src/store/orbitAccountPickerStore.ts
T
2026-04-24 18:49:55 +02:00

40 lines
1.1 KiB
TypeScript

import { create } from 'zustand';
import type { ServerProfile } from './authStore';
let _resolve: ((server: ServerProfile | null) => void) | null = null;
interface OrbitAccountPickerStore {
isOpen: boolean;
accounts: ServerProfile[];
/** Open the picker with the given candidates. Resolves with the chosen
* server or null if the user cancels. */
request: (accounts: ServerProfile[]) => Promise<ServerProfile | null>;
pick: (server: ServerProfile) => void;
cancel: () => void;
}
export const useOrbitAccountPickerStore = create<OrbitAccountPickerStore>(set => ({
isOpen: false,
accounts: [],
request: (accounts) =>
new Promise<ServerProfile | null>(resolve => {
// If another picker is already pending, treat the previous one as cancelled.
if (_resolve) _resolve(null);
_resolve = resolve;
set({ isOpen: true, accounts });
}),
pick: (server) => {
_resolve?.(server);
_resolve = null;
set({ isOpen: false });
},
cancel: () => {
_resolve?.(null);
_resolve = null;
set({ isOpen: false });
},
}));