counting better

pull/2092/head
filip mertens 3 years ago
parent d6e6c0350e
commit fe8f8114c8
  1. 11
      libs/remix-ui/search/src/lib/components/results/ResultItem.tsx
  2. 7
      libs/remix-ui/search/src/lib/context/context.tsx
  3. 12
      libs/remix-ui/search/src/lib/reducers/Reducer.ts
  4. 1
      libs/remix-ui/search/src/lib/types/index.ts

@ -18,6 +18,7 @@ export const ResultItem = (props: ResultItemProps) => {
const reloadTimeOut = useRef(null) const reloadTimeOut = useRef(null)
const subscribed = useRef(true) const subscribed = useRef(true)
useEffect(() => { useEffect(() => {
reload() reload()
}, [props.file.timeStamp]) }, [props.file.timeStamp])
@ -48,7 +49,13 @@ export const ResultItem = (props: ResultItemProps) => {
findText(props.file.filename).then(res => { findText(props.file.filename).then(res => {
if (subscribed.current) { if (subscribed.current) {
setLines(res) setLines(res)
if (res) updateCount(res.length) if (res) {
let count = 0
res.forEach(line => {
count += line.lines.length
})
updateCount(count, props.file.filename)
}
setLoading(false) setLoading(false)
disableForceReload(props.file.filename) disableForceReload(props.file.filename)
} }
@ -71,7 +78,7 @@ export const ResultItem = (props: ResultItemProps) => {
<ResultFileName file={props.file} /> <ResultFileName file={props.file} />
<div className="result_count"> <div className="result_count">
<div className="result_count_number badge badge-pill badge-secondary"> <div className="result_count_number badge badge-pill badge-secondary">
{lines.length} {props.file.count}
</div> </div>
</div> </div>
</div> </div>

@ -35,7 +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 updateCount: (count: number, file: string) => void
} }
export const SearchContext = createContext<SearchingStateInterface>(null) export const SearchContext = createContext<SearchingStateInterface>(null)
@ -135,10 +135,10 @@ export const SearchProvider = ({
payload: file payload: file
}) })
}, },
updateCount: (count: number) => { updateCount: (count: number, file: string) => {
dispatch({ dispatch({
type: 'UPDATE_COUNT', type: 'UPDATE_COUNT',
payload: count payload: {count, file}
}) })
}, },
findText: async (path: string) => { findText: async (path: string) => {
@ -222,6 +222,7 @@ export const SearchProvider = ({
path: file, path: file,
timeStamp: Date.now(), timeStamp: Date.now(),
forceReload: false, forceReload: false,
count: 0
} }
return r return r
}) })

@ -36,9 +36,19 @@ export const SearchReducer = (state: SearchState = SearchingInitialState, action
count: 0 count: 0
} }
case 'UPDATE_COUNT': case 'UPDATE_COUNT':
const findFile = state.searchResults.find(file => file.filename === action.payload.file)
let count = 0
if (findFile) {
findFile.count = action.payload.count
}
state.searchResults.forEach(file => {
if (file.count) {
count += file.count
}
})
return { return {
...state, ...state,
count: state.count + parseInt(action.payload) count: count
} }
case 'TOGGLE_CASE_SENSITIVE': case 'TOGGLE_CASE_SENSITIVE':
return { return {

@ -32,6 +32,7 @@ export interface SearchResult {
lines: SearchResultLine[], lines: SearchResultLine[],
timeStamp: number, timeStamp: number,
forceReload: boolean, forceReload: boolean,
count: number
} }
export interface SearchState { export interface SearchState {

Loading…
Cancel
Save