/
DefaultAddTrackWorkflow.tsx
203 lines (191 loc) · 5.35 KB
/
DefaultAddTrackWorkflow.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import React, { useState } from 'react'
import {
Alert,
Button,
Step,
StepContent,
StepLabel,
Stepper,
Typography,
} from '@mui/material'
import { makeStyles } from 'tss-react/mui'
import { getRoot } from 'mobx-state-tree'
import {
getSession,
isElectron,
isSessionModelWithWidgets,
isSessionWithAddTracks,
isSupportedIndexingAdapter,
} from '@jbrowse/core/util'
import { getConf } from '@jbrowse/core/configuration'
import { observer } from 'mobx-react'
// locals
import ConfirmTrack from './ConfirmTrack'
import TrackSourceSelect from './TrackSourceSelect'
import { AddTrackModel } from '../model'
const useStyles = makeStyles()(theme => ({
root: {
marginTop: theme.spacing(1),
},
stepper: {
backgroundColor: theme.palette.background.default,
},
button: {
marginRight: theme.spacing(1),
},
actionsContainer: {
marginTop: theme.spacing(10),
marginBottom: theme.spacing(2),
},
alertContainer: {
padding: `${theme.spacing(2)}px 0px ${theme.spacing(2)}px 0px`,
},
}))
const steps = ['Enter track data', 'Confirm track type']
const DefaultAddTrackWorkflow = observer(function ({
model,
}: {
model: AddTrackModel
}) {
const [activeStep, setActiveStep] = useState(0)
const { classes } = useStyles()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { jobsManager } = getRoot<any>(model)
const session = getSession(model)
const {
assembly,
trackAdapter,
trackData,
trackName,
trackType,
textIndexTrack,
textIndexingConf,
} = model
const [trackErrorMessage, setTrackErrorMessage] = useState<string>()
function getStepContent(step: number) {
switch (step) {
case 0:
return <TrackSourceSelect model={model} />
case 1:
return <ConfirmTrack model={model} />
default:
return <Typography>Unknown step</Typography>
}
}
async function handleNext() {
if (activeStep !== steps.length - 1) {
setActiveStep(activeStep + 1)
return
}
const trackId = [
`${trackName.toLowerCase().replaceAll(' ', '_')}-${Date.now()}`,
`${session.adminMode ? '' : '-sessionTrack'}`,
].join('')
const assemblyInstance = session.assemblyManager.get(assembly)
if (!isSessionWithAddTracks(session)) {
setTrackErrorMessage('Unable to add tracks to this model')
return
}
if (assemblyInstance && trackAdapter && trackAdapter.type !== 'UNKNOWN') {
session.addTrackConf({
trackId,
type: trackType,
name: trackName,
assemblyNames: [assembly],
adapter: {
...trackAdapter,
sequenceAdapter: getConf(assemblyInstance, ['sequence', 'adapter']),
},
})
model.view.showTrack?.(trackId)
if (
isElectron &&
textIndexTrack &&
isSupportedIndexingAdapter(trackAdapter.type)
) {
const attr = textIndexingConf || {
attributes: ['Name', 'ID'],
exclude: ['CDS', 'exon'],
}
const indexName = trackName + '-index'
const newEntry = {
indexingParams: {
...attr,
assemblies: [assembly],
tracks: [trackId],
indexType: 'perTrack',
name: indexName,
timestamp: new Date().toISOString(),
},
name: indexName,
cancelCallback: () => jobsManager.abortJob(),
}
jobsManager.queueJob(newEntry)
}
model.clearData()
if (isSessionModelWithWidgets(session)) {
session.hideWidget(model)
}
} else {
setTrackErrorMessage(
'Failed to add track.\nThe configuration of this file is not currently supported.',
)
}
}
function isNextDisabled() {
switch (activeStep) {
case 0:
return !trackData
case 1:
return !(trackName && trackType && trackAdapter?.type && assembly)
default:
return true
}
}
return (
<div className={classes.root}>
<Stepper
className={classes.stepper}
activeStep={activeStep}
orientation="vertical"
>
{steps.map((label, idx) => (
<Step key={label}>
<StepLabel>{label}</StepLabel>
<StepContent>
{getStepContent(idx)}
<div className={classes.actionsContainer}>
<Button
disabled={activeStep === 0}
onClick={() => {
setTrackErrorMessage(undefined)
setActiveStep(activeStep - 1)
}}
className={classes.button}
>
Back
</Button>
<Button
disabled={isNextDisabled()}
variant="contained"
color="primary"
onClick={handleNext}
className={classes.button}
data-testid="addTrackNextButton"
>
{activeStep === steps.length - 1 ? 'Add' : 'Next'}
</Button>
</div>
{trackErrorMessage ? (
<div className={classes.alertContainer}>
<Alert severity="error">{trackErrorMessage}</Alert>
</div>
) : null}
</StepContent>
</Step>
))}
</Stepper>
</div>
)
})
export default DefaultAddTrackWorkflow