chat-list.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import DeleteIcon from "../icons/delete.svg";
  2. import BotIcon from "../icons/bot.svg";
  3. import styles from "./home.module.scss";
  4. import {
  5. DragDropContext,
  6. Droppable,
  7. Draggable,
  8. OnDragEndResponder,
  9. } from "@hello-pangea/dnd";
  10. import { useChatStore } from "../store";
  11. import Locale from "../locales";
  12. import { Link, useNavigate } from "react-router-dom";
  13. import { Path } from "../constant";
  14. import { MaskAvatar } from "./mask";
  15. import { Mask } from "../store/mask";
  16. export function ChatItem(props: {
  17. onClick?: () => void;
  18. onDelete?: () => void;
  19. title: string;
  20. count: number;
  21. time: string;
  22. selected: boolean;
  23. id: number;
  24. index: number;
  25. narrow?: boolean;
  26. mask: Mask;
  27. }) {
  28. return (
  29. <Draggable draggableId={`${props.id}`} index={props.index}>
  30. {(provided) => (
  31. <div
  32. className={`${styles["chat-item"]} ${
  33. props.selected && styles["chat-item-selected"]
  34. }`}
  35. onClick={props.onClick}
  36. ref={provided.innerRef}
  37. {...provided.draggableProps}
  38. {...provided.dragHandleProps}
  39. title={`${props.title}\n${Locale.ChatItem.ChatItemCount(
  40. props.count,
  41. )}`}
  42. >
  43. {props.narrow ? (
  44. <div className={styles["chat-item-narrow"]}>
  45. <div className={styles["chat-item-avatar"] + " no-dark"}>
  46. <MaskAvatar mask={props.mask} />
  47. </div>
  48. <div className={styles["chat-item-narrow-count"]}>
  49. {props.count}
  50. </div>
  51. </div>
  52. ) : (
  53. <>
  54. <div className={styles["chat-item-title"]}>{props.title}</div>
  55. <div className={styles["chat-item-info"]}>
  56. <div className={styles["chat-item-count"]}>
  57. {Locale.ChatItem.ChatItemCount(props.count)}
  58. </div>
  59. <div className={styles["chat-item-date"]}>
  60. {new Date(props.time).toLocaleString()}
  61. </div>
  62. </div>
  63. </>
  64. )}
  65. <div className={styles["chat-item-delete"]} onClick={props.onDelete}>
  66. <DeleteIcon />
  67. </div>
  68. </div>
  69. )}
  70. </Draggable>
  71. );
  72. }
  73. export function ChatList(props: { narrow?: boolean }) {
  74. const [sessions, selectedIndex, selectSession, removeSession, moveSession] =
  75. useChatStore((state) => [
  76. state.sessions,
  77. state.currentSessionIndex,
  78. state.selectSession,
  79. state.removeSession,
  80. state.moveSession,
  81. ]);
  82. const chatStore = useChatStore();
  83. const navigate = useNavigate();
  84. const onDragEnd: OnDragEndResponder = (result) => {
  85. const { destination, source } = result;
  86. if (!destination) {
  87. return;
  88. }
  89. if (
  90. destination.droppableId === source.droppableId &&
  91. destination.index === source.index
  92. ) {
  93. return;
  94. }
  95. moveSession(source.index, destination.index);
  96. };
  97. return (
  98. <DragDropContext onDragEnd={onDragEnd}>
  99. <Droppable droppableId="chat-list">
  100. {(provided) => (
  101. <div
  102. className={styles["chat-list"]}
  103. ref={provided.innerRef}
  104. {...provided.droppableProps}
  105. >
  106. {sessions.map((item, i) => (
  107. <ChatItem
  108. title={item.topic}
  109. time={new Date(item.lastUpdate).toLocaleString()}
  110. count={item.messages.length}
  111. key={item.id}
  112. id={item.id}
  113. index={i}
  114. selected={i === selectedIndex}
  115. onClick={() => {
  116. navigate(Path.Chat);
  117. selectSession(i);
  118. }}
  119. onDelete={() => {
  120. if (!props.narrow || confirm(Locale.Home.DeleteChat)) {
  121. chatStore.deleteSession(i);
  122. }
  123. }}
  124. narrow={props.narrow}
  125. mask={item.mask}
  126. />
  127. ))}
  128. {provided.placeholder}
  129. </div>
  130. )}
  131. </Droppable>
  132. </DragDropContext>
  133. );
  134. }