| 1 |
import React from 'react' |
| 2 |
import { __, sprintf } from '@wordpress/i18n' |
| 3 |
import { Button } from '../../common/Button' |
| 4 |
import { ImportCard } from '../common/ImportCard' |
| 5 |
import type { UseSelection } from '../../../hooks/useSelection' |
| 6 |
import type { ImportableSnippet } from './WithMigrationData' |
| 7 |
|
| 8 |
interface TableNavProps extends SimpleSnippetTableProps { |
| 9 |
which: 'top' | 'bottom' |
| 10 |
} |
| 11 |
|
| 12 |
const TableNav: React.FC<TableNavProps> = ({ which, onImport, isImporting, selection }) => |
| 13 |
<div className={`tablenav ${which}`}> |
| 14 |
<Button onClick={selection.selectAll}> |
| 15 |
{selection.isAllSelected |
| 16 |
? __('Deselect All', 'code-snippets') |
| 17 |
: __('Select All', 'code-snippets')} |
| 18 |
</Button> |
| 19 |
|
| 20 |
<Button |
| 21 |
primary |
| 22 |
onClick={onImport} |
| 23 |
disabled={0 === selection.selectedItems.size || isImporting} |
| 24 |
> |
| 25 |
{isImporting |
| 26 |
? __('Importing…', 'code-snippets') |
| 27 |
// translators: %d: number of selected snippets. |
| 28 |
: sprintf(__('Import Selected (%d)', 'code-snippets'), selection.selectedItems.size)} |
| 29 |
</Button> |
| 30 |
</div> |
| 31 |
|
| 32 |
export interface SimpleSnippetTableProps { |
| 33 |
selection: UseSelection<ImportableSnippet, number> |
| 34 |
onImport: VoidFunction |
| 35 |
isImporting: boolean |
| 36 |
} |
| 37 |
|
| 38 |
export const SimpleSnippetTable: React.FC<SimpleSnippetTableProps> = props => { |
| 39 |
const { selection: { availableItems, selectedItems, selectAll, isAllSelected, toggleItem } } = props |
| 40 |
|
| 41 |
return ( |
| 42 |
<ImportCard className="migrate-snippets-table-card snippets-table-card"> |
| 43 |
<header> |
| 44 |
<h3>{sprintf( |
| 45 |
// translators: %d: number of available snippets. |
| 46 |
__('Available snippets (%d)', 'code-snippets'), |
| 47 |
availableItems.length |
| 48 |
)}</h3> |
| 49 |
<p>{__('We found the following snippets:', 'code-snippets')}</p> |
| 50 |
</header> |
| 51 |
|
| 52 |
<TableNav which="top" {...props} /> |
| 53 |
|
| 54 |
<table className="wp-list-table widefat fixed striped"> |
| 55 |
<thead> |
| 56 |
<tr> |
| 57 |
<th scope="col" className="check-column"> |
| 58 |
<input |
| 59 |
type="checkbox" |
| 60 |
checked={isAllSelected} |
| 61 |
onChange={selectAll} |
| 62 |
aria-label={__('Select all snippets', 'code-snippets')} |
| 63 |
/> |
| 64 |
</th> |
| 65 |
<th scope="col" className="column-name">{__('Snippet name', 'code-snippets')}</th> |
| 66 |
<th scope="col" className="column-id">{__('ID', 'code-snippets')}</th> |
| 67 |
</tr> |
| 68 |
</thead> |
| 69 |
<tbody> |
| 70 |
{availableItems.map(snippet => |
| 71 |
<tr key={snippet.table_data.id}> |
| 72 |
<th scope="row" className="check-column"> |
| 73 |
<input |
| 74 |
type="checkbox" |
| 75 |
checked={selectedItems.has(snippet.table_data.id)} |
| 76 |
onChange={() => toggleItem(snippet.table_data.id)} |
| 77 |
aria-label={sprintf( |
| 78 |
// translators: %s: snippet name. |
| 79 |
__('Select %s', 'code-snippets'), |
| 80 |
snippet.table_data.title |
| 81 |
)} |
| 82 |
/> |
| 83 |
</th> |
| 84 |
<td className="column-name">{snippet.table_data.title}</td> |
| 85 |
<td className="column-id">{snippet.table_data.id}</td> |
| 86 |
</tr>)} |
| 87 |
</tbody> |
| 88 |
</table> |
| 89 |
<TableNav which="bottom" {...props} /> |
| 90 |
</ImportCard> |
| 91 |
) |
| 92 |
} |
| 93 |
|