import { ActionIcon, Box, Button, Group, Select, Stack, Text, ThemeIcon } from '@mantine/core'; import { useMediaQuery } from '@mantine/hooks'; import { showNotification } from '@mantine/notifications'; import { IconDatabaseSmile, IconRefresh, IconTools, IconViewportNarrow } from '@tabler/icons-react'; import { __, sprintf } from '@wordpress/i18n'; import { useContextMenu } from 'mantine-contextmenu'; import { DataTable } from 'mantine-datatable'; import { useState } from 'react'; import useSWR from 'swr'; import { post } from '../../utils/post'; import DatabaseActions from './database-actions'; export function Database() { const { data, isLoading, isValidating, mutate } = useSWR('database', post); const { data: databaseSize, isLoading: isLoadingDatabaseSize } = useSWR( 'get_database_size', post ); const [selectedRecords, setSelectedRecords] = useState([]); const { showContextMenu } = useContextMenu(); const isTouch = useMediaQuery('(pointer: coarse)'); const [bulkAction, setBulkAction] = useState(''); /** * Report an action's outcome: the server's error when it refused (it answered HTTP 200 with * success: false, which used to be announced as done), the success message otherwise. * * @param result What the action helper returned. * @param result.error Present when the request failed or was refused. * @param result.error.statusText The server's message, when it gave one. * @param message The success message. */ const notify = (result: { error?: { statusText?: string } }, message: string) => { if (result?.error) { showNotification({ title: __('Error', 'scotty'), message: result.error.statusText || __('The database did not accept the request.', 'scotty'), color: 'red', withBorder: true, }); return; } showNotification({ message, withBorder: true }); }; /** * Handle Optimize Table. * * @param {*} record * @param record.Name */ const handleOptimizeTable = async (record: { Name: string }) => { const result = await DatabaseActions.tables(record.Name).optimize(); notify( result, // translators: %s: database table name(s). sprintf(__('Optimizing table %s', 'scotty'), record.Name) ); await mutate(undefined, { revalidate: true }); }; /** * Handle Reset Auto Increment. * * @param {*} record * @param record.Name */ const handleResetAutoIncrement = async (record: { Name: string }) => { const result = await DatabaseActions.tables(record.Name).resetAutoIncrement(); notify( result, // translators: %s: database table name(s). sprintf(__('Resetting auto increment for table %s', 'scotty'), record.Name) ); await mutate(undefined, { revalidate: true }); }; const handleBulkAction = async () => { const names = selectedRecords.map((record: { Name: string }) => record.Name).join(', '); switch (bulkAction) { case 'reset_auto_increment': notify( await DatabaseActions.tables(selectedRecords).resetAutoIncrement(), // translators: %s: database table name(s). sprintf(__('Resetting auto increment for table %s', 'scotty'), names) ); break; case 'optimize_table': notify( await DatabaseActions.tables(selectedRecords).optimize(), // translators: %s: database table name(s). sprintf(__('Optimizing table %s', 'scotty'), names) ); break; default: break; } await mutate(undefined, { revalidate: true }); setBulkAction(''); setSelectedRecords([]); }; return (