new-chat.tsx 5.8 KB

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