Files
veridian/app/composables/useDialog.ts
T
zoeissleeping 62f1fe1c27 fix: dropdown settings buttons | fix: improve model selector
I should have split this into two commits, but I found the issue with the
settings buttons *after* I changed how the model selector worked, so its
all in this single commit.
2026-02-25 16:12:51 +00:00

59 lines
1.4 KiB
TypeScript

import { Err, Ok, type Result } from "~~/types/result";
export enum DialogType {
Settings = 'settings',
Textbox = 'textbox',
Confirm = 'confirm',
}
interface DialogOptions {
title?: string;
initialValue?: string;
[key: string]: any;
}
let actionCallback: ((value: any) => void) | undefined;
export const useDialog = () => {
const page = useState<DialogType | null>('dialog:page', () => null);
const open = useState('dialog:open', () => false);
const data = useState<DialogOptions>('dialog:data', () => ({}));
const openDialog = <T = any>(type: DialogType, cb?: (value: Result<T, string>) => void, options: DialogOptions = {}) => {
page.value = type;
data.value = options;
open.value = true;
actionCallback = cb;
};
const setOptions = (options: DialogOptions) => {
data.value = options;
};
const confirm = (result: any) => {
open.value = false;
if (actionCallback === undefined) return;
actionCallback?.(Ok(result));
actionCallback = undefined;
};
const close = () => {
open.value = false;
if (actionCallback === undefined) return;
actionCallback?.(Err('closed'));
actionCallback = undefined;
};
return {
open: readonly(open),
page: readonly(page),
data: readonly(data),
openDialog,
setOptions,
confirm,
close,
};
};