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 subscribed = useRef(true)
useEffect(() => {
reload()
}, [props.file.timeStamp])
@ -48,7 +49,13 @@ export const ResultItem = (props: ResultItemProps) => {
findText(props.file.filename).then(res => {
if (subscribed.current) {
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)
disableForceReload(props.file.filename)
}
@ -71,7 +78,7 @@ export const ResultItem = (props: ResultItemProps) => {
<ResultFileName file={props.file} />
<div className="result_count">
<div className="result_count_number badge badge-pill badge-secondary">
{lines.length}
{props.file.count}
</div>
</div>
</div>

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

@ -36,9 +36,19 @@ export const SearchReducer = (state: SearchState = SearchingInitialState, action
count: 0
}
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 {
...state,
count: state.count + parseInt(action.payload)
count: count
}
case 'TOGGLE_CASE_SENSITIVE':
return {

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

Loading…
Cancel
Save