-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🔮 [HADXVI-53] Browser SDK extension search bar improvement #2771
Merged
Merged
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
48bde8f
Add escape for whitespaces
cy-moi 46974cf
Add search for description
cy-moi 32ed6b2
Add search partial string with * within search keys
cy-moi 1029f8d
Add copy search query button
cy-moi 93a7022
Add escape for whitespace; format file
cy-moi d1454dc
Format json.tsx
cy-moi be6d847
Add comments
cy-moi e874d46
Fix bug json stringify
cy-moi 944bcff
Fix bad pratice of useContext
cy-moi 949def6
Simplify regex
cy-moi ed2a73c
Add comment for removing quotes
cy-moi 36f0822
Update developer-extension/src/panel/hooks/useEvents/eventFilters.ts
cy-moi 2fd701c
Update developer-extension/src/panel/components/json.tsx
cy-moi e160f5e
Add unit tests for parse query
cy-moi 9a50071
Merge branch 'main' into congyao/HADXVI-53-extension-search-bar
cy-moi 3e7d812
Add unit tests; improve copy search query menu item
cy-moi 3f7d4b9
Make prettier happy
cy-moi d5d0de0
Remove comments
cy-moi 9fba2c0
make prettier happy
cy-moi 846ae00
Remove outdated comment; improve code
cy-moi 810cce4
remove quantifier in regex for safari
cy-moi bcc6402
Skip IE and safari for unit tests bc regex not compatible
cy-moi 8e48323
Remove browserstack unit tests for developer extension
cy-moi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ import type { | |
import type { SdkEvent } from '../../../sdkEvent' | ||
import { isTelemetryEvent, isLogEvent, isRumEvent } from '../../../sdkEvent' | ||
import { formatDate, formatDuration } from '../../../formatNumber' | ||
import { defaultFormatValue, Json } from '../../json' | ||
import { CopyMenuItem, defaultFormatValue, Json } from '../../json' | ||
import { LazyCollapse } from '../../lazyCollapse' | ||
import type { FacetRegistry } from '../../../hooks/useEvents' | ||
import { useSdkInfos } from '../../../hooks/useSdkInfos' | ||
|
@@ -90,6 +90,11 @@ export const EventRow = React.memo( | |
) | ||
} | ||
|
||
function getCopyMenuItemsForPath(path: string, value: unknown) { | ||
const searchTerm = String(value).replace(/ /g, '\\ ') | ||
return <CopyMenuItem value={`${path}:${searchTerm}`}>Copy search query</CopyMenuItem> | ||
} | ||
|
||
return ( | ||
<Table.Tr> | ||
{columns.map((column): React.ReactElement => { | ||
|
@@ -151,7 +156,15 @@ export const EventRow = React.memo( | |
<Json | ||
value={value} | ||
defaultCollapseLevel={0} | ||
getMenuItemsForPath={(path) => getMenuItemsForPath(path ? `${column.path}.${path}` : column.path)} | ||
getMenuItemsForPath={(path) => { | ||
const fullPath = path ? `${column.path}.${path}` : column.path | ||
return ( | ||
<> | ||
{getMenuItemsForPath(fullPath)} | ||
{typeof value === 'string' ? getCopyMenuItemsForPath(column.path, value) : undefined} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💬 suggestion: move this code to function getMenuItemsForPath(path: string, value: unknown) {
const menuItems: React.ChildNode[] = []
const newColumn: EventListColumn = { type: 'field', path }
if (path && !includesColumn(columns, newColumn)) {
menuItems.push(
<Menu.Item
key="add-column"
onClick={() => {
onColumnsChange(addColumn(columns, newColumn))
}}
leftSection={<IconColumnInsertRight size={14} />}
>
Add column
</Menu.Item>
)
}
if (typeof value === "string") {
const searchTerm = String(value).replace(/ /g, '\\ ')
menuItems.push(
<CopyMenuItem value={`${path}:${searchTerm}`}>Copy search query</CopyMenuItem>
)
}
return <>{menuItems}</>
} |
||
</> | ||
) | ||
}} | ||
formatValue={(path, value) => formatValue(path ? `${column.path}.${path}` : column.path, value)} | ||
/> | ||
)} | ||
|
74 changes: 74 additions & 0 deletions
74
developer-extension/src/panel/hooks/useEvents/eventFilters.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { parseQuery, matchWithWildcard } from './eventFilters' | ||
|
||
describe('parseQuery', () => { | ||
it('return a simple field', () => { | ||
expect(parseQuery('foo:bar')).toEqual([['foo', 'bar']]) | ||
}) | ||
it('return intermediary fields', () => { | ||
expect(parseQuery('foo.bar:baz')).toEqual([['foo.bar', 'baz']]) | ||
}) | ||
it('return multiple fields', () => { | ||
expect(parseQuery('foo:bar baz:qux')).toEqual([ | ||
['foo', 'bar'], | ||
['baz', 'qux'], | ||
]) | ||
}) | ||
it('parse escaped whitespace with backslashes in search terms', () => { | ||
expect(parseQuery('foo:bar\\ baz')).toEqual([['foo', 'bar\\ baz']]) | ||
}) | ||
it('parse escaped whitespace with backslashes in keys', () => { | ||
expect(parseQuery('foo\\ bar:baz')).toEqual([['foo\\ bar', 'baz']]) | ||
}) | ||
it('return multiple fields with escaped whitespace', () => { | ||
expect(parseQuery('foo\\ bar:baz\\ qux')).toEqual([['foo\\ bar', 'baz\\ qux']]) | ||
expect(parseQuery('foo:bar\\ baz qux:quux\\ corge')).toEqual([ | ||
['foo', 'bar\\ baz'], | ||
['qux', 'quux\\ corge'], | ||
]) | ||
}) | ||
}) | ||
|
||
describe('matchWithWildcard', () => { | ||
it('matches exact strings', () => { | ||
expect(matchWithWildcard('foo', 'foo')).toBe(true) | ||
}) | ||
it('matches exact strings case-insensitively', () => { | ||
expect(matchWithWildcard('foo', 'FOO')).toBe(true) | ||
}) | ||
it('matches substrings', () => { | ||
expect(matchWithWildcard('foo', 'oo')).toBe(true) | ||
}) | ||
it('matches substrings case-insensitively', () => { | ||
expect(matchWithWildcard('foo', 'OO')).toBe(true) | ||
}) | ||
it('does not match missing substrings', () => { | ||
expect(matchWithWildcard('foo', 'bar')).toBe(false) | ||
}) | ||
it('does not match missing substrings case-insensitively', () => { | ||
expect(matchWithWildcard('foo', 'BAR')).toBe(false) | ||
}) | ||
it('matches with wildcard at the beginning', () => { | ||
expect(matchWithWildcard('foo', '*oo')).toBe(true) | ||
}) | ||
it('matches with wildcard at the end', () => { | ||
expect(matchWithWildcard('foo', 'fo*')).toBe(true) | ||
}) | ||
it('matches with wildcard at the beginning and the end', () => { | ||
expect(matchWithWildcard('foo', '*o*')).toBe(true) | ||
}) | ||
it('matches with wildcard at the beginning and the end case-insensitively', () => { | ||
expect(matchWithWildcard('foo', '*O*')).toBe(true) | ||
}) | ||
it('does not match missing substrings with wildcard at the beginning', () => { | ||
expect(matchWithWildcard('foo', '*bar')).toBe(false) | ||
}) | ||
it('does not match missing substrings with wildcard at the end', () => { | ||
expect(matchWithWildcard('foo', 'bar*')).toBe(false) | ||
}) | ||
it('does not match missing substrings with wildcard at the beginning and the end', () => { | ||
expect(matchWithWildcard('foo', '*bar*')).toBe(false) | ||
}) | ||
it('does not match missing substrings with wildcard at the beginning and the end case-insensitively', () => { | ||
expect(matchWithWildcard('foo', '*BAR*')).toBe(false) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,8 +26,14 @@ export function applyEventFilters(filters: EventFilters, events: SdkEvent[], fac | |
filteredEvents = filterExcludedFacets(filteredEvents, filters.excludedFacetValues, facetRegistry) | ||
|
||
if (filters.query) { | ||
const query = parseQuery(filters.query) | ||
filteredEvents = filteredEvents.filter(query.match) | ||
const queryParts: string[][] = parseQuery(filters.query) | ||
const matchQuery = (event: SdkEvent) => | ||
queryParts.every((queryPart) => { | ||
// Hack it to restore the whitespace | ||
const searchTerm = queryPart.length > 1 ? queryPart[1].replaceAll(/\\\s+/gm, ' ') : '' | ||
return matchQueryPart(event, queryPart[0], searchTerm) | ||
}) | ||
filteredEvents = filteredEvents.filter(matchQuery) | ||
} | ||
|
||
if (!filters.outdatedVersions) { | ||
|
@@ -73,20 +79,38 @@ function filterOutdatedVersions(events: SdkEvent[]): SdkEvent[] { | |
return events.filter((event) => !outdatedEvents.has(event)) | ||
} | ||
|
||
function parseQuery(query: string) { | ||
export function parseQuery(query: string) { | ||
const queryParts = query | ||
.split(' ') | ||
.split(/(?<!\\)\s/gm) // Hack it to escape whitespace with backslashes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👏 praise: nice! |
||
.filter((queryPart) => queryPart) | ||
.map((queryPart) => queryPart.split(':')) | ||
|
||
return { | ||
match: (event: SdkEvent) => | ||
queryParts.every((queryPart) => matchQueryPart(event, queryPart[0], queryPart.length ? queryPart[1] : '')), | ||
return queryParts | ||
} | ||
|
||
export function matchWithWildcard(value: string, searchTerm: string): boolean { | ||
value = value.toLowerCase() | ||
searchTerm = searchTerm.toLowerCase() | ||
if (!searchTerm.includes('*')) { | ||
return value.includes(searchTerm) | ||
} | ||
const searchTerms = searchTerm.toLowerCase().split('*') | ||
let lastIndex = 0 | ||
for (const term of searchTerms) { | ||
const index = value.indexOf(term, lastIndex) | ||
if (index === -1) { | ||
return false | ||
} | ||
lastIndex = index + term.length | ||
} | ||
return true | ||
} | ||
|
||
function matchQueryPart(json: unknown, searchKey: string, searchTerm: string, jsonPath = ''): boolean { | ||
if (jsonPath.endsWith(searchKey) && String(json).startsWith(searchTerm)) { | ||
if (searchKey.toLowerCase() === 'description') { | ||
return matchWithWildcard(JSON.stringify(json), searchTerm) | ||
} | ||
if (jsonPath.endsWith(searchKey) && matchWithWildcard(String(json), searchTerm)) { | ||
return true | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥜 nitpick: this comment isn't accurate anymore