You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.7 KiB
56 lines
1.7 KiB
import { toRef, isRef } from 'vue';
|
|
import { u as useNuxtApp } from './server.mjs';
|
|
|
|
const useStateKeyPrefix = "$s";
|
|
function useState(...args) {
|
|
const autoKey = typeof args[args.length - 1] === "string" ? args.pop() : void 0;
|
|
if (typeof args[0] !== "string") {
|
|
args.unshift(autoKey);
|
|
}
|
|
const [_key, init] = args;
|
|
if (!_key || typeof _key !== "string") {
|
|
throw new TypeError("[nuxt] [useState] key must be a string: " + _key);
|
|
}
|
|
if (init !== void 0 && typeof init !== "function") {
|
|
throw new Error("[nuxt] [useState] init must be a function: " + init);
|
|
}
|
|
const key = useStateKeyPrefix + _key;
|
|
const nuxtApp = useNuxtApp();
|
|
const state = toRef(nuxtApp.payload.state, key);
|
|
if (init) {
|
|
nuxtApp._state[key] ??= { _default: init };
|
|
}
|
|
if (state.value === void 0 && init) {
|
|
const initialValue = init();
|
|
if (isRef(initialValue)) {
|
|
nuxtApp.payload.state[key] = initialValue;
|
|
return initialValue;
|
|
}
|
|
state.value = initialValue;
|
|
}
|
|
return state;
|
|
}
|
|
let _counter = 0;
|
|
function useToast() {
|
|
const toasts = useState("app:toasts", () => []);
|
|
function toast(msg, type = "info", duration = 3e3) {
|
|
const id = ++_counter;
|
|
toasts.value.push({ id, type, msg });
|
|
if (duration > 0) {
|
|
setTimeout(() => {
|
|
toasts.value = toasts.value.filter((t) => t.id !== id);
|
|
}, duration);
|
|
}
|
|
}
|
|
return {
|
|
toasts,
|
|
toast,
|
|
success: (msg, duration) => toast(msg, "success", duration),
|
|
error: (msg, duration) => toast(msg, "error", duration),
|
|
info: (msg, duration) => toast(msg, "info", duration),
|
|
warn: (msg, duration) => toast(msg, "warning", duration)
|
|
};
|
|
}
|
|
|
|
export { useToast as u };
|
|
//# sourceMappingURL=useToast-BwtTR9dH.mjs.map
|
|
|