All files / src/shared/FilePreview/components SearchSubHeaderV2.tsx

13.63% Statements 3/22
0% Branches 0/6
0% Functions 0/6
15% Lines 3/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140                                      2x                                                                                                                                                   2x                                                           2x                                
import useHighlightWord from "@common/FilePreview/hooks/useHighlightWord";
import useInputFieldFocusListener from "@common/FilePreview/hooks/useInputFieldFocusListener";
import { Stack } from "@mui/material";
import { CaretDownIcon, CaretUpIcon, MagnifyingGlassIcon, XIcon } from "@phosphor-icons/react";
import { GiveInput } from "@shared/GiveInputs/GiveInput";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import GiveText from "@shared/Text/GiveText";
import { styled, useAppTheme } from "@theme/v2/Provider";
import { debounce } from "lodash";
import React, { useRef, useState } from "react";
 
type SearchSubHeaderPropsV2 = {
  onClose: () => void;
  onSearch: (newValue: string) => void;
  highlights: NodeListOf<HTMLElement> | null;
  onFocus: (isFocused: boolean) => void;
  isDisabled?: boolean;
};
 
const SearchSubHeaderV2 = ({
  onClose,
  onSearch,
  highlights,
  onFocus,
  isDisabled,
}: SearchSubHeaderPropsV2) => {
  const [value, setValue] = useState<string>("");
  const { highlightIndex, onNext, onPrev } = useHighlightWord(highlights);
  const theme = useAppTheme();
 
  const inputRef = useRef<HTMLInputElement>(null);
 
  const debouncedSearch = useRef(
    debounce((value: string) => onSearch(value), 200),
  );
 
  const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const newValue = event.target.value;
    setValue(newValue);
    debouncedSearch.current.cancel();
    debouncedSearch.current(newValue);
  };
 
  useInputFieldFocusListener({ inputRef, onFocus, autofocus: true });
 
  const highlightsTrackerProps = {
    visible: !!value,
    current: highlights?.length ? highlightIndex + 1 : 0,
    total: highlights?.length || 0,
  };
 
  return (
    <Container
      onClick={(event) => event.stopPropagation()}
      data-testid="search-container"
    >
      <GiveInput
        value={value}
        onChange={onChange}
        inputRef={inputRef}
        placeholder="Find in document"
        rightContent={<HighlightTracker {...highlightsTrackerProps} />}
        leftContent={
          <MagnifyingGlassIcon
            size={20}
            color={theme.palette.text?.["secondary"]}
          />
        }
        autoFocus
      />
      <Stack direction="row" gap="4px" alignItems="center">
        <GiveIconButton
          Icon={CaretDownIcon}
          size="small"
          variant="ghost"
          onClick={onPrev}
          disabled={isDisabled}
        />
        <GiveIconButton
          Icon={CaretUpIcon}
          size="small"
          variant="ghost"
          onClick={onNext}
          disabled={isDisabled}
        />
      </Stack>
      <GiveIconButton Icon={XIcon} size="small" variant="ghost" onClick={onClose} />
    </Container>
  );
};
 
export default SearchSubHeaderV2;
 
const Container = styled(Stack)(({ theme }) => ({
  position: "absolute",
  top: 0,
  right: "16px",
  zIndex: 1050,
  padding: "8px",
  flexDirection: "row",
  gap: "8px",
  alignItems: "center",
  borderRadius: "16px",
  backgroundColor: theme.palette.surface?.["secondary-transparent"],
  backdropFilter: "blur(15px)",
  WebkitBackdropFilter: "blur(15px)",
 
  "& .MuiTextField-root": {
    width: "270px",
    height: "48px",
    "& .MuiInputAdornment-positionEnd": {
      width: "35px",
    },
  },
 
  [theme.breakpoints.down("sm")]: {
    right: "8px",
    "& .MuiTextField-root": {
      width: "200px",
    },
  },
}));
 
const HighlightTracker = ({
  visible,
  current,
  total,
}: {
  visible: boolean;
  current: number;
  total: number;
}) => {
  if (!visible) return <></>;
  return (
    <GiveText variant="bodyS" color="secondary">
      {current}/{total}
    </GiveText>
  );
};