|
1 | | -import { useCallback, useState } from 'react'; |
| 1 | +import { useMemo, useRef } from 'react'; |
| 2 | +import DataGrid, { |
| 3 | + AI, |
| 4 | + AIAssistant, |
| 5 | + Column, |
| 6 | + FilterRow, |
| 7 | + GroupPanel, |
| 8 | + HeaderFilter, |
| 9 | + Pager, |
| 10 | + Paging, |
| 11 | + SearchPanel, |
| 12 | +} from 'devextreme-react/data-grid'; |
| 13 | +import type { ButtonGroupTypes } from 'devextreme-react/button-group'; |
| 14 | +import type { ChatTypes } from 'devextreme-react/chat'; |
| 15 | +import type dxChat from 'devextreme/ui/chat'; |
| 16 | +import 'devextreme/dist/css/dx.fluent.blue.light.css'; |
2 | 17 | import './App.css'; |
3 | | -import 'devextreme/dist/css/dx.material.blue.light.compact.css'; |
4 | | -import Button from 'devextreme-react/button'; |
| 18 | + |
| 19 | +import AIService from './service'; |
| 20 | +import { AI_ASSISTANT_URL, AI_COLUMN_URL, sales } from './data'; |
| 21 | + |
| 22 | +const HELP_PROMPT = `💡 The DataGrid AI Assistant allows you to control the component using natural language. You can execute commands such as the following: |
| 23 | + • Sort records |
| 24 | + • Apply a filter |
| 25 | + • Search for a specific value |
| 26 | + • Group records by a field |
| 27 | + • Focus and select rows |
| 28 | + • Modify paging settings |
| 29 | + • Pin, resize, and reorder columns |
| 30 | + • Configure data summaries |
| 31 | + • Pick a suggestion or enter a custom request to get started.`; |
| 32 | + |
| 33 | +const allowedPageSizes = [10, 25, 50, 100]; |
| 34 | + |
| 35 | +const service = new AIService(AI_COLUMN_URL, AI_ASSISTANT_URL); |
5 | 36 |
|
6 | 37 | function App(): JSX.Element { |
7 | | - var [count, setCount] = useState<number>(0); |
8 | | - const clickHandler = useCallback(() => { |
9 | | - setCount((prev) => prev + 1); |
10 | | - }, [setCount]); |
| 38 | + const chatRef = useRef<dxChat | null>(null); |
| 39 | + |
| 40 | + const chatOptions = useMemo(() => ({ |
| 41 | + onInitialized: (e: ChatTypes.InitializedEvent) => { |
| 42 | + chatRef.current = e.component || null; |
| 43 | + }, |
| 44 | + user: { id: 'user' }, |
| 45 | + suggestions: { |
| 46 | + items: [ |
| 47 | + { text: '💡 Help', prompt: HELP_PROMPT }, |
| 48 | + { text: '🔍 Filter Sector by Health', prompt: 'Filter Sector by Health' }, |
| 49 | + { text: '↕️ Sort by Region', prompt: 'Sort by Region' }, |
| 50 | + { text: '🧩 Group by Product', prompt: 'Group by Product', width: 170 }, |
| 51 | + ], |
| 52 | + onItemClick: (e: ButtonGroupTypes.ItemClickEvent) => { |
| 53 | + const { prompt, text } = e.itemData; |
| 54 | + const userId = text === '💡 Help' ? 'help' : 'user'; |
| 55 | + |
| 56 | + const message = { |
| 57 | + id: Date.now(), |
| 58 | + timestamp: new Date(), |
| 59 | + author: { id: userId }, |
| 60 | + text: prompt, |
| 61 | + }; |
| 62 | + |
| 63 | + chatRef.current?.getDataSource().store().push([{ |
| 64 | + type: 'insert', |
| 65 | + data: message, |
| 66 | + }]); |
| 67 | + }, |
| 68 | + }, |
| 69 | + }), []); |
| 70 | + |
11 | 71 | return ( |
12 | | - <div className="main"> |
13 | | - <Button text={`Click count: ${count}`} onClick={clickHandler} /> |
| 72 | + <div className="demo-container"> |
| 73 | + <DataGrid |
| 74 | + id="grid-container" |
| 75 | + dataSource={sales} |
| 76 | + aiIntegration={service.columnAiIntegration} |
| 77 | + showBorders |
| 78 | + keyExpr="Id" |
| 79 | + filterSyncEnabled |
| 80 | + > |
| 81 | + <SearchPanel visible width={240} placeholder="Search..." /> |
| 82 | + <GroupPanel visible /> |
| 83 | + <HeaderFilter visible /> |
| 84 | + <FilterRow visible /> |
| 85 | + <Paging defaultPageSize={10} /> |
| 86 | + <Pager |
| 87 | + visible |
| 88 | + allowedPageSizes={allowedPageSizes} |
| 89 | + showPageSizeSelector |
| 90 | + /> |
| 91 | + |
| 92 | + <AIAssistant |
| 93 | + enabled |
| 94 | + aiIntegration={service.assistantAiIntegration} |
| 95 | + chat={chatOptions} |
| 96 | + /> |
| 97 | + |
| 98 | + <Column dataField="Product" /> |
| 99 | + <Column dataField="Amount" caption="Sale Amount" dataType="number" format="currency" /> |
| 100 | + <Column dataField="Region" dataType="string" /> |
| 101 | + <Column dataField="Sector" dataType="string" /> |
| 102 | + <Column dataField="SaleDate" dataType="date" /> |
| 103 | + <Column dataField="Customer" dataType="string" /> |
| 104 | + <Column |
| 105 | + name="AI column" |
| 106 | + caption="AI Column" |
| 107 | + type="ai" |
| 108 | + width={200} |
| 109 | + fixed |
| 110 | + fixedPosition="right" |
| 111 | + cssClass="ai-cell" |
| 112 | + > |
| 113 | + <AI |
| 114 | + prompt="Identify the country where product is manufactured. When looking up a country, consider Sector and Customers." |
| 115 | + mode="auto" |
| 116 | + noDataText="No data" |
| 117 | + /> |
| 118 | + </Column> |
| 119 | + </DataGrid> |
14 | 120 | </div> |
15 | 121 | ); |
16 | 122 | } |
|
0 commit comments