Browse Source

添加 vconsole

yourname 6 months ago
parent
commit
62c5f09721
5 changed files with 23 additions and 9 deletions
  1. 2 2
      src/client/app.tsx
  2. 1 2
      src/server/api.ts
  3. 2 2
      src/server/api/base.ts
  4. 3 2
      src/server/index.tsx
  5. 15 1
      src/server/renderer.tsx

+ 2 - 2
src/client/app.tsx

@@ -2,7 +2,7 @@ import { createBrowserRouter, RouterProvider } from 'react-router-dom'
 import { use, Suspense } from 'react'
 import { hc } from 'hono/client'
 import type { InferResponseType } from 'hono/client'
-import type { AppType } from '../server/api'
+import type { AppType } from '../server/api/base'
 
 const client = hc<AppType>('/api')
 
@@ -31,7 +31,7 @@ const About = () => {
 
 const ApiDemo = ({ promise }: { promise: Promise<InferResponseType<typeof client.index.$get>> }) => {
   const data = use(promise)
-  return <h2 className="text-2xl">{data.message}{JSON.stringify(import.meta.env)}</h2>
+  return <h2 className="text-2xl">{data.message}</h2>
 }
 
 const router = createBrowserRouter([

+ 1 - 2
src/server/api.ts

@@ -17,7 +17,6 @@ app.doc('/doc', {
     title: 'API Documentation',
     version: '1.0.0'
   }
-})
+})   
 
 export default app
-export type AppType = typeof app

+ 2 - 2
src/server/api/base.ts

@@ -40,10 +40,10 @@ const route = createRoute({
   }
 })
 
-app.openapi(route, (c) => {
+const routes = app.openapi(route, (c) => {
   const { name } = c.req.valid('query')
   return c.json({ message: `Hello from API${name ? `, ${name}` : ''}` })
 })
 
 export default app
-export type BaseAppType = typeof app
+export type AppType = typeof routes

+ 3 - 2
src/server/index.tsx

@@ -1,7 +1,8 @@
-// import { Hono } from 'hono'
+import { Hono } from 'hono'
 import { swaggerUI } from '@hono/swagger-ui'
 import { renderer } from './renderer'
 import app from './api'
+// const app = new Hono()
 
 app.use(renderer)
 
@@ -14,6 +15,6 @@ app.get('/*', (c) => {
       <div id="root"></div>
     </>
   )
-})
+}) 
 
 export default app

+ 15 - 1
src/server/renderer.tsx

@@ -9,8 +9,22 @@ export const renderer = reactRenderer(({ children }) => {
         <meta name="viewport" content="width=device-width, initial-scale=1.0" />
         <Script/>
         <Link href="/src/style.css" rel="stylesheet" />
+        <script src="https://ai-oss.d8d.fun/umd/vconsole.3.15.1.min.js"></script>
+        <script dangerouslySetInnerHTML={{ __html: `
+          const init = () => {
+            const urlParams = new URLSearchParams(window.location.search);
+            if (${import.meta.env?.PROD ? "true":"false"} && !urlParams.has('vconsole')) return;
+            var vConsole = new VConsole({
+              theme: urlParams.get('vconsole_theme') || 'light',
+              onReady: function() {
+                console.log('vConsole is ready');
+              }
+            });
+          }
+          init();
+        `}} />
       </head>
       <body>{children}</body>
-    </html>
+    </html> 
   )
 })