Skip to content

Commit

Permalink
feat(web-analytics): Add localised number formatting and right-aligne…
Browse files Browse the repository at this point in the history
…d cells (#18116)

* Add better table formatting

* Use QueryContent to right-align table cells and headings

* Just use toLocaleString
  • Loading branch information
robbie-c authored Oct 20, 2023
1 parent b3c4d41 commit b35ea46
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 4 additions & 0 deletions frontend/src/queries/nodes/DataTable/renderColumnMeta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import { getQueryFeatures, QueryFeature } from '~/queries/nodes/DataTable/queryF
export interface ColumnMeta {
title?: JSX.Element | string
width?: number
align?: 'left' | 'right' | 'center'
}

export function renderColumnMeta(key: string, query: DataTableNode, context?: QueryContext): ColumnMeta {
let width: number | undefined
let title: JSX.Element | string | undefined
const queryFeatures = getQueryFeatures(query.source)
let align: ColumnMeta['align']

if (isHogQLQuery(query.source)) {
title = key
Expand All @@ -43,6 +45,7 @@ export function renderColumnMeta(key: string, query: DataTableNode, context?: Qu
) : (
queryContextColumn?.title ?? column.replace('_', ' ')
)
align = queryContextColumn?.align
} else if (key === 'person.$delete') {
title = ''
width = 0
Expand Down Expand Up @@ -71,5 +74,6 @@ export function renderColumnMeta(key: string, query: DataTableNode, context?: Qu
return {
title,
...(typeof width !== 'undefined' ? { width } : {}),
...(align ? { align } : {}),
}
}
1 change: 1 addition & 0 deletions frontend/src/queries/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ interface QueryContextColumn {
title?: string
renderTitle?: QueryContextColumnTitleComponent
render?: QueryContextColumnComponent
align?: 'left' | 'right' | 'center' // default is left
}
15 changes: 5 additions & 10 deletions frontend/src/scenes/web-analytics/WebDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,14 @@ import { DateFilter } from 'lib/components/DateFilter/DateFilter'

const PercentageCell: QueryContextColumnComponent = ({ value }) => {
if (typeof value === 'number') {
return (
<div className="w-full text-right">
<span className="flex-1 text-right">{`${(value * 100).toFixed(1)}%`}</span>
</div>
)
return <span>{`${(value * 100).toFixed(1)}%`}</span>
} else {
return null
}
}

const NumericCell: QueryContextColumnComponent = ({ value }) => {
return (
<div className="w-full text-right">
<span className="flex-1 text-right">{String(value)}</span>
</div>
)
return <span>{typeof value === 'number' ? value.toLocaleString() : String(value)}</span>
}

const BreakdownValueTitle: QueryContextColumnTitleComponent = (props) => {
Expand Down Expand Up @@ -115,14 +107,17 @@ const queryContext: QueryContext = {
bounce_rate: {
title: 'Bounce Rate',
render: PercentageCell,
align: 'right',
},
views: {
title: 'Views',
render: NumericCell,
align: 'right',
},
visitors: {
title: 'Visitors',
render: NumericCell,
align: 'right',
},
},
}
Expand Down

0 comments on commit b35ea46

Please sign in to comment.