File size: 4,786 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
import { recordTracksEvent } from '@automattic/calypso-analytics';
import { useSuspenseQuery } from '@tanstack/react-query';
import {
	__experimentalHStack as HStack,
	__experimentalText as Text,
	__experimentalVStack as VStack,
	Button,
	Card,
	CardBody,
} from '@wordpress/components';
import { useDispatch } from '@wordpress/data';
import { createInterpolateElement } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { blockTable } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';
import { useState } from 'react';
import { siteBySlugQuery } from '../../app/queries/site';
import Notice from '../../components/notice';
import PageLayout from '../../components/page-layout';
import { HostingFeatures } from '../../data/constants';
import { fetchPhpMyAdminToken } from '../../data/site-hosting';
import HostingFeatureGatedWithCallout from '../hosting-feature-gated-with-callout';
import SettingsPageHeader from '../settings-page-header';
import ResetPasswordModal from './reset-password-modal';
import upsellIllustrationUrl from './upsell-illustration.svg';

export default function SiteDatabaseSettings( { siteSlug }: { siteSlug: string } ) {
	const { data: site } = useSuspenseQuery( siteBySlugQuery( siteSlug ) );
	const { createErrorNotice, createSuccessNotice } = useDispatch( noticesStore );
	const [ isFetchingToken, setIsFetchingToken ] = useState( false );
	const [ isResetPasswordModalOpen, setIsResetPasswordModalOpen ] = useState( false );

	const handleOpenPhpMyAdmin = async () => {
		setIsFetchingToken( true );

		try {
			const token = await fetchPhpMyAdminToken( site.ID );
			if ( ! token ) {
				throw new Error( 'No token found' );
			}

			window.open( `https://wordpress.com/pma-login?token=${ token }` );
		} catch {
			createErrorNotice( __( 'Failed to fetch phpMyAdmin token.' ), { type: 'snackbar' } );
		}

		setIsFetchingToken( false );
	};

	const handleResetPasswordClick = () => {
		setIsResetPasswordModalOpen( true );
		recordTracksEvent( 'calypso_sites_dashboard_database_reset_password_click' );
	};

	const handleResetPasswordSuccess = () => {
		setIsResetPasswordModalOpen( false );
		createSuccessNotice( __( 'Your database password has been restored.' ), {
			type: 'snackbar',
		} );
	};

	const handleResetPasswordError = () => {
		setIsResetPasswordModalOpen( false );
		createErrorNotice(
			__( 'Sorry, we had a problem restoring your database password. Please try again.' ),
			{
				type: 'snackbar',
			}
		);
	};

	return (
		<PageLayout
			size="small"
			header={
				<SettingsPageHeader
					title={ __( 'Database' ) }
					description={ __(
						'For the tech-savvy, manage your database with phpMyAdmin and run a wide range of operations with MySQL.'
					) }
				/>
			}
		>
			<HostingFeatureGatedWithCallout
				site={ site }
				feature={ HostingFeatures.DATABASE }
				tracksFeatureId="settings-database"
				upsellIcon={ blockTable }
				upsellImage={ upsellIllustrationUrl }
				upsellTitle={ __( 'Fast, familiar database access' ) }
				upsellDescription={ __(
					'Access your sites database with phpMyAdminperfect for inspecting data, running queries, and quick debugging.'
				) }
			>
				<Card>
					<CardBody>
						<VStack spacing={ 4 }>
							<VStack spacing={ 2 }>
								<Text size="15px" weight={ 500 } lineHeight="20px">
									phpMyAdmin
								</Text>
								<Text variant="muted" lineHeight="20px">
									{ __(
										'phpMyAdmin is a free open source software tool that allows you to administer your site’s MySQL database over the Web.'
									) }
								</Text>
							</VStack>
							<VStack>
								<Notice density="medium">
									{ __(
										'Managing a database can be tricky and it’s not necessary for your site to function.'
									) }
								</Notice>
							</VStack>
							<HStack justify="flex-start" expanded={ false } as="span">
								<Button
									variant="primary"
									isBusy={ isFetchingToken }
									onClick={ handleOpenPhpMyAdmin }
								>
									{ __( 'Open phpMyAdmin ↗' ) }
								</Button>
							</HStack>
							<Text variant="muted" lineHeight="20px">
								{ createInterpolateElement(
									__( 'Having problems with access? Try <link>resetting the password</link>.' ),
									{
										link: <Button variant="link" onClick={ handleResetPasswordClick } />,
									}
								) }
							</Text>
						</VStack>
					</CardBody>
				</Card>

				{ isResetPasswordModalOpen && (
					<ResetPasswordModal
						siteId={ site.ID }
						onClose={ () => setIsResetPasswordModalOpen( false ) }
						onSuccess={ handleResetPasswordSuccess }
						onError={ handleResetPasswordError }
					/>
				) }
			</HostingFeatureGatedWithCallout>
		</PageLayout>
	);
}