|
| 1 | +// istanbul ignore file: experimental |
| 2 | +import React, { useMemo, useState } from "react"; |
| 3 | +import { createDecorators, type EventEmitter } from "@next-core/element"; |
| 4 | +import { ReactNextElement, wrapBrick } from "@next-core/react-element"; |
| 5 | +import moment from "moment"; |
| 6 | +import type { Link, LinkProps } from "@next-bricks/basic/link"; |
| 7 | +import type { |
| 8 | + ActionType, |
| 9 | + EoMiniActions, |
| 10 | + EoMiniActionsEvents, |
| 11 | + EoMiniActionsEventsMapping, |
| 12 | + EoMiniActionsProps, |
| 13 | + SimpleActionType, |
| 14 | +} from "@next-bricks/basic/mini-actions"; |
| 15 | +import "@next-core/theme"; |
| 16 | +import { initializeI18n } from "@next-core/i18n"; |
| 17 | +import { K, NS, locales, t } from "./i18n.js"; |
| 18 | +import styleText from "./styles.shadow.css"; |
| 19 | +import type { TaskState } from "../cruise-canvas/interfaces.js"; |
| 20 | +import classNames from "classnames"; |
| 21 | +import { DONE_STATES } from "../cruise-canvas/constants.js"; |
| 22 | + |
| 23 | +initializeI18n(NS, locales); |
| 24 | + |
| 25 | +const WrappedLink = wrapBrick<Link, LinkProps>("eo-link"); |
| 26 | +const WrappedMiniActions = wrapBrick< |
| 27 | + EoMiniActions, |
| 28 | + EoMiniActionsProps, |
| 29 | + EoMiniActionsEvents, |
| 30 | + EoMiniActionsEventsMapping |
| 31 | +>("eo-mini-actions", { |
| 32 | + onActionClick: "action.click", |
| 33 | + onVisibleChange: "visible.change", |
| 34 | +}); |
| 35 | + |
| 36 | +const { defineElement, property, event } = createDecorators(); |
| 37 | + |
| 38 | +export interface ChatHistoryProps { |
| 39 | + list?: HistoryItem[]; |
| 40 | + actions?: ActionType[]; |
| 41 | +} |
| 42 | + |
| 43 | +export interface HistoryItem { |
| 44 | + id: string; |
| 45 | + title: string; |
| 46 | + startTime: number; |
| 47 | + state?: TaskState; |
| 48 | +} |
| 49 | + |
| 50 | +export interface ActionClickDetail { |
| 51 | + action: SimpleActionType; |
| 52 | + item: HistoryItem; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * 构件 `ai-portal.chat-history` |
| 57 | + */ |
| 58 | +export |
| 59 | +@defineElement("ai-portal.chat-history", { |
| 60 | + styleTexts: [styleText], |
| 61 | +}) |
| 62 | +class ChatHistory extends ReactNextElement implements ChatHistoryProps { |
| 63 | + @property({ attribute: false }) |
| 64 | + accessor list: HistoryItem[] | undefined; |
| 65 | + |
| 66 | + @property({ attribute: false }) |
| 67 | + accessor actions: ActionType[] | undefined; |
| 68 | + |
| 69 | + @event({ type: "action.click" }) |
| 70 | + accessor #actionClick!: EventEmitter<ActionClickDetail>; |
| 71 | + |
| 72 | + #handleActionClick = (detail: ActionClickDetail) => { |
| 73 | + this.#actionClick.emit(detail); |
| 74 | + }; |
| 75 | + |
| 76 | + render() { |
| 77 | + return ( |
| 78 | + <ChatHistoryComponent |
| 79 | + list={this.list} |
| 80 | + actions={this.actions} |
| 81 | + onActionClick={this.#handleActionClick} |
| 82 | + /> |
| 83 | + ); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +export interface ChatHistoryComponentProps extends ChatHistoryProps { |
| 88 | + onActionClick?: (detail: ActionClickDetail) => void; |
| 89 | +} |
| 90 | + |
| 91 | +interface GroupedHistory { |
| 92 | + title: string; |
| 93 | + items: HistoryItem[]; |
| 94 | +} |
| 95 | + |
| 96 | +export function ChatHistoryComponent({ |
| 97 | + list, |
| 98 | + actions, |
| 99 | + onActionClick, |
| 100 | +}: ChatHistoryComponentProps) { |
| 101 | + const groups = useMemo(() => { |
| 102 | + const groupMap = new Map<string, GroupedHistory>(); |
| 103 | + // Group history by |
| 104 | + // - today |
| 105 | + // - yesterday |
| 106 | + // - previous 7 days |
| 107 | + // - previous 30 days |
| 108 | + // - each month this year |
| 109 | + // - each year before. |
| 110 | + const now = moment(); |
| 111 | + const startOfDay = now.startOf("day"); |
| 112 | + const yesterday = startOfDay.clone().subtract(1, "day"); |
| 113 | + const sevenDaysAgo = startOfDay.clone().subtract(7, "days"); |
| 114 | + const thirtyDaysAgo = startOfDay.clone().subtract(30, "days"); |
| 115 | + const thisYear = now.year(); |
| 116 | + |
| 117 | + const timestamps = { |
| 118 | + startOfDay: +startOfDay / 1000, |
| 119 | + yesterday: +yesterday / 1000, |
| 120 | + sevenDaysAgo: +sevenDaysAgo / 1000, |
| 121 | + thirtyDaysAgo: +thirtyDaysAgo / 1000, |
| 122 | + thisYear, |
| 123 | + }; |
| 124 | + for (const item of list ?? []) { |
| 125 | + let groupKey: string; |
| 126 | + if (item.startTime >= timestamps.startOfDay) { |
| 127 | + groupKey = t(K.TODAY); |
| 128 | + } else if (item.startTime >= timestamps.yesterday) { |
| 129 | + groupKey = t(K.YESTERDAY); |
| 130 | + } else if (item.startTime >= timestamps.sevenDaysAgo) { |
| 131 | + groupKey = t(K.PREVIOUS_7_DAYS); |
| 132 | + } else if (item.startTime >= timestamps.thirtyDaysAgo) { |
| 133 | + groupKey = t(K.PREVIOUS_30_DAYS); |
| 134 | + } else if (item.startTime >= timestamps.thisYear) { |
| 135 | + groupKey = moment(item.startTime * 1000).format("MMMM"); |
| 136 | + } else { |
| 137 | + groupKey = moment(item.startTime * 1000).format("YYYY"); |
| 138 | + } |
| 139 | + let group = groupMap.get(groupKey); |
| 140 | + if (!group) { |
| 141 | + groupMap.set(groupKey, (group = { title: groupKey, items: [] })); |
| 142 | + } |
| 143 | + group.items.push(item); |
| 144 | + } |
| 145 | + |
| 146 | + return [...groupMap.values()]; |
| 147 | + }, [list]); |
| 148 | + |
| 149 | + const [actionsVisible, setActionsVisible] = useState<string | null>(null); |
| 150 | + |
| 151 | + return ( |
| 152 | + <ul> |
| 153 | + {groups.map((group) => ( |
| 154 | + <li key={group.title} className="group"> |
| 155 | + <div className="group-title">{group.title}</div> |
| 156 | + <ul className="items"> |
| 157 | + {group.items.map((item) => ( |
| 158 | + <li key={item.id}> |
| 159 | + <WrappedLink |
| 160 | + className={classNames("item", { |
| 161 | + active: actionsVisible === item.id, |
| 162 | + })} |
| 163 | + url={`/${item.id}`} |
| 164 | + > |
| 165 | + <div className="item-title">{item.title}</div> |
| 166 | + <WrappedMiniActions |
| 167 | + className="actions" |
| 168 | + actions={actions} |
| 169 | + onActionClick={(e) => { |
| 170 | + onActionClick?.({ action: e.detail, item }); |
| 171 | + }} |
| 172 | + onVisibleChange={(e) => { |
| 173 | + setActionsVisible(e.detail ? item.id : null); |
| 174 | + }} |
| 175 | + /> |
| 176 | + {!DONE_STATES.includes(item.state!) && ( |
| 177 | + <div className="working"></div> |
| 178 | + )} |
| 179 | + </WrappedLink> |
| 180 | + </li> |
| 181 | + ))} |
| 182 | + </ul> |
| 183 | + </li> |
| 184 | + ))} |
| 185 | + </ul> |
| 186 | + ); |
| 187 | +} |
0 commit comments