full-project-search-button.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { sendSearchEvent } from '@/features/event-tracking/search-events'
  2. import OLButton from '@/shared/components/ol/ol-button'
  3. import OLTooltip from '@/shared/components/ol/ol-tooltip'
  4. import { useLayoutContext } from '@/shared/context/layout-context'
  5. import { closeSearchPanel, SearchQuery } from '@codemirror/search'
  6. import { useCallback } from 'react'
  7. import { useCodeMirrorViewContext } from './codemirror-context'
  8. import MaterialIcon from '@/shared/components/material-icon'
  9. import { useTranslation } from 'react-i18next'
  10. import { useIsNewEditorEnabled } from '@/features/ide-redesign/utils/new-editor-utils'
  11. import { useRailContext } from '@/features/ide-redesign/contexts/rail-context'
  12. export const FullProjectSearchButton = ({ query }: { query: SearchQuery }) => {
  13. const view = useCodeMirrorViewContext()
  14. const { t } = useTranslation()
  15. const { setProjectSearchIsOpen } = useLayoutContext()
  16. const newEditor = useIsNewEditorEnabled()
  17. const { openTab } = useRailContext()
  18. const openFullProjectSearch = useCallback(() => {
  19. if (newEditor) {
  20. openTab('full-project-search')
  21. } else {
  22. setProjectSearchIsOpen(true)
  23. }
  24. closeSearchPanel(view)
  25. window.setTimeout(() => {
  26. window.dispatchEvent(
  27. new CustomEvent('editor:full-project-search', { detail: query })
  28. )
  29. }, 200)
  30. }, [setProjectSearchIsOpen, query, view, newEditor, openTab])
  31. const onClick = useCallback(() => {
  32. sendSearchEvent('search-open', {
  33. searchType: 'full-project',
  34. method: 'button',
  35. location: 'search-form',
  36. })
  37. openFullProjectSearch()
  38. }, [openFullProjectSearch])
  39. return (
  40. <>
  41. <OLTooltip
  42. id="open-full-project-search"
  43. overlayProps={{ placement: 'top' }}
  44. description={t('search_all_project_files')}
  45. >
  46. <OLButton variant="secondary" size="sm" onClick={onClick}>
  47. <MaterialIcon
  48. type="manage_search"
  49. accessibilityLabel={t('search_all_project_files')}
  50. />
  51. </OLButton>
  52. </OLTooltip>
  53. </>
  54. )
  55. }