-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Excluding goals in dashboard (#4983)
* Simple frontend for has_done_not * Simple UI for goal filter adding or removal * Better alignment on trash icons, avoid moving around if row expands * Refactor filter text functions, share code * has_not_done, special casing for has not done when formatting filter text * Changelog * Fix lint * prettier format * Add tests * Lowercase Goal * Update changelog * has_not_done for goals is now named `is not` in the UI * prettier * Document and test serializeApiFilters
- Loading branch information
Showing
14 changed files
with
211 additions
and
79 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from 'react' | ||
import { DashboardQuery, Filter, FilterClauseLabels } from '../query' | ||
import { plainFilterText, styledFilterText } from './filter-text' | ||
import { render, screen } from '@testing-library/react' | ||
|
||
describe('styledFilterText() and plainFilterText()', () => { | ||
it.each<[Filter, FilterClauseLabels, string]>([ | ||
[['is', 'page', ['/docs', '/blog']], {}, 'Page is /docs or /blog'], | ||
[['is', 'country', ['US']], { US: 'United States' }, 'Country is United States'], | ||
[['is', 'goal', ['Signup']], {}, 'Goal is Signup'], | ||
[['is', 'props:browser_language', ['en-US']], {}, 'Property browser_language is en-US'], | ||
[['has_not_done', 'goal', ['Signup', 'Login']], {}, 'Goal is not Signup or Login'], | ||
])( | ||
'when filter is %p and labels are %p, functions return %p', | ||
(filter, labels, expectedPlainText) => { | ||
const query = { labels } as unknown as DashboardQuery | ||
|
||
expect(plainFilterText(query, filter)).toBe(expectedPlainText) | ||
|
||
render(<p data-testid="filter-text">{styledFilterText(query, filter)}</p>) | ||
expect(screen.getByTestId('filter-text')).toHaveTextContent(expectedPlainText) | ||
} | ||
) | ||
}) |
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,77 @@ | ||
/* @format */ | ||
|
||
import React, { ReactNode, isValidElement, Fragment } from 'react' | ||
import { DashboardQuery, Filter } from '../query' | ||
import { | ||
EVENT_PROPS_PREFIX, | ||
FILTER_OPERATIONS_DISPLAY_NAMES, | ||
formattedFilters, | ||
getLabel, | ||
getPropertyKeyFromFilterKey | ||
} from './filters' | ||
|
||
export function styledFilterText( | ||
query: DashboardQuery, | ||
[operation, filterKey, clauses]: Filter | ||
) { | ||
if (filterKey.startsWith(EVENT_PROPS_PREFIX)) { | ||
const propKey = getPropertyKeyFromFilterKey(filterKey) | ||
return ( | ||
<> | ||
Property <b>{propKey}</b> {FILTER_OPERATIONS_DISPLAY_NAMES[operation]}{' '} | ||
{formatClauses(clauses)} | ||
</> | ||
) | ||
} | ||
|
||
const formattedFilter = ( | ||
formattedFilters as Record<string, string | undefined> | ||
)[filterKey] | ||
const clausesLabels = clauses.map((value) => | ||
getLabel(query.labels, filterKey, value) | ||
) | ||
|
||
if (!formattedFilter) { | ||
throw new Error(`Unknown filter: ${filterKey}`) | ||
} | ||
|
||
return ( | ||
<> | ||
{capitalize(formattedFilter)} {FILTER_OPERATIONS_DISPLAY_NAMES[operation]}{' '} | ||
{formatClauses(clausesLabels)} | ||
</> | ||
) | ||
} | ||
|
||
export function plainFilterText(query: DashboardQuery, filter: Filter) { | ||
return reactNodeToString(styledFilterText(query, filter)) | ||
} | ||
|
||
function formatClauses(labels: Array<string | number>): ReactNode[] { | ||
return labels.map((label, index) => ( | ||
<Fragment key={index}> | ||
{index > 0 && ' or '} | ||
<b>{label}</b> | ||
</Fragment> | ||
)) | ||
} | ||
|
||
function capitalize(str: string): string { | ||
return str[0].toUpperCase() + str.slice(1) | ||
} | ||
|
||
function reactNodeToString(reactNode: ReactNode): string { | ||
let string = '' | ||
if (typeof reactNode === 'string') { | ||
string = reactNode | ||
} else if (typeof reactNode === 'number') { | ||
string = reactNode.toString() | ||
} else if (reactNode instanceof Array) { | ||
reactNode.forEach(function (child) { | ||
string += reactNodeToString(child) | ||
}) | ||
} else if (isValidElement(reactNode)) { | ||
string += reactNodeToString(reactNode.props.children) | ||
} | ||
return string | ||
} |
Oops, something went wrong.