pull/5370/head
filip mertens 3 years ago
parent 9ea619df08
commit ebfff7c7fb
  1. 28
      libs/remix-ui/search/src/lib/components/results/ResultItem.tsx
  2. 7
      libs/remix-ui/search/src/lib/components/results/Results.tsx
  3. 9
      libs/remix-ui/search/src/lib/context/context.tsx
  4. 6
      libs/remix-ui/search/src/lib/reducers/Reducer.ts
  5. 11
      libs/remix-ui/search/src/lib/search.css
  6. 9
      libs/remix-ui/search/src/lib/types/index.ts

@ -9,18 +9,21 @@ interface ResultItemProps {
} }
export const ResultItem = (props: ResultItemProps) => { export const ResultItem = (props: ResultItemProps) => {
const { state, findText, disableForceReload } = useContext(SearchContext) const { state, findText, disableForceReload, updateCount } = useContext(
SearchContext
)
const [loading, setLoading] = useState<boolean>(false) const [loading, setLoading] = useState<boolean>(false)
const [lines, setLines] = useState<SearchResultLine[]>([]) const [lines, setLines] = useState<SearchResultLine[]>([])
const [toggleExpander, setToggleExpander] = useState<boolean>(false) const [toggleExpander, setToggleExpander] = useState<boolean>(false)
const reloadTimeOut = useRef(null) const reloadTimeOut = useRef(null)
const subscribed = useRef(true)
useEffect(() => { useEffect(() => {
reload() reload()
}, [props.file.timeStamp]) }, [props.file.timeStamp])
useEffect(() => { useEffect(() => {
if(props.file.forceReload){ if (props.file.forceReload) {
clearTimeout(reloadTimeOut.current) clearTimeout(reloadTimeOut.current)
reloadTimeOut.current = setTimeout(() => reload(), 1000) reloadTimeOut.current = setTimeout(() => reload(), 1000)
} }
@ -34,11 +37,21 @@ export const ResultItem = (props: ResultItemProps) => {
reload() reload()
}, [state.find]) }, [state.find])
useEffect(() => {
subscribed.current = true
return () => {
subscribed.current = false
}
}, [])
const reload = () => { const reload = () => {
findText(props.file.filename).then(res => { findText(props.file.filename).then(res => {
setLines(res) if (subscribed.current) {
setLoading(false) setLines(res)
disableForceReload(props.file.filename) if (res) updateCount(res.length)
setLoading(false)
disableForceReload(props.file.filename)
}
}) })
} }
@ -56,6 +69,11 @@ export const ResultItem = (props: ResultItemProps) => {
></i> ></i>
</button>{' '} </button>{' '}
<ResultFileName file={props.file} /> <ResultFileName file={props.file} />
<div className="result_count">
<div className="result_count_number badge badge-pill badge-secondary">
{lines.length}
</div>
</div>
</div> </div>
{loading ? <div className="loading">Loading...</div> : null} {loading ? <div className="loading">Loading...</div> : null}
{!toggleExpander && !loading ? ( {!toggleExpander && !loading ? (

@ -1,16 +1,17 @@
import React, { useContext } from 'react' import React, { useContext, useEffect } from 'react'
import { SearchContext } from '../../context/context' import { SearchContext } from '../../context/context'
import { ResultItem } from './ResultItem' import { ResultItem } from './ResultItem'
export const Results = () => { export const Results = () => {
const { state } = useContext(SearchContext) const { state } = useContext(SearchContext)
return ( return (
<div data-id='search_results'> <div data-id='search_results'>
{state.searchResults && {state.find ? <div className='result_count_number badge badge-pill badge-secondary'>{state.count} results</div>: null}
{state.count < state.maxResults && state.searchResults &&
state.searchResults.map((result, index) => { state.searchResults.map((result, index) => {
return <ResultItem key={index} file={result} /> return <ResultItem key={index} file={result} />
})} })}
{state.find && state.count >= state.maxResults? <div className='alert alert-warning mt-1'>Too many results to display.<br></br>Please narrow your search.</div>: null}
</div> </div>
) )
} }

@ -35,6 +35,7 @@ export interface SearchingStateInterface {
toggleUseRegex: () => void toggleUseRegex: () => void
setReplaceWithoutConfirmation: (value: boolean) => void setReplaceWithoutConfirmation: (value: boolean) => void
disableForceReload: (file: string) => void disableForceReload: (file: string) => void
updateCount: (count: number) => void
} }
export const SearchContext = createContext<SearchingStateInterface>(null) export const SearchContext = createContext<SearchingStateInterface>(null)
@ -134,6 +135,12 @@ export const SearchProvider = ({
payload: file payload: file
}) })
}, },
updateCount: (count: number) => {
dispatch({
type: 'UPDATE_COUNT',
payload: count
})
},
findText: async (path: string) => { findText: async (path: string) => {
if (!plugin) return if (!plugin) return
try { try {
@ -214,7 +221,7 @@ export const SearchProvider = ({
lines: [], lines: [],
path: file, path: file,
timeStamp: Date.now(), timeStamp: Date.now(),
forceReload: false forceReload: false,
} }
return r return r
}) })

@ -33,6 +33,12 @@ export const SearchReducer = (state: SearchState = SearchingInitialState, action
return { return {
...state, ...state,
searchResults: action.payload, searchResults: action.payload,
count: 0
}
case 'UPDATE_COUNT':
return {
...state,
count: state.count + parseInt(action.payload)
} }
case 'TOGGLE_CASE_SENSITIVE': case 'TOGGLE_CASE_SENSITIVE':
return { return {

@ -93,3 +93,14 @@
overflow: hidden; overflow: hidden;
text-transform: uppercase; text-transform: uppercase;
} }
.search_tab .result_count {
flex-grow: 1;
text-align: right;
display: flex;
justify-content: flex-end;
}
.search_tab .result_count_number {
font-size: x-small;
}

@ -1,3 +1,4 @@
import { count } from "console";
export interface Action { export interface Action {
type: string type: string
@ -30,7 +31,7 @@ export interface SearchResult {
path: string, path: string,
lines: SearchResultLine[], lines: SearchResultLine[],
timeStamp: number, timeStamp: number,
forceReload: boolean forceReload: boolean,
} }
export interface SearchState { export interface SearchState {
@ -44,6 +45,8 @@ export interface SearchState {
replaceWithOutConfirmation: boolean, replaceWithOutConfirmation: boolean,
useRegExp: boolean, useRegExp: boolean,
timeStamp: number, timeStamp: number,
count: number,
maxResults: number
} }
export const SearchingInitialState: SearchState = { export const SearchingInitialState: SearchState = {
@ -56,5 +59,7 @@ export const SearchingInitialState: SearchState = {
matchWord: false, matchWord: false,
useRegExp: false, useRegExp: false,
replaceWithOutConfirmation: false, replaceWithOutConfirmation: false,
timeStamp: 0 timeStamp: 0,
count: 0,
maxResults: 500
} }
Loading…
Cancel
Save