|
| 1 | +import React, { useCallback, useRef } from 'react'; |
| 2 | + |
| 3 | +import type dxChat from 'devextreme/ui/chat'; |
| 4 | +import type { InitializedEvent, Properties as ChatProperties } from 'devextreme/ui/chat'; |
| 5 | +import type { ItemClickEvent } from 'devextreme/ui/button_group'; |
| 6 | + |
| 7 | +import DataGrid, { |
| 8 | + Column, |
| 9 | + Paging, |
| 10 | + Pager, |
| 11 | + SearchPanel, |
| 12 | + GroupPanel, |
| 13 | + HeaderFilter, |
| 14 | + FilterRow, |
| 15 | + AIAssistant, |
| 16 | +} from 'devextreme-react/data-grid'; |
| 17 | + |
| 18 | +import { sales } from './data.ts'; |
| 19 | +import { aiIntegration } from './service.ts'; |
| 20 | + |
| 21 | +const allowedPageSizes = [10, 25, 50, 100]; |
| 22 | + |
| 23 | +const suggestions = { |
| 24 | + items: [ |
| 25 | + { |
| 26 | + text: '💡 Help', |
| 27 | + prompt: `💡 The DataGrid AI Assistant allows you to control the component using natural language. You can execute commands such as the following: |
| 28 | + • Sort records |
| 29 | + • Apply a filter |
| 30 | + • Search for a specific value |
| 31 | + • Group records by a field |
| 32 | + • Focus and select rows |
| 33 | + • Modify paging settings |
| 34 | + • Pin, resize, and reorder columns |
| 35 | + • Configure data summaries |
| 36 | + • Pick a suggestion or enter a custom request to get started.`, |
| 37 | + }, |
| 38 | + { |
| 39 | + text: '🔍 Filter Sector by Health', |
| 40 | + prompt: 'Filter Sector by Health', |
| 41 | + }, |
| 42 | + { |
| 43 | + text: '↕️ Sort by Region', |
| 44 | + prompt: 'Sort by Region', |
| 45 | + }, |
| 46 | + { |
| 47 | + text: '🧩 Group by Product', |
| 48 | + prompt: 'Group by Product', |
| 49 | + width: 170, |
| 50 | + }, |
| 51 | + ], |
| 52 | +}; |
| 53 | + |
| 54 | +export default function App() { |
| 55 | + const chatRef = useRef<dxChat | null>(null); |
| 56 | + |
| 57 | + const onChatInitialized = useCallback((e: InitializedEvent) => { |
| 58 | + chatRef.current = e.component ?? null; |
| 59 | + }, []); |
| 60 | + |
| 61 | + const onSuggestionItemClick = useCallback((e: ItemClickEvent) => { |
| 62 | + const { prompt, text } = e.itemData; |
| 63 | + const userId = text === '💡 Help' ? 'help' : 'user'; |
| 64 | + |
| 65 | + const message = { |
| 66 | + id: Date.now(), |
| 67 | + timestamp: new Date(), |
| 68 | + author: { id: userId }, |
| 69 | + text: prompt, |
| 70 | + }; |
| 71 | + |
| 72 | + chatRef.current?.getDataSource().store().push([{ |
| 73 | + type: 'insert', |
| 74 | + data: message, |
| 75 | + }]); |
| 76 | + }, []); |
| 77 | + |
| 78 | + const chatOptions: ChatProperties = { |
| 79 | + onInitialized: onChatInitialized, |
| 80 | + user: { |
| 81 | + id: 'user', |
| 82 | + }, |
| 83 | + suggestions: { |
| 84 | + ...suggestions, |
| 85 | + onItemClick: onSuggestionItemClick, |
| 86 | + }, |
| 87 | + }; |
| 88 | + |
| 89 | + return ( |
| 90 | + <DataGrid |
| 91 | + id="gridContainer" |
| 92 | + dataSource={sales} |
| 93 | + showBorders |
| 94 | + keyExpr="Id" |
| 95 | + filterSyncEnabled |
| 96 | + > |
| 97 | + <SearchPanel |
| 98 | + visible |
| 99 | + width={240} |
| 100 | + placeholder="Search..." |
| 101 | + /> |
| 102 | + <GroupPanel visible /> |
| 103 | + <HeaderFilter visible /> |
| 104 | + <FilterRow visible /> |
| 105 | + <Paging pageSize={10} /> |
| 106 | + <Pager |
| 107 | + visible |
| 108 | + allowedPageSizes={allowedPageSizes} |
| 109 | + showPageSizeSelector |
| 110 | + /> |
| 111 | + <AIAssistant |
| 112 | + enabled |
| 113 | + aiIntegration={aiIntegration} |
| 114 | + chat={chatOptions} |
| 115 | + /> |
| 116 | + <Column dataField="Product" /> |
| 117 | + <Column |
| 118 | + dataField="Amount" |
| 119 | + caption="Sale Amount" |
| 120 | + dataType="number" |
| 121 | + format="currency" |
| 122 | + /> |
| 123 | + <Column |
| 124 | + dataField="Region" |
| 125 | + dataType="string" |
| 126 | + /> |
| 127 | + <Column |
| 128 | + dataField="Sector" |
| 129 | + dataType="string" |
| 130 | + /> |
| 131 | + <Column |
| 132 | + dataField="SaleDate" |
| 133 | + dataType="date" |
| 134 | + /> |
| 135 | + <Column |
| 136 | + dataField="Customer" |
| 137 | + dataType="string" |
| 138 | + /> |
| 139 | + </DataGrid> |
| 140 | + ); |
| 141 | +} |
0 commit comments