-
-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Clickable filters with ability to enter custom keyword filters.
- Loading branch information
Showing
7 changed files
with
158 additions
and
9 deletions.
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
24 changes: 24 additions & 0 deletions
24
src/Exceptionless.Web/ClientApp/src/lib/components/filters/FilterableItem.svelte
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,24 @@ | ||
<script lang="ts"> | ||
import type { TermFilter } from './filters'; | ||
export let term: string; | ||
export let value: string | number | boolean | null | undefined; | ||
const title = `Search ${term}:${value}`; | ||
function onSearchClick() { | ||
document.dispatchEvent( | ||
new CustomEvent('filter', { | ||
detail: <TermFilter>{ | ||
term, | ||
value | ||
}, | ||
bubbles: true | ||
}) | ||
); | ||
} | ||
</script> | ||
|
||
<button on:click|preventDefault={onSearchClick} {title}> | ||
<slot /> | ||
</button> |
89 changes: 89 additions & 0 deletions
89
src/Exceptionless.Web/ClientApp/src/lib/components/filters/filters.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,89 @@ | ||
export interface IFilter {} | ||
|
||
export interface KeywordFilter extends IFilter { | ||
keyword: string; | ||
} | ||
|
||
export interface TermFilter extends IFilter { | ||
term: string; | ||
value: string | number | boolean | Date | null | undefined; | ||
} | ||
|
||
function isKeywordFilter(filter: IFilter): filter is KeywordFilter { | ||
return 'keyword' in filter; | ||
} | ||
|
||
function isTermFilter(filter: IFilter): filter is TermFilter { | ||
return 'term' in filter; | ||
} | ||
|
||
export function toFilter(filters: IFilter[]): string { | ||
return filters.map(toFilterPart).filter(Boolean).join(' '); | ||
} | ||
|
||
function toFilterPart(filter: IFilter): string | undefined { | ||
if (isKeywordFilter(filter)) { | ||
return filter.keyword; | ||
} else if (isTermFilter(filter)) { | ||
return `${filter.term}:${filter.value}`; | ||
} | ||
} | ||
|
||
/** | ||
* Update the filters with the given filter. If the filter already exists, it will be removed. | ||
* @param filters The filters | ||
* @param filter The filter to add or remove | ||
* @returns The updated filters | ||
*/ | ||
export function updateFilters(filters: IFilter[], filter: IFilter): IFilter[] { | ||
const index = filters.findIndex((f) => toFilterPart(f) === toFilterPart(filter)); | ||
if (index !== -1) { | ||
filters.splice(index, 1); | ||
} else { | ||
filters.push(filter); | ||
} | ||
|
||
return filters; | ||
} | ||
|
||
/** | ||
* Given the existing filters try and parse out any existing filters while adding new user filters as a keyword filter. | ||
* @param filters The current filters | ||
* @param filter The current filter string that was modified by the user | ||
* @returns The updated filter | ||
*/ | ||
export function updateFiltersWithUserModifications(filters: IFilter[], input: string): IFilter[] { | ||
const resolvedFilters: IFilter[] = []; | ||
|
||
const keywordFilterParts = []; | ||
for (const filter of filters) { | ||
input = input?.trim(); | ||
if (!input) { | ||
break; | ||
} | ||
|
||
// NOTE: This is a super naive implementation... | ||
const part = toFilterPart(filter); | ||
if (part) { | ||
// Check for whole word / phrase match | ||
const regex = new RegExp(`(^|\\s)${part}(\\s|$)`); | ||
if (regex.test(input)) { | ||
input = input.replace(regex, ''); | ||
if (isKeywordFilter(filter)) { | ||
keywordFilterParts.push(part); | ||
} else { | ||
resolvedFilters.push(filter); | ||
} | ||
} | ||
} | ||
} | ||
|
||
input = `${keywordFilterParts.join(' ')} ${input ?? ''}`.trim(); | ||
if (input) { | ||
resolvedFilters.push({ | ||
keyword: input | ||
}); | ||
} | ||
|
||
return resolvedFilters; | ||
} |
5 changes: 2 additions & 3 deletions
5
...rc/lib/components/WebSocketMessage.svelte → ...nents/messaging/CustomEventMessage.svelte
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
8 changes: 8 additions & 0 deletions
8
src/Exceptionless.Web/ClientApp/src/lib/components/messaging/WebSocketMessage.svelte
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,8 @@ | ||
<script lang="ts"> | ||
import type { WebSocketMessageType } from '$lib/models/websocket'; | ||
import CustomEventMessage from './CustomEventMessage.svelte'; | ||
export let type: WebSocketMessageType; | ||
</script> | ||
|
||
<CustomEventMessage {type} on:message}></CustomEventMessage> |
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