@@ -14,6 +14,7 @@ import useEventCallback from "use-event-callback"
1414import { useSetRecoilState } from "recoil"
1515import { themeAtom } from "../../hooks/use-colors"
1616import CreatableSelect from "react-select/creatable"
17+ import NormalSelect from "react-select"
1718import LocationOnIcon from "@material-ui/icons/LocationOn"
1819import TimelapseIcon from "@material-ui/icons/Timelapse"
1920import ZoomInIcon from "@material-ui/icons/ZoomIn"
@@ -89,22 +90,20 @@ export const Toolbar = ({
8990 timestamps = [ ] ,
9091 selectedTimestampIndex,
9192 durationGroups = [ ] ,
93+ durationLabels = [ ] ,
94+ timestampLabels = [ ] ,
9295 selectedDurationGroupIndex,
9396 selectedDurationIndex,
9497 onChangeSelectedItemLabel,
98+ allowCustomLabels = false ,
9599} ) => {
96100 const themeColors = useColors ( )
97101 const [ mode , setToolMode ] = useToolMode ( )
98102 const setTheme = useSetRecoilState ( themeAtom )
99103
100- const [ labelSet , labelColorMap ] = useMemo ( ( ) => {
101- const labelSet = new Set ( )
104+ const [ durationLabelSet , durationLabelColorMap ] = useMemo ( ( ) => {
105+ const labelSet = new Set ( durationLabels )
102106 const labelColorMap = { }
103- for ( const timestamp of timestamps ) {
104- if ( ! timestamp . label ) continue
105- labelSet . add ( timestamp . label )
106- labelColorMap [ timestamp . label ] = timestamp . color
107- }
108107 for ( const dg of durationGroups ) {
109108 for ( const duration of dg . durations ) {
110109 if ( ! duration . label ) continue
@@ -116,7 +115,18 @@ export const Toolbar = ({
116115 labelColorMap [ dg . label ] = dg . color
117116 }
118117 return [ labelSet , labelColorMap ]
119- } , [ timestamps , durationGroups ] )
118+ } , [ timestamps , durationGroups , durationLabels ] )
119+
120+ const [ timestampLabelSet , timestampLabelColorMap ] = useMemo ( ( ) => {
121+ const labelSet = new Set ( timestampLabels )
122+ const labelColorMap = { }
123+ for ( const timestamp of timestamps ) {
124+ if ( ! timestamp . label ) continue
125+ labelSet . add ( timestamp . label )
126+ labelColorMap [ timestamp . label ] = timestamp . color
127+ }
128+ return [ labelSet , labelColorMap ]
129+ } , [ timestamps , durationGroups , timestampLabels ] )
120130
121131 const onSelectCreateTool = useEventCallback ( ( ) => setToolMode ( "create" ) )
122132 const onSelectPanTool = useEventCallback ( ( ) => setToolMode ( "pan" ) )
@@ -134,12 +144,18 @@ export const Toolbar = ({
134144 const { label } = newValue || { }
135145 onChangeSelectedItemLabel ( {
136146 label,
137- color : labelColorMap [ label ] ,
147+ color : durationLabelColorMap [ label ] || timestampLabelColorMap [ label ] ,
138148 } )
139149 } )
140- const creatableSelectOptions = useMemo (
141- ( ) => Array . from ( labelSet ) . map ( ( label ) => ( { label, value : label } ) ) ,
142- [ labelSet ]
150+ const timestampCreatableSelectOptions = useMemo (
151+ ( ) =>
152+ Array . from ( timestampLabelSet ) . map ( ( label ) => ( { label, value : label } ) ) ,
153+ [ timestampLabelSet ]
154+ )
155+ const durationCreatableSelectOptions = useMemo (
156+ ( ) =>
157+ Array . from ( durationLabelSet ) . map ( ( label ) => ( { label, value : label } ) ) ,
158+ [ durationLabelSet ]
143159 )
144160
145161 const selectedTimestamp =
@@ -159,6 +175,8 @@ export const Toolbar = ({
159175 return { label, value : label }
160176 } , [ selectedTimestamp , selectedDuration ] )
161177
178+ const SelectComponent = allowCustomLabels ? CreatableSelect : NormalSelect
179+
162180 return (
163181 < Container themeColors = { themeColors } >
164182 < Box
@@ -186,13 +204,17 @@ export const Toolbar = ({
186204 </ Box >
187205 < Box display = "block" height = { 40 } flexGrow = { 1 } paddingRight = { 2 } >
188206 { ( selectedTimestamp || selectedDuration ) && (
189- < CreatableSelect
207+ < SelectComponent
190208 isClearable
191209 value = { selectedItemValue }
192210 formatCreateLabel = { formatCreateLabel }
193211 styles = { selectFieldStyles }
194212 onChange = { onChangeSelectedLabel }
195- options = { creatableSelectOptions }
213+ options = {
214+ selectedTimestamp
215+ ? timestampCreatableSelectOptions
216+ : durationCreatableSelectOptions
217+ }
196218 />
197219 ) }
198220 </ Box >
0 commit comments