@difizen/magent-au
Version:
119 lines • 6.28 kB
JavaScript
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { CaretRightOutlined, DeleteOutlined } from '@ant-design/icons';
import { useInject, ViewInstance } from '@difizen/mana-app';
import { Input, Popconfirm } from 'antd';
import classNames from 'classnames';
import { useLayoutEffect, useRef, useState } from 'react';
import "./index.less";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
var TextArea = Input.TextArea;
export var ConversationItem = function ConversationItem(_ref) {
var _sessionsInstance$act;
var session = _ref.session;
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
editing = _useState2[0],
setEditing = _useState2[1];
var _useState3 = useState(false),
_useState4 = _slicedToArray(_useState3, 2),
deleteConfirmOpen = _useState4[0],
setDeleteConfirmOpen = _useState4[1];
var _useState5 = useState(false),
_useState6 = _slicedToArray(_useState5, 2),
isMouseOver = _useState6[0],
setIsMouseOver = _useState6[1];
var sessionsInstance = useInject(ViewInstance);
var ref = useRef(null);
var changeTitle = function changeTitle() {
var _ref$current;
var newTitle = (_ref$current = ref.current) === null || _ref$current === void 0 || (_ref$current = _ref$current.resizableTextArea) === null || _ref$current === void 0 ? void 0 : _ref$current.textArea.value.trim();
if (newTitle) {
// convUpdateTitle(conversation, newTitle);
// TODO: change title
}
setEditing(false);
};
useLayoutEffect(function () {
if (editing) {
var _ref$current2, _ref$current3;
(_ref$current2 = ref.current) === null || _ref$current2 === void 0 || (_ref$current2 = _ref$current2.resizableTextArea) === null || _ref$current2 === void 0 || _ref$current2.textArea.focus();
(_ref$current3 = ref.current) === null || _ref$current3 === void 0 || (_ref$current3 = _ref$current3.resizableTextArea) === null || _ref$current3 === void 0 || _ref$current3.textArea.setSelectionRange(30, 30);
}
}, [editing]);
var isActive = session.id === ((_sessionsInstance$act = sessionsInstance.active) === null || _sessionsInstance$act === void 0 ? void 0 : _sessionsInstance$act.id);
return /*#__PURE__*/_jsx("div", {
className: classNames('chat-history-item', isActive && 'chat-history-item-active', deleteConfirmOpen && 'chat-history-item-show-action'),
onMouseEnter: function onMouseEnter() {
setIsMouseOver(true);
},
onMouseLeave: function onMouseLeave() {
setIsMouseOver(false);
},
onClick: function onClick() {
sessionsInstance.selectSession(session);
},
children: editing ? /*#__PURE__*/_jsx(TextArea, {
ref: ref,
defaultValue: session.id,
onBlur: changeTitle,
maxLength: 30,
onPressEnter: function onPressEnter(e) {
e.preventDefault();
if (e.keyCode !== 229) {
var _ref$current4;
if ((_ref$current4 = ref.current) !== null && _ref$current4 !== void 0 && (_ref$current4 = _ref$current4.resizableTextArea) !== null && _ref$current4 !== void 0 && _ref$current4.textArea.value) {
changeTitle();
}
// 229 为中文输入法下按enter,不应触发
}
},
autoSize: {
maxRows: 5
}
}) : /*#__PURE__*/_jsxs(_Fragment, {
children: [/*#__PURE__*/_jsxs("div", {
className: 'chat-history-item-title',
title: session.id,
children: [isActive && session.id && /*#__PURE__*/_jsx(CaretRightOutlined, {}), session.previewTitle]
}), isMouseOver ?
/*#__PURE__*/
// <EditOutlined
// className={'chat-history-item-i'}
// onClick={(e) => {
// e.stopPropagation();
// setEditing(true);
// }}
// />
_jsx(_Fragment, {}) : /*#__PURE__*/_jsx("div", {
className: 'chat-history-item-time',
children: session.gmtCreate.format('HH:mm:ss')
}), isMouseOver && /*#__PURE__*/_jsx(Popconfirm, {
placement: "topRight",
title: '你确定删除该会话吗?',
onConfirm: function onConfirm(e) {
e === null || e === void 0 || e.stopPropagation();
sessionsInstance.deleteSession(session);
},
onOpenChange: function onOpenChange(open) {
setDeleteConfirmOpen(open);
},
onCancel: function onCancel(e) {
e === null || e === void 0 || e.stopPropagation();
},
children: /*#__PURE__*/_jsx(DeleteOutlined, {
className: 'chat-history-item-i',
onClick: function onClick(e) {
e.stopPropagation();
}
})
})]
})
});
};