SuggestionFocus.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { useEffect, useRef, useState } from "react";
  2. import { useAppSelector } from "../../../hooks";
  3. import { prInfo } from "../../../reducers/pr-load";
  4. interface IWidget {
  5. book: number;
  6. para: number;
  7. start: number;
  8. end: number;
  9. channelId: string;
  10. children?: React.ReactNode;
  11. }
  12. const SuggestionFocusWidget = ({
  13. book,
  14. para,
  15. start,
  16. end,
  17. channelId,
  18. children,
  19. }: IWidget) => {
  20. const pr = useAppSelector(prInfo);
  21. const [highlight, setHighlight] = useState(false);
  22. const divRef = useRef<HTMLDivElement>(null);
  23. useEffect(() => {
  24. if (pr) {
  25. if (
  26. book === pr.book &&
  27. para === pr.paragraph &&
  28. start === pr.word_start &&
  29. end === pr.word_end &&
  30. channelId === pr.channel.id
  31. ) {
  32. setHighlight(true);
  33. divRef.current?.scrollIntoView({
  34. behavior: "smooth",
  35. block: "center",
  36. inline: "nearest",
  37. });
  38. } else {
  39. setHighlight(false);
  40. }
  41. } else {
  42. setHighlight(false);
  43. }
  44. }, [book, channelId, end, para, pr, start]);
  45. return (
  46. <div
  47. ref={divRef}
  48. style={{
  49. backgroundColor: highlight ? "rgb(255 255 0 / 20%)" : undefined,
  50. width: "100%",
  51. }}
  52. >
  53. {children}
  54. </div>
  55. );
  56. };
  57. export default SuggestionFocusWidget;