Skip to content

Commit

Permalink
Avoid using origName, origId, origType
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Jun 7, 2022
1 parent dfb87e8 commit 68c0974
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 50 deletions.
22 changes: 11 additions & 11 deletions packages/core/BaseFeatureWidget/BaseFeatureDetail.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import React, { useState } from 'react'
import dompurify from 'dompurify'
import { ErrorBoundary } from 'react-error-boundary'
import {
Accordion,
Expand Down Expand Up @@ -30,9 +31,6 @@ const MAX_FIELD_NAME_WIDTH = 170

// these are always omitted as too detailed
const globalOmit = [
'origId',
'origName',
'origType',
'length',
'position',
'subfeatures',
Expand Down Expand Up @@ -524,30 +522,32 @@ function isEmpty(obj: Record<string, unknown>) {
return Object.keys(obj).length === 0
}

function clean(value: string) {
const node = document.createElement('div')
node.innerHTML = dompurify.sanitize(value)
return node.innerText
}
function generateTitle(name: string, id: string, type: string) {
return [ellipses(name || id), type].filter(f => !!f).join(' - ')
console.log({ name, id }, clean(name || id))
return [ellipses(clean(name || id)), type].filter(f => !!f).join(' - ')
}

export const FeatureDetails = (props: {
model: IAnyStateTreeNode
feature: SimpleFeatureSerialized & {
origName?: string
origId?: string
origType?: string
}
feature: SimpleFeatureSerialized
depth?: number
omit?: string[]
formatter?: (val: unknown, key: string) => React.ReactElement
}) => {
const { omit = [], model, feature, depth = 0 } = props
const { origName = '', origId = '', origType = '', subfeatures } = feature
const { name = '', id = '', type = '', subfeatures } = feature
const session = getSession(model)
const defaultSeqTypes = ['mRNA', 'transcript']
const sequenceTypes =
getConf(session, ['featureDetails', 'sequenceTypes']) || defaultSeqTypes

return (
<BaseCard title={generateTitle(origName, origId, origType)}>
<BaseCard title={generateTitle(name, id, type)}>
<Typography>Core details</Typography>
<CoreDetails {...props} />
<Divider />
Expand Down
45 changes: 11 additions & 34 deletions packages/core/BaseFeatureWidget/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,6 @@ import { ElementId } from '../util/types/mst'

const configSchema = ConfigurationSchema('BaseFeatureWidget', {})

function isObject(obj: unknown): obj is Record<string, unknown> {
return typeof obj === 'object' && obj !== null
}

function iterate(
obj: Record<string, unknown>,
parse: (key: string, value: unknown, obj: Record<string, unknown>) => void,
) {
for (const k in obj) {
const r = obj[k]
if (isObject(r)) {
iterate(r, parse)
} else if (obj.hasOwnProperty(k)) {
parse(k, r, obj)
}
}
}

function iterateSubfeatures(
obj: Record<string, unknown>,
parse: (obj: Record<string, unknown>) => void,
Expand Down Expand Up @@ -76,18 +58,6 @@ export default function stateModelFactory(pluginManager: PluginManager) {
const feature = clone(self.unformattedFeatureData)
let trackExtra = {}

iterate(feature, (key, val, obj) => {
if (key === 'id') {
obj.origId = val
}
if (key === 'name') {
obj.origName = val
}
if (key === 'type') {
obj.origType = val
}
})

if (self.track) {
const ret = getConf(self.track, ['formatFields'], { feature })
if (ret) {
Expand All @@ -109,12 +79,19 @@ export default function stateModelFactory(pluginManager: PluginManager) {
},
}))
.preProcessSnapshot(snap => {
const { featureData, ...rest } = snap
return { unformattedFeatureData: featureData, ...rest }
// @ts-ignore
const { featureData, finalizedFeatureData, ...rest } = snap
return {
unformattedFeatureData: featureData,
featureData: finalizedFeatureData,
...rest,
}
})
.postProcessSnapshot(snap => {
const { unformattedFeatureData, ...rest } = snap
return rest
const { unformattedFeatureData, featureData, ...rest } = snap
// finalizedFeatureData avoids running formatter twice if loading from
// snapshot
return { finalizedFeatureData: featureData, ...rest }
})
}

Expand Down
7 changes: 3 additions & 4 deletions packages/core/pluggableElementTypes/models/baseTrackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,14 @@ export function createBaseTrackConfig(pluginManager: PluginManager) {

formatFields: {
type: 'frozen',
description: 'adds extra fields to the base feature details',
description: 'adds extra fields to the feature details',
defaultValue: {},
contextVariable: ['feature'],
},

formatFieldsNested: {
formatSubfeatureFields: {
type: 'frozen',
description:
'adds extra fields to the base feature details, applied across nested subfeatures',
description: 'adds extra fields to the subfeatures of a feature',
defaultValue: {},
contextVariable: ['feature'],
},
Expand Down
2 changes: 1 addition & 1 deletion test_data/config_demo.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
"type": "FeatureTrack",
"trackId": "ncbi_gff_hg19_2",
"name": "NCBI RefSeq w/ top-level feature details",
"formatFields": "jexl:{name:\"https://google.com/?q=\"+feature.name,random:\"Wow\"}",
"formatFields": "jexl:{name:\"https://google.com/?q=\"+feature.name,random:\"Wow \"+feature.name}",
"assemblyNames": ["hg19"],
"category": ["Annotation"],
"metadata": {
Expand Down

0 comments on commit 68c0974

Please sign in to comment.