new-chat.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { useEffect, useRef, useState } from "react";
  2. import { Path, SlotID } from "../constant";
  3. import { IconButton } from "./button";
  4. import { EmojiAvatar } from "./emoji";
  5. import styles from "./new-chat.module.scss";
  6. import LeftIcon from "../icons/left.svg";
  7. import AddIcon from "../icons/lightning.svg";
  8. import { useLocation, useNavigate } from "react-router-dom";
  9. import { createEmptyMask, Mask, useMaskStore } from "../store/mask";
  10. import Locale from "../locales";
  11. import { useAppConfig, useChatStore } from "../store";
  12. import { MaskAvatar } from "./mask";
  13. function getIntersectionArea(aRect: DOMRect, bRect: DOMRect) {
  14. const xmin = Math.max(aRect.x, bRect.x);
  15. const xmax = Math.min(aRect.x + aRect.width, bRect.x + bRect.width);
  16. const ymin = Math.max(aRect.y, bRect.y);
  17. const ymax = Math.min(aRect.y + aRect.height, bRect.y + bRect.height);
  18. const width = xmax - xmin;
  19. const height = ymax - ymin;
  20. const intersectionArea = width < 0 || height < 0 ? 0 : width * height;
  21. return intersectionArea;
  22. }
  23. function MaskItem(props: { mask: Mask; onClick?: () => void }) {
  24. const domRef = useRef<HTMLDivElement>(null);
  25. useEffect(() => {
  26. const changeOpacity = () => {
  27. const dom = domRef.current;
  28. const parent = document.getElementById(SlotID.AppBody);
  29. if (!parent || !dom) return;
  30. const domRect = dom.getBoundingClientRect();
  31. const parentRect = parent.getBoundingClientRect();
  32. const intersectionArea = getIntersectionArea(domRect, parentRect);
  33. const domArea = domRect.width * domRect.height;
  34. const ratio = intersectionArea / domArea;
  35. const opacity = ratio > 0.9 ? 1 : 0.4;
  36. dom.style.opacity = opacity.toString();
  37. };
  38. setTimeout(changeOpacity, 30);
  39. window.addEventListener("resize", changeOpacity);
  40. return () => window.removeEventListener("resize", changeOpacity);
  41. }, [domRef]);
  42. return (
  43. <div className={styles["mask"]} ref={domRef} onClick={props.onClick}>
  44. <MaskAvatar mask={props.mask} />
  45. <div className={styles["mask-name"] + " one-line"}>{props.mask.name}</div>
  46. </div>
  47. );
  48. }
  49. function useMaskGroup(masks: Mask[]) {
  50. const [groups, setGroups] = useState<Mask[][]>([]);
  51. useEffect(() => {
  52. const appBody = document.getElementById(SlotID.AppBody);
  53. if (!appBody || masks.length === 0) return;
  54. const rect = appBody.getBoundingClientRect();
  55. const maxWidth = rect.width;
  56. const maxHeight = rect.height * 0.6;
  57. const maskItemWidth = 120;
  58. const maskItemHeight = 50;
  59. const randomMask = () => masks[Math.floor(Math.random() * masks.length)];
  60. let maskIndex = 0;
  61. const nextMask = () => masks[maskIndex++ % masks.length];
  62. const rows = Math.ceil(maxHeight / maskItemHeight);
  63. const cols = Math.ceil(maxWidth / maskItemWidth);
  64. const newGroups = new Array(rows)
  65. .fill(0)
  66. .map((_, _i) =>
  67. new Array(cols)
  68. .fill(0)
  69. .map((_, j) => (j < 1 || j > cols - 2 ? randomMask() : nextMask())),
  70. );
  71. setGroups(newGroups);
  72. // eslint-disable-next-line react-hooks/exhaustive-deps
  73. }, []);
  74. return groups;
  75. }
  76. export function NewChat() {
  77. const chatStore = useChatStore();
  78. const maskStore = useMaskStore();
  79. const masks = maskStore.getAll();
  80. const groups = useMaskGroup(masks);
  81. const navigate = useNavigate();
  82. const config = useAppConfig();
  83. const { state } = useLocation();
  84. const startChat = (mask?: Mask) => {
  85. chatStore.newSession(mask);
  86. navigate(Path.Chat);
  87. };
  88. return (
  89. <div className={styles["new-chat"]}>
  90. <div className={styles["mask-header"]}>
  91. <IconButton
  92. icon={<LeftIcon />}
  93. text={Locale.NewChat.Return}
  94. onClick={() => navigate(Path.Home)}
  95. ></IconButton>
  96. {!state?.fromHome && (
  97. <IconButton
  98. text={Locale.NewChat.NotShow}
  99. onClick={() => {
  100. if (confirm(Locale.NewChat.ConfirmNoShow)) {
  101. startChat();
  102. config.update(
  103. (config) => (config.dontShowMaskSplashScreen = true),
  104. );
  105. }
  106. }}
  107. ></IconButton>
  108. )}
  109. </div>
  110. <div className={styles["mask-cards"]}>
  111. <div className={styles["mask-card"]}>
  112. <EmojiAvatar avatar="1f606" size={24} />
  113. </div>
  114. <div className={styles["mask-card"]}>
  115. <EmojiAvatar avatar="1f916" size={24} />
  116. </div>
  117. <div className={styles["mask-card"]}>
  118. <EmojiAvatar avatar="1f479" size={24} />
  119. </div>
  120. </div>
  121. <div className={styles["title"]}>{Locale.NewChat.Title}</div>
  122. <div className={styles["sub-title"]}>{Locale.NewChat.SubTitle}</div>
  123. <div className={styles["actions"]}>
  124. <input
  125. className={styles["search-bar"]}
  126. placeholder={Locale.NewChat.More}
  127. type="text"
  128. onClick={() => navigate(Path.Masks)}
  129. />
  130. <IconButton
  131. text={Locale.NewChat.Skip}
  132. onClick={() => startChat()}
  133. icon={<AddIcon />}
  134. type="primary"
  135. shadow
  136. />
  137. </div>
  138. <div className={styles["masks"]}>
  139. {groups.map((masks, i) => (
  140. <div key={i} className={styles["mask-row"]}>
  141. {masks.map((mask, index) => (
  142. <MaskItem
  143. key={index}
  144. mask={mask}
  145. onClick={() => startChat(mask)}
  146. />
  147. ))}
  148. </div>
  149. ))}
  150. </div>
  151. </div>
  152. );
  153. }