File size: 1,961 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import useIsBrowser from '@docusaurus/useIsBrowser';
import { DataViews, filterSortAndPaginate, type Field, type View } from '@wordpress/dataviews';
import { useState } from 'react';
import { data, statuses, type ComponentData } from './data';
import { IconLink } from './icon-link';
import { StatusIndicator } from './status-indicator';
import styles from './wp-components-table.module.scss';

const FIELDS: Field< ComponentData >[] = [
	{
		id: 'status',
		label: 'Status',
		enableHiding: true,
		elements: statuses,
		render: ( { item } ) => {
			return <StatusIndicator status={ item.status } />;
		},
	},
	{
		id: 'name',
		label: 'Name',
		enableHiding: false,
		enableGlobalSearch: true,
	},
	{
		id: 'whereUsed',
		label: 'Where used',
		enableHiding: true,
		elements: [
			{ value: 'global', label: 'Global' },
			{ value: 'editor', label: 'Editor' },
		],
	},
	{
		id: 'docs',
		label: 'Docs',
		enableHiding: true,
		enableSorting: false,
		render: ( { item } ) => {
			return <IconLink href={ item.docs } type="storybook" />;
		},
	},
	{
		id: 'notes',
		label: 'Notes',
		enableHiding: true,
		enableSorting: false,
		render: ( { item } ) => {
			return <div className={ styles[ 'wp-components-table-notes' ] }>{ item.notes }</div>;
		},
	},
];

export function WPComponentsTable() {
	const isBrowser = useIsBrowser();

	const [ view, setView ] = useState< View >( {
		type: 'table',
		page: 1,
		sort: {
			field: 'name',
			direction: 'asc',
		},
		titleField: 'name',
		fields: [ 'status', 'whereUsed', 'docs', 'notes' ],
	} );

	// DataViews includes Emotion components, and thus cannot be rendered on the server.
	if ( ! isBrowser ) {
		return null;
	}

	const { data: filteredData, paginationInfo } = filterSortAndPaginate( data, view, FIELDS );

	return (
		<DataViews
			data={ filteredData ?? [] }
			fields={ FIELDS }
			view={ view }
			onChangeView={ setView }
			paginationInfo={ paginationInfo }
			defaultLayouts={ {} }
		/>
	);
}