|
| 1 | +import { ConversationAction, ConversationItemGroup, ListConversationsResult } from '@aws/language-server-runtimes-types' |
| 2 | +import { ChatItemButton, DetailedList, DetailedListItem, MynahUI, TextBasedFormItem } from '@aws/mynah-ui' |
| 3 | +import { toMynahIcon } from '../utils' |
| 4 | +import { Messager } from '../messager' |
| 5 | + |
| 6 | +export const ChatHistory = { |
| 7 | + TabBarButtonId: 'history_sheet', |
| 8 | +} as const |
| 9 | + |
| 10 | +interface MynahDetailedList { |
| 11 | + update: (data: DetailedList) => void |
| 12 | + close: () => void |
| 13 | + changeTarget: (direction: 'up' | 'down', snapOnLastAndFirst?: boolean) => void |
| 14 | + getTargetElementId: () => string | undefined |
| 15 | +} |
| 16 | + |
| 17 | +export class ChatHistoryList { |
| 18 | + historyDetailedList: MynahDetailedList | undefined |
| 19 | + |
| 20 | + constructor( |
| 21 | + private mynahUi: MynahUI, |
| 22 | + private messager: Messager |
| 23 | + ) {} |
| 24 | + |
| 25 | + show(params: ListConversationsResult) { |
| 26 | + const detailedList = { |
| 27 | + header: params.header, |
| 28 | + filterOptions: params.filterOptions?.map(filter => ({ |
| 29 | + ...filter, |
| 30 | + icon: toMynahIcon(filter.icon), |
| 31 | + })), |
| 32 | + list: this.toConversationGroups(params.list), |
| 33 | + } |
| 34 | + // set auto focus on the 1st filter option item |
| 35 | + if (detailedList.filterOptions && detailedList.filterOptions.length > 0) { |
| 36 | + // we currently support only text-based items |
| 37 | + ;(detailedList.filterOptions[0] as TextBasedFormItem).autoFocus = true |
| 38 | + } |
| 39 | + |
| 40 | + if (this.historyDetailedList) { |
| 41 | + this.historyDetailedList.update(detailedList) |
| 42 | + } else { |
| 43 | + this.historyDetailedList = this.mynahUi.openDetailedList({ |
| 44 | + tabId: '', // TODO: remove after MynahUI is changed to remove the property |
| 45 | + detailedList: detailedList, |
| 46 | + events: { |
| 47 | + onFilterValueChange: this.onFilterValueChange, |
| 48 | + onKeyPress: this.onKeyPress, |
| 49 | + onItemSelect: this.onItemSelect, |
| 50 | + onActionClick: this.onActionClick, |
| 51 | + onClose: this.onClose, |
| 52 | + }, |
| 53 | + }) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + close() { |
| 58 | + this.historyDetailedList?.close() |
| 59 | + } |
| 60 | + |
| 61 | + private onFilterValueChange = (filterValues: Record<string, any>) => { |
| 62 | + this.messager.onListConversations(filterValues) |
| 63 | + } |
| 64 | + |
| 65 | + private onItemSelect = (item: DetailedListItem) => { |
| 66 | + if (!item.id) { |
| 67 | + throw new Error('Conversation id is not defined') |
| 68 | + } |
| 69 | + this.messager.onConversationClick(item.id) |
| 70 | + } |
| 71 | + |
| 72 | + private onActionClick = (action: ChatItemButton) => { |
| 73 | + const conversationAction = this.getConversationAction(action.text) |
| 74 | + this.messager.onConversationClick(action.id, conversationAction) |
| 75 | + } |
| 76 | + |
| 77 | + private onClose = () => { |
| 78 | + this.historyDetailedList = undefined |
| 79 | + } |
| 80 | + |
| 81 | + private onKeyPress = (e: KeyboardEvent) => { |
| 82 | + if (e.key === 'Escape') { |
| 83 | + this.close() |
| 84 | + } else if (e.key === 'Enter') { |
| 85 | + const targetElementId = this.historyDetailedList?.getTargetElementId() |
| 86 | + if (targetElementId) { |
| 87 | + this.onItemSelect({ |
| 88 | + id: targetElementId, |
| 89 | + }) |
| 90 | + } |
| 91 | + } else if (e.key === 'ArrowUp') { |
| 92 | + this.historyDetailedList?.changeTarget('up') |
| 93 | + } else if (e.key === 'ArrowDown') { |
| 94 | + this.historyDetailedList?.changeTarget('down') |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private toConversationGroups = (groups: ConversationItemGroup[]) => { |
| 99 | + return groups.map(group => ({ |
| 100 | + groupName: group.groupName, |
| 101 | + icon: toMynahIcon(group.icon), |
| 102 | + children: group.items?.map(item => ({ |
| 103 | + ...item, |
| 104 | + icon: toMynahIcon(item.icon), |
| 105 | + actions: item.actions?.map(action => ({ |
| 106 | + ...action, |
| 107 | + icon: toMynahIcon(action.icon), |
| 108 | + })), |
| 109 | + })), |
| 110 | + })) |
| 111 | + } |
| 112 | + |
| 113 | + private getConversationAction = (actionText: string | undefined): ConversationAction => { |
| 114 | + switch (actionText) { |
| 115 | + case 'Export': |
| 116 | + return 'export' |
| 117 | + case 'Delete': |
| 118 | + return 'delete' |
| 119 | + default: |
| 120 | + throw new Error(`Unsupported action: ${actionText}`) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments