import { gridSpans } from '@patternfly/react-core' ; import { IDashboard , IDashboardReference , IPlaceholders } from '@kobsio/plugin-core' ; import { IDashboardsOptions } from './interfaces' ; export const toGridSpans = ( defaultSpan , force , span ) : gridSpans => { if ( ! span || span < 1 || span > 12 || force ) { return defaultSpan ; } return span as gridSpans ; } ; export const rowHeight = ( rowSize , rowSpan ) : string => { if ( ! rowSize || rowSize < 1 || rowSize > 12 ) { rowSize = 2 ; } if ( rowSpan && rowSpan > 1 && rowSpan <= 12 ) { return `${ rowSize * 150 * rowSpan + ( rowSpan - 1 ) * 16 }px` ; } return `${ rowSize * 150 }px` ; } ; export const getInitialOptions = ( search , references , useDrawer ) : IDashboardsOptions => { const params = new URLSearchParams ( search ) ; const dashboard = params . get ( 'dashboard' ) ; let isReferenced = false ; if ( useDrawer ) { for ( const reference of references ) { if ( reference . title === dashboard ) { isReferenced = true ; } } } return { dashboard : dashboard && isReferenced ? dashboard : references . length > 0 ? references [ 0 ] . title : '' , } ; } ; export const filterDashboards = ( dashboards , searchTerm ) : IDashboard [ ] => { if ( searchTerm === '' ) { return dashboards ; } return dashboards . filter ( ( dashboard ) => dashboard . name . includes ( searchTerm ) ) ; } ; export const getPlaceholdersObject = ( dashboard ? ) : IPlaceholders | undefined => { if ( ! dashboard || ! dashboard . placeholders ) { return undefined ; } const placeholders = { } ; for ( const placeholder of dashboard . placeholders ) { placeholders [ placeholder . name ] = '' ; } return placeholders ; } ; export const getPlaceholdersFromSearch = ( search ) : IPlaceholders | undefined => { const placeholders = ( /^[?#]/ . test ( search ) ? search . slice ( 1 ) : search ) . split ( '&' ) . reduce ( ( params , param ) => { const item = param . split ( '=' ) ; const key = decodeURIComponent ( item [ 0 ] || '' ) ; const value = decodeURIComponent ( item [ 1 ] || '' ) ; if ( key ) { params [ key ] = value ; } return params ; } , { } ) ; return placeholders ; } ;