chat-list.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. import { useRef, useEffect } from "react";
  17. import { showConfirm } from "./ui-lib";
  18. export function ChatItem(props: {
  19. onClick?: () => void;
  20. onDelete?: () => void;
  21. title: string;
  22. count: number;
  23. time: string;
  24. selected: boolean;
  25. id: string;
  26. index: number;
  27. narrow?: boolean;
  28. mask: Mask;
  29. }) {
  30. const draggableRef = useRef<HTMLDivElement | null>(null);
  31. useEffect(() => {
  32. if (props.selected && draggableRef.current) {
  33. draggableRef.current?.scrollIntoView({
  34. block: "center",
  35. });
  36. }
  37. }, [props.selected]);
  38. const modelConfig = useChatStore().extractModelConfig(props.mask.config);
  39. return (
  40. <Draggable draggableId={`${props.id}`} index={props.index}>
  41. {(provided) => (
  42. <div
  43. className={`${styles["chat-item"]} ${
  44. props.selected && styles["chat-item-selected"]
  45. }`}
  46. onClick={props.onClick}
  47. ref={(ele) => {
  48. draggableRef.current = ele;
  49. provided.innerRef(ele);
  50. }}
  51. {...provided.draggableProps}
  52. {...provided.dragHandleProps}
  53. title={`${props.title}\n${Locale.ChatItem.ChatItemCount(
  54. props.count,
  55. )}`}
  56. >
  57. {props.narrow ? (
  58. <div className={styles["chat-item-narrow"]}>
  59. <div className={styles["chat-item-avatar"] + " no-dark"}>
  60. <MaskAvatar
  61. avatar={props.mask.avatar}
  62. model={modelConfig.model}
  63. />
  64. </div>
  65. <div className={styles["chat-item-narrow-count"]}>
  66. {props.count}
  67. </div>
  68. </div>
  69. ) : (
  70. <>
  71. <div className={styles["chat-item-title"]}>{props.title}</div>
  72. <div className={styles["chat-item-info"]}>
  73. <div className={styles["chat-item-count"]}>
  74. {Locale.ChatItem.ChatItemCount(props.count)}
  75. </div>
  76. <div className={styles["chat-item-date"]}>{props.time}</div>
  77. </div>
  78. </>
  79. )}
  80. <div
  81. className={styles["chat-item-delete"]}
  82. onClickCapture={props.onDelete}
  83. >
  84. <DeleteIcon />
  85. </div>
  86. </div>
  87. )}
  88. </Draggable>
  89. );
  90. }
  91. export function ChatList(props: { narrow?: boolean }) {
  92. const [sessions, selectedIndex, selectSession, moveSession] = useChatStore(
  93. (state) => [
  94. state.sessions,
  95. state.currentSessionIndex,
  96. state.selectSession,
  97. state.moveSession,
  98. ],
  99. );
  100. const chatStore = useChatStore();
  101. const navigate = useNavigate();
  102. const onDragEnd: OnDragEndResponder = (result) => {
  103. const { destination, source } = result;
  104. if (!destination) {
  105. return;
  106. }
  107. if (
  108. destination.droppableId === source.droppableId &&
  109. destination.index === source.index
  110. ) {
  111. return;
  112. }
  113. moveSession(source.index, destination.index);
  114. };
  115. return (
  116. <DragDropContext onDragEnd={onDragEnd}>
  117. <Droppable droppableId="chat-list">
  118. {(provided) => (
  119. <div
  120. className={styles["chat-list"]}
  121. ref={provided.innerRef}
  122. {...provided.droppableProps}
  123. >
  124. {sessions.map((item, i) => (
  125. <ChatItem
  126. title={item.topic}
  127. time={new Date(item.lastUpdate).toLocaleString()}
  128. count={item.messages.length}
  129. key={item.id}
  130. id={item.id}
  131. index={i}
  132. selected={i === selectedIndex}
  133. onClick={() => {
  134. navigate(Path.Chat);
  135. selectSession(i);
  136. }}
  137. onDelete={async () => {
  138. if (
  139. !props.narrow ||
  140. (await showConfirm(Locale.Home.DeleteChat))
  141. ) {
  142. chatStore.deleteSession(i);
  143. }
  144. }}
  145. narrow={props.narrow}
  146. mask={item.mask}
  147. />
  148. ))}
  149. {provided.placeholder}
  150. </div>
  151. )}
  152. </Droppable>
  153. </DragDropContext>
  154. );
  155. }