f6dc4c1ee9
This commit adds message editing as a feature. To accomplish this, the settings dialog was refactored into a singleton and now the message edit and settings use the singleton where appropriate.
54 lines
1.3 KiB
TypeScript
54 lines
1.3 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 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,
|
|
confirm,
|
|
close,
|
|
};
|
|
};
|