Skip to content

Commit

Permalink
feat: add footer total to statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorusclarence committed Dec 26, 2021
1 parent 1033941 commit 1137e5e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/components/ReactTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Props<T extends object = {}> = {
options?: Omit<TableOptions<T>, 'data' | 'columns'>;
plugins?: PluginHook<T>[];
className?: string;
withFooter?: boolean;
};

// eslint-disable-next-line @typescript-eslint/ban-types
Expand All @@ -28,6 +29,7 @@ export default function ReactTable<T extends object>({
options,
plugins = [],
className,
withFooter = true,
}: Props<T>) {
const {
getTableProps,
Expand All @@ -37,6 +39,7 @@ export default function ReactTable<T extends object>({
prepareRow,
state,
setGlobalFilter,
footerGroups,
} = useTable<T>(
{ ...options, data, columns },
useGlobalFilter,
Expand Down Expand Up @@ -166,6 +169,29 @@ export default function ReactTable<T extends object>({
);
})}
</tbody>
{withFooter && (
<tfoot className='bg-gray-50 dark:bg-gray-700'>
{footerGroups.map((footerGroup, index) => (
<tr
{...footerGroup.getFooterGroupProps()}
key={index}
className='group px-6 py-3 text-xs font-medium tracking-wider text-left text-gray-500 uppercase dark:text-gray-200'
>
{footerGroup.headers.map((column) => (
<td
{...column.getFooterProps()}
className={clsx(
'px-6 py-3 text-sm font-medium tracking-wider text-left text-gray-500 uppercase dark:text-gray-200'
)}
key={column.id}
>
{column.render('Footer')}
</td>
))}
</tr>
))}
</tfoot>
)}
</table>
</div>
</div>
Expand Down
52 changes: 51 additions & 1 deletion src/pages/statistics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,56 @@ export default function StatisticsPage() {
() => [
{
Header: 'Blog Slug',
Footer: 'Total',
accessor: 'slug',
sortDescFirst: true,
},
{
Header: 'Total Views',
accessor: 'views',
Cell: ({ value }) => value.toLocaleString(),
Footer: ({ rows }) =>
React.useMemo(
() => rows.reduce((sum, row) => sum + row.original.views, 0),
[rows]
).toLocaleString(),
sortDescFirst: true,
},
{
Header: 'Web Views',
accessor: 'webViews',
Cell: ({ value }) => value.toLocaleString(),
Footer: ({ rows }) =>
React.useMemo(
() => rows.reduce((sum, row) => sum + row.original.webViews, 0),
[rows]
).toLocaleString(),
sortDescFirst: true,
},
{
Header: 'Dev.to',
accessor: 'devtoViews',
Cell: ({ value }) => value?.toLocaleString() || '-',
Footer: ({ rows }) =>
React.useMemo(
() =>
rows.reduce(
(sum, row) => sum + (row.original?.devtoViews ?? 0),
0
),
[rows]
).toLocaleString(),
sortDescFirst: true,
},
{
Header: 'Likes',
accessor: 'likes',
Cell: ({ value }) => value.toLocaleString(),
Footer: ({ rows }) =>
React.useMemo(
() => rows.reduce((sum, row) => sum + row.original.likes, 0),
[rows]
).toLocaleString(),
sortDescFirst: true,
},
],
Expand All @@ -66,18 +91,29 @@ export default function StatisticsPage() {
{
Header: 'Project Slug',
accessor: 'slug',
Footer: 'Total',
sortDescFirst: true,
},
{
Header: 'Total Views',
accessor: 'views',
Cell: ({ value }) => value.toLocaleString(),
Footer: ({ rows }) =>
React.useMemo(
() => rows.reduce((sum, row) => sum + row.original.views, 0),
[rows]
).toLocaleString(),
sortDescFirst: true,
},
{
Header: 'Likes',
accessor: 'likes',
Cell: ({ value }) => value.toLocaleString(),
Footer: ({ rows }) =>
React.useMemo(
() => rows.reduce((sum, row) => sum + row.original.likes, 0),
[rows]
).toLocaleString(),
sortDescFirst: true,
},
],
Expand All @@ -92,18 +128,29 @@ export default function StatisticsPage() {
{
Header: 'Library Slug',
accessor: 'slug',
Footer: 'Total',
sortDescFirst: true,
},
{
Header: 'Total Views',
accessor: 'views',
Cell: ({ value }) => value.toLocaleString(),
Footer: ({ rows }) =>
React.useMemo(
() => rows.reduce((sum, row) => sum + row.original.views, 0),
[rows]
).toLocaleString(),
sortDescFirst: true,
},
{
Header: 'Likes',
accessor: 'likes',
Cell: ({ value }) => value.toLocaleString(),
Footer: ({ rows }) =>
React.useMemo(
() => rows.reduce((sum, row) => sum + row.original.likes, 0),
[rows]
).toLocaleString(),
sortDescFirst: true,
},
],
Expand All @@ -113,7 +160,10 @@ export default function StatisticsPage() {

return (
<Layout>
<Seo templateTitle='Statistics' />
<Seo
templateTitle='Statistics'
description='Metadata statistics of theodorusclarence.com blogs, projects and libraries.'
/>

<main>
<section className=''>
Expand Down

0 comments on commit 1137e5e

Please sign in to comment.