diff --git a/package.json b/package.json index fe46367..df64166 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "web:dev": "vite", "web:preview": "vite preview", - "web:build": "tsc && vite build", + "web:build": "vite build", "sidecar:build": "node build-sidecar.mjs", "tauri": "tauri" }, diff --git a/src/App.tsx b/src/App.tsx index 1988d51..20845cd 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,26 +1,28 @@ -import { useStartupCheck } from '@/hooks/useStartupCheck' -import { usePreventDefault } from '@/hooks/usePreventDefaultEventListener' -import { useGlobalShortcut } from '@/hooks/useGlobalShortcut' -import { useRecreateTheCircuit } from '@/hooks/useRecreateTheCircuit' -import { useCoreConfig } from './hooks/useCoreConfig' +import { useStartupCheck } from "@/hooks/useStartupCheck"; +import { usePreventDefault } from "@/hooks/usePreventDefaultEventListener"; +import { useGlobalShortcut } from "@/hooks/useGlobalShortcut"; +import { useRecreateTheCircuit } from "@/hooks/useRecreateTheCircuit"; +import { useCoreConfig } from "./hooks/useCoreConfig"; // import { commands } from '@/bindings' -import Titlebar from '@/components/Titlebar' +import Titlebar from "@/components/Titlebar"; + +import Layout from "@/layout"; +import Tray from "@/components/Tray"; -import Layout from '@/layout' -import Tray from '@/components/Tray' function App() { // 执行启动自检 - useStartupCheck() + useStartupCheck(); // 阻止默认事件 - usePreventDefault() + usePreventDefault(); // 注册全局快捷方式 - useGlobalShortcut() + useGlobalShortcut(); // 当核心启动时重新创建电路 - useRecreateTheCircuit() + useRecreateTheCircuit(); // 读取配置,若文件不存在则持久化到本地 `%APPDATA%/com.paw.paw-gui` - const { loadCoreConfig } = useCoreConfig() - loadCoreConfig() + const { loadCoreConfig } = useCoreConfig(); + loadCoreConfig(); + return ( @@ -28,7 +30,7 @@ function App() { - ) + ); } -export default App +export default App; diff --git a/src/api/block.ts b/src/api/block.ts new file mode 100644 index 0000000..8919c25 --- /dev/null +++ b/src/api/block.ts @@ -0,0 +1,28 @@ +import { CoreConfig } from "@/bindings"; +import { api } from "@/utils/api"; + +// 区块链api 类 +export class BlockChainApi { + public coreConfig: CoreConfig; + public baseUrl = "http://localhost:3000/api"; + + constructor() { + this.coreConfig = {} as CoreConfig; + } + + // async initCoreConfig() { + // // 异步调用 loadCoreConfig 函数,获取核心配置信息 + // this.coreConfig = await loadCoreConfig(); + // } + + // 获取最新的区块信息 + async getLatestBlock() { + const res = await api.get( + this.baseUrl + "/cosmos/base/tendermint/v1beta1/blocks/latest", + ); + return res + } +} + +// 导出一个异步函数 initBlockChinApi,用于初始化区块链API +export const blockChainApi = new BlockChainApi(); diff --git a/src/api/home.ts b/src/api/home.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/hooks/useCoreConfig.ts b/src/hooks/useCoreConfig.ts index 69496ac..3b61ce5 100644 --- a/src/hooks/useCoreConfig.ts +++ b/src/hooks/useCoreConfig.ts @@ -31,24 +31,26 @@ const createConfigStore = async () => { return await load('core_config.json', { autoSave: true }) } + +export const loadCoreConfig = async () => { + const store = await createConfigStore() + const savedConfig = await store.get('core_config') + + if (savedConfig) { + return savedConfig + } + + const defaultConfig = await commands.getDefaultCoreConfig() + await store.set('core_config', defaultConfig) + await store.save() + return defaultConfig +} + export function useCoreConfig() { /** * 从本地存储加载配置,若没有则加载默认配置并存储到本地 */ - const loadCoreConfig = async () => { - const store = await createConfigStore() - const savedConfig = await store.get('core_config') - console.log(savedConfig,'defaultConfig') - if (savedConfig) { - return savedConfig - } - - const defaultConfig = await commands.getDefaultCoreConfig() - await store.set('core_config', defaultConfig) - await store.save() - return defaultConfig - } /** * 保存配置到本地存储 diff --git a/src/pages/new-home/index.tsx b/src/pages/new-home/index.tsx index 2681172..e8152df 100644 --- a/src/pages/new-home/index.tsx +++ b/src/pages/new-home/index.tsx @@ -26,6 +26,7 @@ import type { AppDispatch, RootState } from "@/store"; import "./index.scss"; import { DialogConfig, FormAlertDialog } from "./components/FormAlertDialog"; import { ClearNodeDialog } from "./components/ClearNodeDialog"; +import { blockChainApi } from "@/api/block"; export const DIALOGTYPE = { ADDNode: { @@ -68,6 +69,8 @@ const NewHome = () => { NODEDIALOGTYPE.ClearFailNode ); + const [blockChain, setBlockChain] = useState(null); + const [tooltipClosed, setTooltipClosed] = useState(false); const [tooltipType, setTooltipType] = useState( @@ -170,9 +173,17 @@ const NewHome = () => { // return () => clearInterval(interval); // }, [dispatch]); + useEffect(()=>{ + blockChainApi.getLatestBlock().then((res)=>{ + console.log("res",res) + setBlockChain(res) + }) + },[]) + return ( + {blockChain?.block_id?.hash || "111"} {/*