Skip to content

Commit

Permalink
jump to marked result
Browse files Browse the repository at this point in the history
  • Loading branch information
warrenday committed Dec 28, 2023
1 parent f997ade commit 9fea1eb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 55 deletions.
48 changes: 0 additions & 48 deletions src/components/SupportPopover/index.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions src/containers/Main/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useSearch } from "@/hooks/useSearch"
import { useNetworkTabs } from "@/hooks/useNetworkTabs"
import { NetworkPanel } from "../NetworkPanel"
import { SearchPanel } from "../SearchPanel"
import { SupportPopover } from "../../components/SupportPopover"
import { useWebSocketNetworkMonitor } from "../../hooks/useWebSocketNetworkMonitor"
import { useOperationFilters } from "../../hooks/useOperationFilters"

Expand Down Expand Up @@ -52,7 +51,6 @@ export const Main = () => {
/>
}
/>
<SupportPopover />
</>
)
}
3 changes: 2 additions & 1 deletion src/containers/NetworkPanel/NetworkDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { CloseButton } from "@/components/CloseButton"
import { useApolloTracing } from "@/hooks/useApolloTracing"
import { useToggle } from "@/hooks/useToggle"
import { useShareMessage } from "../../../hooks/useShareMessage"
import { useMemo } from "react"
import { useEffect, useMemo } from "react"

Check warning on line 13 in src/containers/NetworkPanel/NetworkDetails/index.tsx

View workflow job for this annotation

GitHub Actions / build (16)

'useEffect' is defined but never used
import { useSearch } from "../../../hooks/useSearch"

Check warning on line 14 in src/containers/NetworkPanel/NetworkDetails/index.tsx

View workflow job for this annotation

GitHub Actions / build (16)

'useSearch' is defined but never used

export interface INetworkDetailsProps {
data: INetworkRequest
Expand Down
20 changes: 16 additions & 4 deletions src/hooks/useMark.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef } from "react"
import { useCallback, useEffect, useRef } from "react"
import Mark from "mark.js"
import { useSearch } from "./useSearch"

Expand All @@ -8,7 +8,11 @@ import { useSearch } from "./useSearch"
*
* @returns ref to the element to mark
*/
export const useMark = (searchQuery: string, content?: string) => {
export const useMark = (
searchQuery: string,
content?: string,
done?: () => void
) => {
const ref = useRef(null)

useEffect(() => {
Expand All @@ -20,12 +24,13 @@ export const useMark = (searchQuery: string, content?: string) => {
mark.mark(searchQuery, {
caseSensitive: false,
separateWordSearch: false,
done,
})

return () => {
mark.unmark()
}
}, [ref, searchQuery, content])
}, [ref, searchQuery, content, done])

return ref
}
Expand All @@ -38,7 +43,14 @@ export const useMark = (searchQuery: string, content?: string) => {
*/
export const useMarkSearch = (content?: string) => {
const { searchQuery } = useSearch()
const ref = useMark(searchQuery, content)

// Once mark is complete we can jump to the first result
const onMarkDone = useCallback(() => {
const element = document.querySelector('mark[data-markjs="true"]')
element?.scrollIntoView()
}, [])

const ref = useMark(searchQuery, content, onMarkDone)

return ref
}

0 comments on commit 9fea1eb

Please sign in to comment.