@@ -2,24 +2,120 @@ import React from 'react';
22
33import { BrowserRouter as Router , Routes , Route , Link , useLocation } from 'react-router-dom' ;
44import { Layout , Menu } from 'antd' ;
5- import { Advisor } from '@antv/ava ' ;
5+ import type { MenuProps } from 'antd ' ;
66
7- import AdviseSummary from './examples/advise-summary' ;
8- import MultipleChartsDemo from './examples/advise-summary/multiple' ;
9- import { chartRenderer } from './utils/renderer' ;
7+ import AdviseSummary from './examples/advise' ;
8+ import MultipleChartsDemo from './examples/advise/multiple' ;
109
1110import './App.css' ;
11+ import RenderCustom from './examples/render/custom' ;
12+ import RenderDefault from './examples/render/default' ;
13+ import RenderDemand from './examples/render/demand' ;
1214
1315const { Header, Content, Sider } = Layout ;
1416
15- // 全局只绑定一次渲染器
16- Advisor . bindRenderer ( chartRenderer as any ) ;
17+ // 菜单项配置类型
18+ interface MenuItemConfig {
19+ key : string ;
20+ label : string ;
21+ path : string ;
22+ component : React . ComponentType ;
23+ }
1724
18- // 路由到菜单项的映射
19- const routeToMenuKey : Record < string , string > = {
20- '/' : 'advise-summary-basic' ,
21- '/advise-summary' : 'advise-summary-basic' ,
22- '/advise-summary-multiple' : 'advise-summary-multiple' ,
25+ interface SubMenuConfig {
26+ key : string ;
27+ label : string ;
28+ children : MenuItemConfig [ ] ;
29+ }
30+
31+ interface MenuConfig {
32+ key : string ;
33+ label : string ;
34+ children : SubMenuConfig [ ] ;
35+ }
36+
37+ // 菜单配置
38+ const menuConfig : MenuConfig [ ] = [
39+ {
40+ key : 'advice' ,
41+ label : '图表示例' ,
42+ children : [
43+ {
44+ key : 'advise-summary' ,
45+ label : '图表推荐' ,
46+ children : [
47+ {
48+ key : 'advise-summary-basic' ,
49+ label : '基础示例' ,
50+ path : '/advise-summary' ,
51+ component : AdviseSummary ,
52+ } ,
53+ {
54+ key : 'advise-summary-multiple' ,
55+ label : '多图表示例' ,
56+ path : '/advise-summary-multiple' ,
57+ component : MultipleChartsDemo ,
58+ } ,
59+ ] ,
60+ } ,
61+ {
62+ key : 'render-summary' ,
63+ label : '图表渲染' ,
64+ children : [
65+ {
66+ key : 'render-summary-basic' ,
67+ label : '基础示例' ,
68+ path : '/render-summary' ,
69+ component : RenderDefault ,
70+ } ,
71+ {
72+ key : 'render-summary-demand' ,
73+ label : 'GPT-Vis 按需引用' ,
74+ path : '/render-demand' ,
75+ component : RenderDemand ,
76+ } ,
77+ {
78+ key : 'render-summary-custom' ,
79+ label : '自定义示例' ,
80+ path : '/render-custom' ,
81+ component : RenderCustom ,
82+ } ,
83+ ] ,
84+ } ,
85+ ] ,
86+ } ,
87+ ] ;
88+
89+ // 从配置生成路由映射
90+ const routeToMenuKey : Record < string , string > = { } ;
91+ const routeComponents : Array < { path : string ; component : React . ComponentType } > = [ ] ;
92+
93+ menuConfig . forEach ( ( topMenu ) => {
94+ topMenu . children . forEach ( ( subMenu ) => {
95+ subMenu . children . forEach ( ( item ) => {
96+ routeToMenuKey [ item . path ] = item . key ;
97+ routeComponents . push ( { path : item . path , component : item . component } ) ;
98+ } ) ;
99+ } ) ;
100+ } ) ;
101+
102+ // 设置默认路由
103+ routeToMenuKey [ '/' ] = 'advise-summary-basic' ;
104+
105+ // 从配置生成菜单项
106+ const generateMenuItems = ( config : MenuConfig [ ] ) : MenuProps [ 'items' ] => {
107+ return config . map ( ( topMenu ) => ( {
108+ key : topMenu . key ,
109+ label : topMenu . label ,
110+ children : topMenu . children . map ( ( subMenu ) => ( {
111+ key : subMenu . key ,
112+ label : subMenu . label ,
113+ children : subMenu . children . map ( ( item ) => ( {
114+ key : item . key ,
115+ label : < Link to = { item . path } > { item . label } </ Link > ,
116+ } ) ) ,
117+ } ) ) ,
118+ } ) ) ;
23119} ;
24120
25121const AppContent : React . FC = ( ) => {
@@ -30,38 +126,28 @@ const AppContent: React.FC = () => {
30126 < Layout style = { { minHeight : '100vh' } } >
31127 < Header style = { { color : 'white' , fontSize : '24px' , fontWeight : 'bold' } } > AVA Playground</ Header >
32128 < Layout >
33- < Sider width = { 220 } style = { { background : '#fff' } } >
129+ < Sider width = { 220 } >
34130 < Menu
35131 mode = "inline"
36132 selectedKeys = { [ selectedKey ] }
37- defaultOpenKeys = { [ 'advice' , 'advise-summary' ] }
133+ defaultOpenKeys = { [ 'advice' , 'advise-summary' , 'render-summary' ] }
38134 style = { { height : '100%' , borderRight : 0 } }
39- >
40- < Menu . SubMenu key = "advice" title = "Advice Examples" >
41- < Menu . SubMenu key = "advise-summary" title = "Advise Summary" >
42- < Menu . Item key = "advise-summary-basic" >
43- < Link to = "/advise-summary" > 基础示例</ Link >
44- </ Menu . Item >
45- < Menu . Item key = "advise-summary-multiple" >
46- < Link to = "/advise-summary-multiple" > 多图表示例</ Link >
47- </ Menu . Item >
48- </ Menu . SubMenu >
49- </ Menu . SubMenu >
50- </ Menu >
135+ items = { generateMenuItems ( menuConfig ) }
136+ />
51137 </ Sider >
52- < Layout style = { { padding : '24px' } } >
138+ < Layout >
53139 < Content
54140 style = { {
55141 background : '#fff' ,
56- padding : 24 ,
57142 margin : 0 ,
58143 minHeight : 280 ,
59144 } }
60145 >
61146 < Routes >
62147 < Route path = "/" element = { < AdviseSummary /> } />
63- < Route path = "/advise-summary" element = { < AdviseSummary /> } />
64- < Route path = "/advise-summary-multiple" element = { < MultipleChartsDemo /> } />
148+ { routeComponents . map ( ( { path, component : Component } ) => (
149+ < Route key = { path } path = { path } element = { < Component /> } />
150+ ) ) }
65151 </ Routes >
66152 </ Content >
67153 </ Layout >
0 commit comments