Skip to content

Commit 0e70f25

Browse files
committed
fix: support disabling custom labels
1 parent ae7cf99 commit 0e70f25

File tree

4 files changed

+66
-20
lines changed

4 files changed

+66
-20
lines changed

src/components/MainLayout/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export const MainLayout = ({
2424
timeFormat,
2525
durationGroups,
2626
onChangeDurationGroups,
27+
timestampLabels = [],
28+
durationLabels = [],
29+
allowCustomLabels,
2730
timestamps,
2831
width = 500,
2932
graphHeight = 300,
@@ -147,11 +150,14 @@ export const MainLayout = ({
147150
<Container width={width} themeColors={themeColors}>
148151
<Toolbar
149152
timestamps={timestamps}
153+
timestampLabels={timestampLabels}
154+
durationLabels={durationLabels}
150155
selectedTimestampIndex={selectedTimestampIndex}
151156
onChangeSelectedItemLabel={onChangeSelectedItemLabel}
152157
selectedDurationGroupIndex={activeDurationGroup}
153158
selectedDurationIndex={selectedDurationIndex}
154159
durationGroups={durationGroups}
160+
allowCustomLabels={allowCustomLabels}
155161
/>
156162
<Timeline
157163
timeFormat={timeFormat}

src/components/ReactTimeSeries/ReactTimeSeries.stories.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const SimpleTimeSeries = () => {
2424
})
2525
return (
2626
<ReactTimeSeries
27-
interface={{}}
27+
interface={{ allowCustomLabels: true }}
2828
sample={sample}
2929
onModifySample={setSample}
3030
/>
@@ -38,7 +38,7 @@ export const WithAudioURL = () => {
3838
})
3939
return (
4040
<ReactTimeSeries
41-
interface={{}}
41+
interface={{ allowCustomLabels: true }}
4242
sample={sample}
4343
onModifySample={setSample}
4444
/>
@@ -48,7 +48,7 @@ export const WithAudioURL = () => {
4848
export const ReallySimple = () => {
4949
return (
5050
<ReactTimeSeries
51-
interface={{}}
51+
interface={{ allowCustomLabels: true }}
5252
sample={{
5353
timeData: [
5454
{ time: 0, value: 0 },
@@ -66,6 +66,7 @@ export const SmallValues = () => {
6666
<ReactTimeSeries
6767
interface={{
6868
timeFormat: "none",
69+
allowCustomLabels: true,
6970
}}
7071
sample={{
7172
timeData: [
@@ -78,3 +79,20 @@ export const SmallValues = () => {
7879
/>
7980
)
8081
}
82+
83+
export const PredefinedLabelsOnly = () => {
84+
const [sample, setSample] = useState({
85+
audioUrl:
86+
"https://s3.amazonaws.com/datasets.workaround.online/voice-samples/001/voice.mp3",
87+
})
88+
return (
89+
<ReactTimeSeries
90+
interface={{
91+
durationLabels: ["duration1", "duration2"],
92+
timestampLabels: ["ts1", "ts2"],
93+
}}
94+
sample={sample}
95+
onModifySample={setSample}
96+
/>
97+
)
98+
}

src/components/ReactTimeSeries/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,9 @@ export const ReactTimeSeriesWithoutContext = ({
3939
timeFormat,
4040
// eslint-disable-next-line
4141
enabledTools = defaultEnabledTools,
42-
// eslint-disable-next-line
4342
durationLabels = emptyAr,
44-
// eslint-disable-next-line
4543
timestampLabels = emptyAr,
4644
graphs = defaultGraphs,
47-
// eslint-disable-next-line
4845
allowCustomLabels,
4946
} = iface
5047
let { timeData: sampleTimeData, audioUrl, csvUrl, annotation } = sample
@@ -205,6 +202,9 @@ export const ReactTimeSeriesWithoutContext = ({
205202
onChangeDurationGroups={onChangeDurationGroups}
206203
timestamps={timestamps}
207204
onChangeTimestamps={onChangeTimestamps}
205+
timestampLabels={timestampLabels}
206+
durationLabels={durationLabels}
207+
allowCustomLabels={allowCustomLabels}
208208
/>
209209
</div>
210210
)}

src/components/Toolbar/index.js

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import useEventCallback from "use-event-callback"
1414
import { useSetRecoilState } from "recoil"
1515
import { themeAtom } from "../../hooks/use-colors"
1616
import CreatableSelect from "react-select/creatable"
17+
import NormalSelect from "react-select"
1718
import LocationOnIcon from "@material-ui/icons/LocationOn"
1819
import TimelapseIcon from "@material-ui/icons/Timelapse"
1920
import 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

Comments
 (0)