Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nateweller committed Dec 19, 2024
1 parent a1018bf commit da9eb51
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ export const THREAT_FIELD_AUTO_FIX = 'auto-fix';

export const DEFAULT_TABLE_FIELDS = [
THREAT_FIELD_SEVERITY,
THREAT_FIELD_THREAT,
THREAT_FIELD_TYPE,
THREAT_FIELD_FIRST_DETECTED,
THREAT_FIELD_FIXED_ON,
THREAT_FIELD_STATUS,
THREAT_FIELD_AUTO_FIX,
// THREAT_FIELD_THREAT,
// THREAT_FIELD_TYPE,
// THREAT_FIELD_FIRST_DETECTED,
// THREAT_FIELD_FIXED_ON,
// THREAT_FIELD_STATUS,
// THREAT_FIELD_AUTO_FIX,
];

export const DEFAULT_LIST_FIELDS = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ export default function ThreatsDataViews( {
[ view.filters ]
);

const getFilterValues = useCallback( ( currentView: View, field: string ) => {
const filter = currentView.filters.find( f => f.field === field );
if ( ! filter ) {
return [];
}
return Array.isArray( filter.value ) ? filter.value : [ filter.value ];
}, [] );

/**
* Compute values from the provided threats data.
*
Expand Down Expand Up @@ -303,7 +311,7 @@ export default function ThreatsDataViews( {
{
id: THREAT_FIELD_STATUS,
label: __( 'Status', 'jetpack-components' ),
enableHiding: false,
// enableHiding: false,
elements: THREAT_STATUSES,
getValue( { item }: { item: Threat } ) {
if ( ! item.status ) {
Expand Down Expand Up @@ -354,8 +362,7 @@ export default function ThreatsDataViews( {
},
]
: [] ),
...( dataFields.includes( 'firstDetected' ) &&
( statusFilters.includes( 'fixed' ) || statusFilters.includes( 'ignored' ) )
...( dataFields.includes( 'firstDetected' )
? [
{
id: THREAT_FIELD_FIRST_DETECTED,
Expand All @@ -374,7 +381,7 @@ export default function ThreatsDataViews( {
},
]
: [] ),
...( dataFields.includes( 'fixedOn' ) && statusFilters.includes( 'fixed' )
...( dataFields.includes( 'fixedOn' )
? [
{
id: THREAT_FIELD_FIXED_ON,
Expand All @@ -393,8 +400,7 @@ export default function ThreatsDataViews( {
},
]
: [] ),
...( dataFields.includes( 'fixable' ) &&
( statusFilters.includes( 'current' ) || ! statusFilters.length )
...( dataFields.includes( 'fixable' )
? [
{
id: THREAT_FIELD_AUTO_FIX,
Expand Down Expand Up @@ -426,7 +432,7 @@ export default function ThreatsDataViews( {
];

return result;
}, [ dataFields, plugins, themes, signatures, onFixThreats, statusFilters ] );
}, [ dataFields, plugins, themes, signatures, onFixThreats ] );

/**
* DataView actions - collection of operations that can be performed upon each record.
Expand Down Expand Up @@ -520,9 +526,35 @@ export default function ThreatsDataViews( {
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-dataviews/#onchangeview-function
*/
const onChangeView = useCallback( ( newView: View ) => {
setView( newView );
}, [] );
const onChangeView = useCallback(
( newView: View ) => {
const customView = { ...newView };

setView( currentView => {
const currentStatusFilters = getFilterValues( currentView, THREAT_FIELD_STATUS );
const newStatusFilters = getFilterValues( customView, THREAT_FIELD_STATUS );
const currentIsVisible =
! currentStatusFilters.length ||
currentStatusFilters.includes( 'fixed' ) ||
currentStatusFilters.includes( 'ignored' );
const newIsVisible =
! newStatusFilters.length ||
newStatusFilters.includes( 'fixed' ) ||
newStatusFilters.includes( 'ignored' );

if ( ! currentIsVisible && newIsVisible ) {
customView.fields.push( THREAT_FIELD_STATUS );
}

if ( currentIsVisible && ! newIsVisible ) {
customView.fields = customView.fields.filter( field => field !== THREAT_FIELD_STATUS );
}

return customView;
} );
},
[ getFilterValues ]
);

/**
* DataView getItemId function - returns the unique ID for each record in the dataset.
Expand Down

0 comments on commit da9eb51

Please sign in to comment.