Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Fix: Doc search text highlighter crashing for some queries (#17655)

Greg Richardson81b76e208955
diff --git a/packages/ui/src/components/Command/Command.utils.tsx b/packages/ui/src/components/Command/Command.utils.tsx
index 9d4fe97bed4..7d740a2ad47 100644
--- a/packages/ui/src/components/Command/Command.utils.tsx
+++ b/packages/ui/src/components/Command/Command.utils.tsx
@@ -4,10 +4,10 @@ import * as React from 'react'
import { cn } from './../../lib/utils'
import { DetailedHTMLProps, HTMLAttributes, KeyboardEventHandler } from 'react'
+import { LoadingLine } from '../LoadingLine/LoadingLine'
import { Modal } from '../Modal'
import { ModalProps } from '../Modal/Modal'
import { useCommandMenu } from './CommandMenuProvider'
-import { LoadingLine } from '../LoadingLine/LoadingLine'
type CommandPrimitiveElement = React.ElementRef<typeof CommandPrimitive>
type CommandPrimitiveProps = React.ComponentPropsWithoutRef<typeof CommandPrimitive>
@@ -312,28 +312,24 @@ CommandShortcut.displayName = 'CommandLabel'
export interface TextHighlighterProps
extends DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement> {
- text: string | undefined
- query: string | undefined
+ text: string
+ query: string
}
export const TextHighlighter = ({ text, query, ...props }: TextHighlighterProps) => {
- const highlightMatches = (text?: string) => {
- if (!text) {
- return ''
- }
+ // Wrap all instances of `query` in a span to make them bold
+ const elements = text.split(query).flatMap((part, index, parts) => {
+ const returnValue = [<>{part}</>]
- if (!query) {
- return text
+ // Add back the wrapped `query` (if it's not the last element)
+ if (index !== parts.length - 1) {
+ returnValue.push(<span className="font-semibold text-scale-1200">{query}</span>)
}
- const regex = new RegExp(query, 'gi')
- return text.replace(
- regex,
- (match) => `<span class="font-semibold text-scale-1200">${match}</span>`
- )
- }
+ return returnValue
+ })
- return <span dangerouslySetInnerHTML={{ __html: highlightMatches(text) }} {...props} />
+ return <span {...props}>{elements}</span>
}
TextHighlighter.displayName = 'TextHighlighter'