# code-snippets/4.0.0-beta.3/js/hooks/useDuplicateReports.ts

Code Snippets, version 4.0.0-beta.3. 43 lines.

- Page: https://pluginprobe.com/plugins/code-snippets/4.0.0-beta.3/code/js/hooks/useDuplicateReports.ts
- Raw: https://pluginprobe.com/plugins/code-snippets/4.0.0-beta.3/raw/js/hooks/useDuplicateReports.ts
- Modified: 2026-10-05T14:07:18+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/code-snippets/4.0.0-beta.3/code/js/hooks/useDuplicateReports.ts#L10-L20`.

```typescript
import { useEffect, useState } from 'react'
import { addQueryArg } from '../utils/restAPI'
import { TIMEOUTS } from '../../../tests/e2e/helpers/constants'
import { useRestAPI } from './useRestAPI'
import type { DuplicateReport, DuplicateSearchResponse } from '../types/Feedback'

/** Shortest title worth looking for existing reports of. */
const MIN_SEARCH_LENGTH = 6

/**
 * Offer reports already filed about whatever is being described, so the same problem is
 * not reported twice. A cloud that cannot answer leaves the list empty rather than
 * interrupting the report being written.
 */
export const useDuplicateReports = (searchUrl: string, title: string): DuplicateReport[] => {
	const { api } = useRestAPI()
	const [duplicates, setDuplicates] = useState<DuplicateReport[]>([])

	useEffect(() => {
		const query = title.trim()

		if (MIN_SEARCH_LENGTH > query.length) {
			setDuplicates([])
			return
		}

		let active = true

		const timer = setTimeout(() => {
			api.get<DuplicateSearchResponse>(addQueryArg({ url: searchUrl, name: 'q', value: query }))
				.then(data => active && setDuplicates(data.results))
				.catch(() => active && setDuplicates([]))
		}, TIMEOUTS.VERY_SHORT)

		return () => {
			active = false
			clearTimeout(timer)
		}
	}, [api, searchUrl, title])

	return duplicates
}

```
