Skip to content

Commit

Permalink
add column default visibility logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dauglyon committed Aug 13, 2024
1 parent e75a528 commit 1ab9d48
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/common/components/Table.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useId, useMemo, useState } from 'react';
import { useEffect, useId, useMemo, useState } from 'react';
import {
createColumnHelper,
ColumnDef,
Expand All @@ -7,6 +7,7 @@ import {
Table as TableType,
Row,
CellContext,
VisibilityState,
} from '@tanstack/react-table';
import type { RowData } from '@tanstack/react-table';
import { FontAwesomeIcon as FAIcon } from '@fortawesome/react-fontawesome';
Expand Down Expand Up @@ -351,6 +352,7 @@ export const useTableColumns = ({
fields = [],
order = [],
exclude = [],
defaultVisible = undefined,
}: {
fields?: {
displayName?: string;
Expand All @@ -360,6 +362,7 @@ export const useTableColumns = ({
}[];
order?: string[];
exclude?: string[];
defaultVisible?: string[] | undefined;
}) => {
const accessors: {
[fieldName: string]: <RowData extends unknown[]>(
Expand All @@ -370,13 +373,11 @@ export const useTableColumns = ({
accessors[id] = (rowData) => rowData[index];
});

const [columnVisibility, setColumnVisibility] = useState({});

const fieldsOrdered = fields
.filter(({ id }) => !exclude.includes(id))
.filter(({ id }) => !exclude.includes(id.toLowerCase()))
.sort((a, b) => {
const aOrder = order.indexOf(a.id);
const bOrder = order.indexOf(b.id);
const aOrder = order.indexOf(a.id.toLowerCase());
const bOrder = order.indexOf(b.id.toLowerCase());
if (aOrder !== -1 && bOrder !== -1) {
return aOrder - bOrder;
} else if (aOrder !== -1) {
Expand All @@ -388,6 +389,26 @@ export const useTableColumns = ({
}
});

const createDefaultVisible = (): VisibilityState => {
if (!defaultVisible) return {};
return fieldsOrdered.reduce<VisibilityState>((columnVisibility, field) => {
columnVisibility[field.id] = defaultVisible.includes(
field.id.toLowerCase()
);
return columnVisibility;
}, {});
};

const [columnVisibility, setColumnVisibility] =
useState<VisibilityState>(createDefaultVisible);

// in case fields change, this happens during initialization so is required.
const fieldsKey = JSON.stringify(fieldsOrdered.map(({ id }) => id));
useEffect(() => {
setColumnVisibility(createDefaultVisible);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fieldsKey]);

return {
columnVisibility,
setColumnVisibility,
Expand Down Expand Up @@ -450,10 +471,8 @@ export const ColumnSelect = ({
setColumnVisibility,
columnMeta,
}: {
columnVisibility: { [k: string]: boolean | undefined };
setColumnVisibility: React.Dispatch<
React.SetStateAction<{ [k: string]: boolean | undefined }>
>;
columnVisibility: VisibilityState;
setColumnVisibility: React.Dispatch<React.SetStateAction<VisibilityState>>;
columnMeta: { [k: string]: ColumnMeta } | undefined;
}) => {
const id = useId();
Expand Down
34 changes: 34 additions & 0 deletions src/features/collections/data_products/GenomeAttribs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,42 @@ export const GenomeAttribs: FC<{
: undefined,
})),
// HARDCODED the field order parameter and the hidden fields parameter hardcode overrides for which columns will appear and in what order
// GTDB has different column names (#4)
order: ['kbase_display_name', 'kbase_id', 'genome_size'],
exclude: ['__match__', '__sel__'],
defaultVisible: [
// GROW / ENIGMA / PMI
'kbase_display_name',
'kbase_id',
'completeness',
'contamination',
'classification',
'classification_method',
'kbase_gc_content',
'kbase_genome_size',
'kbase_num_protein_encoding_genes',
'kbase_num_cds',
'kbase_num_contigs',
// GTDB special-casing
'accession',
'gtdb_taxonomy',
'checkm_completeness',
'checkm_contamination',
'genome_size',
'gc_percentage',
'mimag_high_quality',
'mimag_medium_quality',
'mimag_low_quality',
'ncbi_assembly_level',
'ncbi_bioproject',
'ncbi_biosample',
'ncbi_date',
'ncbi_organism_name',
'protein_count',
'trna_count',
'gtdb_representative',
'gtdb_type_species_of_genus',
],
}
);

Expand Down

0 comments on commit 1ab9d48

Please sign in to comment.