File size: 4,189 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { useQuery } from '@tanstack/react-query';
import { useNavigate } from '@tanstack/react-router';
import { Button, ExternalLink, Notice } from '@wordpress/components';
import { DataViews, filterSortAndPaginate } from '@wordpress/dataviews';
import { __ } from '@wordpress/i18n';
import { useState, useMemo } from 'react';
import { emailsQuery } from '../app/queries/emails';
import DataViewsCard from '../components/dataviews-card';
import { PageHeader } from '../components/page-header';
import PageLayout from '../components/page-layout';
import type { Email } from '../data/types';
import type { View } from '@wordpress/dataviews';

const fields = [
	{
		id: 'emailAddress',
		label: __( 'Email Address' ),
		enableGlobalSearch: true,
		render: ( { item }: { item: Email } ) =>
			item.type === 'mailbox' ? (
				<ExternalLink href={ `https://mail.${ item.domainName }` }>
					{ item.emailAddress }
				</ExternalLink>
			) : (
				item.emailAddress
			),
	},
	{
		id: 'type',
		label: __( 'Type' ),
		render: ( { item }: { item: Email } ) =>
			item.type === 'mailbox' ? __( 'Mailbox' ) : __( 'Forwarding' ),
		getValue: ( { item }: { item: Email } ) => item.type,
		elements: [
			{ value: 'mailbox', label: __( 'Mailbox' ) },
			{ value: 'forwarding', label: __( 'Forwarding' ) },
		],
	},
	{
		id: 'provider',
		label: __( 'Provider' ),
		render: ( { item }: { item: Email } ) => {
			if ( item.type === 'forwarding' && item.forwardingTo ) {
				return `${ __( 'Forwards to' ) } ${ item.forwardingTo }`;
			}

			// Display the provider display name from the data
			// This keeps the component agnostic while showing user-friendly names
			return item.providerDisplayName;
		},
		getValue: ( { item }: { item: Email } ) => item.provider,
	},
];

function Emails() {
	const navigate = useNavigate();
	const emails = useQuery( emailsQuery() ).data;
	const [ selection, setSelection ] = useState< Email[] >( [] );

	// View config
	const [ view, setView ] = useState< View >( {
		type: 'table',
		page: 1,
		perPage: 10,
		sort: {
			field: 'emailAddress',
			direction: 'asc',
		},
		fields: [ 'type', 'provider' ],
		titleField: 'emailAddress',
	} );

	const actions = useMemo(
		() => [
			{
				id: 'manage',
				label: __( 'Manage' ),
				callback: ( items: Email[] ) => {
					navigate( { to: `/emails/${ items[ 0 ].id }` } );
				},
			},
			{
				id: 'edit',
				label: __( 'Edit' ),
				callback: ( items: Email[] ) => {
					navigate( { to: `/emails/${ items[ 0 ].id }/edit` } );
				},
			},
			{
				id: 'access-webmail',
				label: __( 'Access Webmail' ),
				callback: ( items: Email[] ) => {
					window.open( `https://mail.${ items[ 0 ].domainName }`, '_blank' );
				},
				isEligible: ( item: Email ) => item.type === 'mailbox',
			},
			{
				id: 'delete',
				label: __( 'Delete' ),
				callback: () => {
					setSelection( [] );
				},
				isDestructive: true,
				supportsBulk: true,
			},
		],
		[ navigate ]
	);

	if ( ! emails ) {
		return;
	}

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

	const onClickItem = ( item: Email ) => {
		navigate( { to: `/emails/${ item.id }` } );
	};

	return (
		<PageLayout
			header={
				<PageHeader
					title={ __( 'Emails' ) }
					actions={
						<>
							<Button variant="secondary" __next40pxDefaultSize>
								{ __( 'Add Email Forwarder' ) }
							</Button>
							<Button variant="primary" __next40pxDefaultSize>
								{ __( 'Add Mailbox' ) }
							</Button>
						</>
					}
				/>
			}
			notices={
				<Notice status="warning" isDismissible={ false }>
					{ __( 'This is using fake data for the moment' ) }
				</Notice>
			}
		>
			<DataViewsCard>
				<DataViews
					data={ filteredData }
					fields={ fields }
					view={ view }
					onChangeView={ setView }
					onClickItem={ onClickItem }
					selection={ selection.map( ( item ) => item.id ) }
					onChangeSelection={ ( ids ) =>
						setSelection( emails.filter( ( email ) => ids.includes( email.id ) ) )
					}
					actions={ actions }
					defaultLayouts={ { table: {} } }
					paginationInfo={ paginationInfo }
				/>
			</DataViewsCard>
		</PageLayout>
	);
}

export default Emails;