Skip to content

Commit

Permalink
Remove RefSelector and use new Table and DataTable (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfheij-sil authored Jul 19, 2024
2 parents 479c4a7 + 238401a commit e53126d
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 93 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@
"webpack-merge": "^5.10.0",
"zip-folder-promise": "^1.2.0"
},
"workspaces": ["src/*"],
"workspaces": [
"src/*"
],
"volta": {
"node": "20.11.1"
}
Expand Down
55 changes: 31 additions & 24 deletions src/paratext-bible-word-list/src/word-content-viewer.component.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { useState, useEffect } from 'react';
import { Canon } from '@sillsdev/scripture';
import { Table } from 'platform-bible-react';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from 'platform-bible-react';
import type { WordListEntry } from 'paratext-bible-word-list';

type Row = {
type WordData = {
reference: string;
text: string;
};
Expand All @@ -13,42 +20,42 @@ function generateTableData(selectedWord: WordListEntry) {
Canon.bookNumberToId(selectedWord.scrRefs[0].bookNum),
);

const newRows: Row[] = [];
const newWordData: WordData[] = [];
for (let id = 0; id < selectedWord.scrRefs.length; id++) {
const { chapterNum } = selectedWord.scrRefs[id];
const { verseNum } = selectedWord.scrRefs[id];
const fullReference: string = `${bookName} ${chapterNum}:${verseNum}`;
newRows.push({ reference: fullReference, text: selectedWord.scriptureSnippets[id] });
newWordData.push({ reference: fullReference, text: selectedWord.scriptureSnippets[id] });
}

return newRows;
return newWordData;
}

export default function WordContentViewer({ selectedWord }: { selectedWord: WordListEntry }) {
const [rows, setRows] = useState<Row[]>([]);
const [wordData, setWordData] = useState<WordData[]>([]);

useEffect(() => {
setRows([]);
setWordData([]);

setRows(generateTableData(selectedWord));
setWordData(generateTableData(selectedWord));
}, [selectedWord]);

return (
<Table<Row>
columns={[
{
key: 'reference',
name: 'Reference',
width: 150,
},
{
key: 'text',
name: 'Text',
},
]}
rows={rows}
rowHeight={30}
headerRowHeight={50}
/>
<Table>
<TableHeader>
<TableRow>
<TableHead>Reference</TableHead>
<TableHead>Text</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{wordData.map((result) => (
<TableRow>
<TableCell>{result.reference}</TableCell>
<TableCell>{result.text}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
);
}
10 changes: 2 additions & 8 deletions src/paratext-bible-word-list/src/word-list.web-view.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useSetting, useData } from '@papi/frontend/react';
import { WebViewProps } from '@papi/core';
import { ChangeEvent, useEffect, useMemo, useState } from 'react';
import { ComboBox, RefSelector, ScriptureReference, Switch, TextField } from 'platform-bible-react';
import { ComboBox, ScriptureReference, Switch, TextField } from 'platform-bible-react';
import type { WordListEntry } from 'paratext-bible-word-list';
import WordContentViewer from './word-content-viewer.component';
import WordTable from './word-table.component';
Expand Down Expand Up @@ -59,7 +59,7 @@ globalThis.webViewComponent = function WordListWebView({
projectId,
useWebViewState,
}: WebViewProps) {
const [scrRef, setScrRef] = useSetting('platform.verseRef', defaultScrRef);
const [scrRef] = useSetting('platform.verseRef', defaultScrRef);
const [scope, setScope] = useWebViewState<Scope>('scope', Scope.Book);
const [wordFilter, setWordFilter] = useState<string>('');
const [selectedWord, setSelectedWord] = useState<WordListEntry>();
Expand Down Expand Up @@ -116,12 +116,6 @@ globalThis.webViewComponent = function WordListWebView({

return (
<div className="word-list">
<RefSelector
scrRef={scrRef}
handleSubmit={(newScrRef) => {
setScrRef(newScrRef);
}}
/>
<div className="filters">
<ComboBox
title="Scope"
Expand Down
108 changes: 48 additions & 60 deletions src/paratext-bible-word-list/src/word-table.component.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,62 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { Table, TableCellClickArgs, TableSortColumn } from 'platform-bible-react';
import { useMemo } from 'react';
import { Button, ColumnDef, DataTable, RowContents, SortDirection } from 'platform-bible-react';
import type { WordListEntry } from 'paratext-bible-word-list';

type Row = {
type WordData = {
word: string;
count: number;
};

const defaultSortColumns: TableSortColumn[] = [{ columnKey: 'word', direction: 'ASC' }];

type WordTableProps = {
wordList: WordListEntry[];
fullWordCount: number;
onWordClick: (word: string) => void;
};

export default function WordTable({ wordList, fullWordCount, onWordClick }: WordTableProps) {
const [sortColumns, setSortColumns] = useState<TableSortColumn[]>(defaultSortColumns);
const onSortColumnsChange = useCallback((changedSortColumns: TableSortColumn[]) => {
setSortColumns(changedSortColumns.slice(-1));
}, []);
const getSortingIcon = (sortDirection: false | SortDirection): string => {
if (sortDirection === 'asc') {
return '↑';
}
if (sortDirection === 'desc') {
return '↓';
}
return '↕';
};

const rows = useMemo(() => {
const newRows: Row[] = [];
const columns = (wordColumnTitle: string): ColumnDef<WordData>[] => [
{
accessorKey: 'word',
header: ({ column }) => {
return (
<Button onClick={() => column.toggleSorting(undefined)}>
{`${wordColumnTitle} ${getSortingIcon(column.getIsSorted())}`}
</Button>
);
},
},
{
accessorKey: 'count',
header: ({ column }) => {
return (
<Button onClick={() => column.toggleSorting(undefined)}>
{`Count ${getSortingIcon(column.getIsSorted())}`}
</Button>
);
},
},
];

export default function WordTable({ wordList, fullWordCount, onWordClick }: WordTableProps) {
const wordData = useMemo(() => {
const newWordData: WordData[] = [];
wordList.forEach((word) => {
newRows.push({ word: word.word, count: word.scrRefs.length });
newWordData.push({ word: word.word, count: word.scrRefs.length });
});
return newRows;
return newWordData;
}, [wordList]);

const sortedRows = useMemo((): readonly Row[] => {
if (sortColumns.length === 0) return rows;
const { columnKey, direction } = sortColumns[0];

let sortedRowsLocal: Row[] = [...rows];

switch (columnKey) {
case 'word':
sortedRowsLocal = sortedRowsLocal.sort((a, b) => a[columnKey].localeCompare(b[columnKey]));
break;
case 'count':
sortedRowsLocal = sortedRowsLocal.sort((a, b) => a[columnKey] - b[columnKey]);
break;
default:
}
return direction === 'DESC' ? sortedRowsLocal.reverse() : sortedRowsLocal;
}, [rows, sortColumns]);

useEffect(() => {
if (sortColumns.length === 0) {
setSortColumns(defaultSortColumns);
}
}, [sortColumns]);

const onCellClick = (args: TableCellClickArgs<Row>) => {
onWordClick(args.row.word);
const onCellClick = (row: RowContents<WordData>): void => {
onWordClick(row.getValue('word'));
};

const wordColumnTitle = useMemo(() => {
Expand All @@ -64,26 +66,12 @@ export default function WordTable({ wordList, fullWordCount, onWordClick }: Word
}, [fullWordCount, wordList.length]);

return (
<Table<Row>
columns={[
{
key: 'word',
name: wordColumnTitle,
},
{
key: 'count',
name: 'Count',
},
]}
rows={sortedRows}
rowKeyGetter={(row: Row) => {
return row.word;
}}
sortColumns={sortColumns}
onSortColumnsChange={onSortColumnsChange}
rowHeight={30}
headerRowHeight={50}
onCellClick={onCellClick}
<DataTable
enablePagination
showPaginationControls
columns={columns(wordColumnTitle)}
data={wordData}
onRowClickHandler={onCellClick}
/>
);
}

0 comments on commit e53126d

Please sign in to comment.