-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #326 from AppQuality/develop
Allow search in trascript
- Loading branch information
Showing
5 changed files
with
133 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from "react"; | ||
|
||
export type HighlightContextType = { | ||
searchTerm: string; | ||
}; | ||
|
||
export const HighlightContext = createContext<HighlightContextType | null>( | ||
null | ||
); | ||
|
||
export const HighlightContextProvider = ({ | ||
term, | ||
children, | ||
}: { | ||
term?: string; | ||
children: React.ReactNode; | ||
}) => { | ||
const [searchTerm, setsearchTerm] = useState<string>(term ?? ""); | ||
|
||
useEffect(() => { | ||
setsearchTerm(term ?? ""); | ||
}, [term]); | ||
|
||
const HighlightContextValue = useMemo( | ||
() => ({ | ||
searchTerm, | ||
}), | ||
[searchTerm] | ||
); | ||
|
||
return ( | ||
<HighlightContext.Provider value={HighlightContextValue}> | ||
{children} | ||
</HighlightContext.Provider> | ||
); | ||
}; | ||
|
||
export const useHighlightContext = () => { | ||
const context = useContext(HighlightContext); | ||
|
||
if (!context) | ||
throw new Error("Provider not found for HighlightContextProvider"); | ||
|
||
return context; // Now we can use the context in the component, SAFELY. | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useHighlightContext } from "./highlightContext"; | ||
|
||
export const Searchable = ({ | ||
start, | ||
text, | ||
}: { | ||
start: number; | ||
text: string; | ||
}) => { | ||
const { searchTerm } = useHighlightContext(); | ||
|
||
if (searchTerm) { | ||
const parts = text.split(new RegExp(`(${searchTerm})`, "gi")); | ||
|
||
return ( | ||
<> | ||
{parts.map((part, index) => | ||
part.toLowerCase() === searchTerm.toLowerCase() ? ( | ||
<mark key={index}>{part}</mark> | ||
) : ( | ||
<>{part}</> | ||
) | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
return <>{text}</>; | ||
}; |