mt-flowbite-react
Version:
Official React components built for Flowbite and Tailwind CSS
87 lines (86 loc) • 3.24 kB
JavaScript
'use client';
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { Dialog, DialogContent, DialogHeader, DialogTrigger } from '@/components/ui/dialog';
import { Editor } from '@monaco-editor/react';
import { Bug } from 'lucide-react';
import { createContext, useContext, useEffect, useRef, useState } from 'react';
import { create, useStore } from 'zustand';
import { persist } from 'zustand/middleware';
export const DebugObjectService = {
ready: false,
count: {
builtin: 0,
},
init(builtinPrompts) {
if (this.ready) {
return;
}
this.ready = true;
},
};
const createDebugStore = (initProps) => {
const DEFAULT_PROPS = {
debugData: {},
};
return create()(persist((set, get) => ({
...DEFAULT_PROPS,
...initProps,
setItem(key, data) {
const state = get();
const newState = { ...state, debugData: { ...state.debugData, [key]: data } };
console.log('set debug item2', key, get(), newState);
set(newState); //报错:
},
}), {
name: 'debug-store',
version: 3,
skipHydration: true,
onRehydrateStorage(state) {
// console.log("onRehydrateStorage", state)
},
migrate(persistedState, version) {
// console.log("version", version)
// console.log("persistedState", persistedState)
return persistedState;
},
}));
};
//--------------------------------------------------------------------------
const Context = createContext(null);
export function DebugProvider({ children, ...props }) {
const storeRef = useRef();
if (!storeRef.current) {
storeRef.current = createDebugStore(props);
}
return _jsx(Context.Provider, { value: storeRef.current, children: children });
}
export function useDebugStore(selector, equals) {
const store = useContext(Context);
if (!store)
throw new Error('Missing Post Context.Provider in the tree');
if (selector) {
// eslint-disable-next-line
return useStore(store, selector, equals);
}
else {
return store;
}
}
export const DebugObject = ({ id, data }) => {
const setItem = useDebugStore((x) => x.setItem);
useEffect(() => {
console.log('[DBG]', id, data);
setItem(id, data);
}, [id, data]);
return _jsx(_Fragment, {});
};
/**
* 调试浮动按钮
* @param param0
* @returns
*/
export const DebugFab = () => {
const [open, setOpen] = useState(false);
const debugItems = useDebugStore((x) => x.debugData);
return (_jsxs(Dialog, { onOpenChange: setOpen, open: open, children: [_jsx(DialogTrigger, { asChild: true, children: _jsx("button", { className: "z-90 fixed bottom-2 right-2 flex h-8 w-8 items-center justify-center rounded-full bg-gradient-to-br from-blue-100 to-indigo-300 text-white shadow-lg", children: _jsx(Bug, {}) }) }), _jsxs(DialogContent, { className: "dlg-content sm:max-w-2lg h-screen max-h-full p-0 pl-8", children: [_jsx(DialogHeader, {}), _jsx(Editor, { height: "90vh", defaultLanguage: "json", value: JSON.stringify(debugItems, null, 2) })] })] }));
};