diff --git a/components/hearing/Transcriptions.tsx b/components/hearing/Transcriptions.tsx index 504ccf738..9943a453b 100644 --- a/components/hearing/Transcriptions.tsx +++ b/components/hearing/Transcriptions.tsx @@ -4,6 +4,8 @@ import React, { useCallback, useEffect, useState } from "react" import styled from "styled-components" import { Col, Container, Row } from "../bootstrap" import { firestore } from "components/firebase" +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" +import { faMagnifyingGlass, faTimes } from "@fortawesome/free-solid-svg-icons" type Paragraph = { confidence: number @@ -11,7 +13,16 @@ type Paragraph = { start: number text: string } - +const ClearButton = styled(FontAwesomeIcon)` + position: absolute; + right: 3rem; + top: 50%; + transform: translateY(-50%); + color: #1a3185; + font-size: 1rem; + z-index: 1; + cursor: pointer; +` const ErrorContainer = styled(Container)` background-color: white; ` @@ -26,6 +37,13 @@ const TimestampCol = styled.div` width: 140px; ` +const NoResultFound = styled.div` + display: flex; + justify-content: center; + align-items: center; + height: 88px; +` + const TranscriptBottom = styled(Row)` background-color: white; border-bottom-left-radius: 0.75rem; @@ -36,6 +54,7 @@ const TranscriptBottom = styled(Row)` const TranscriptContainer = styled(Container)` max-height: 18rem; overflow-y: auto; + background-color: #ffffff; ` const TranscriptHeader = styled(Row)` @@ -66,6 +85,41 @@ const TranscriptRow = styled(Row)` border-bottom-right-radius: 0.75rem; } ` +const SearchInput = styled.input` + width: 100%; + padding: 0.75rem 1rem; + border-radius: 0.5rem; + border: none; + background-color: #ffffff; + font-size: 1rem; + outline: none; + color: #1a3185; + &:focus { + border-color: #999; + background-color: #fff; + } + + &::placeholder { + color: #aaa; + } +` + +const SearchIcon = styled(FontAwesomeIcon)` + position: absolute; + right: 1.75rem; + top: 50%; + transform: translateY(-50%); + color: #1a3185; + font-size: 1rem; + z-index: 1; +` + +const SearchWrapper = styled.div` + position: relative; + width: 100%; + background-color: #8c98c2; + padding: 1.5rem 1rem; +` export const Transcriptions = ({ setCurTimeVideo, @@ -81,6 +135,7 @@ export const Transcriptions = ({ const { t } = useTranslation(["common", "hearing"]) const [highlightedId, setHighlightedId] = useState(-1) const [transcriptData, setTranscriptData] = useState([]) + const [searchTerm, setSearchTerm] = useState("") const vid = videoTranscriptionId || "prevent FirebaseError" const subscriptionRef = collection( @@ -104,10 +159,18 @@ export const Transcriptions = ({ } }, [subscriptionRef, transcriptData]) + const handleClearInput = () => { + setSearchTerm("") + } + useEffect(() => { fetchTranscriptionData() }, [fetchTranscriptionData]) + const filteredData = transcriptData.filter(el => + el.text.toLowerCase().includes(searchTerm.toLowerCase()) + ) + useEffect(() => { const handleTimeUpdate = () => { const currentIndex = transcriptData.findIndex( @@ -130,18 +193,43 @@ export const Transcriptions = ({ return ( <> + + setSearchTerm(e.target.value)} + /> + {searchTerm && ( + + )} + + {transcriptData.length > 0 ? ( - {transcriptData.map((element: Paragraph, index: number) => ( + {filteredData.map((element: Paragraph, index: number) => ( ))} + {filteredData.length === 0 && ( + + {t("no_results_found", { + ns: "hearing", + searchTerm, + defaultValue: "No result found..." + })} + + )} ) : ( @@ -157,12 +245,14 @@ function TranscriptItem({ element, highlightedId, index, - setCurTimeVideo + setCurTimeVideo, + searchTerm }: { element: Paragraph highlightedId: number index: number setCurTimeVideo: any + searchTerm: string }) { const handleClick = (val: number) => { const valSeconds = val / 1000 @@ -194,7 +284,13 @@ function TranscriptItem({ const isHighlighted = (index: number): boolean => { return index === highlightedId } - + const highlightText = (text: string, term: string) => { + if (!term) return text + const regex = new RegExp(`(${term})`, "gi") + return text + .split(regex) + .map((part, i) => (regex.test(part) ? {part} : part)) + } return ( - {element.text} + {highlightText(element.text, searchTerm)} ) } diff --git a/public/locales/en/hearing.json b/public/locales/en/hearing.json index b495bcaf0..6188d5474 100644 --- a/public/locales/en/hearing.json +++ b/public/locales/en/hearing.json @@ -11,5 +11,7 @@ "senate_chair": "Senate Chair", "show_less": "Show less", "show_more": "Show more", - "transcription_not_on_file": "This hearing does not yet have a transcription on file" + "transcription_not_on_file": "This hearing does not yet have a transcription on file", + "search_placeholder": "Search translation", + "no_results_found": "No Search Results for ”{{searchTerm}}”" } \ No newline at end of file