-
Notifications
You must be signed in to change notification settings - Fork 454
/
PhaseSelector.tsx
152 lines (143 loc) · 4.2 KB
/
PhaseSelector.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
import { date } from "shared/dates";
import clsx from "clsx";
import { phaseSummary } from "@/services/utils";
import Tooltip from "@/components/Tooltip/Tooltip";
import SelectField from "../Forms/SelectField";
import { useSnapshot } from "./SnapshotProvider";
export interface Props {
mutateExperiment?: () => void;
editPhases?: () => void;
newUi?: boolean;
}
export default function PhaseSelector({
mutateExperiment,
editPhases,
newUi,
}: Props) {
const { phase, setPhase, experiment } = useSnapshot();
const phaseOptions =
experiment?.phases?.map((phase, i) => ({
label: i + "",
value: i + "",
})) || [];
function formatPhase({ value, label }: { value: string; label: string }) {
if (value === "edit") {
return (
<div
className={clsx("cursor-pointer", {
"btn btn-outline-primary": newUi,
})}
>
{label}
</div>
);
}
const phaseIndex = parseInt(value) || 0;
const phase = experiment?.phases?.[phaseIndex];
if (!phase) return value;
if (newUi) {
return (
<>
<Tooltip
body={
<>
<div className="tooltip-phase-label font-weight-bold">
{phaseIndex + 1}: {phase.name}
</div>
<div className="mt-1">{phaseSummary(phase)}</div>
</>
}
tipPosition="right"
className="phase-selector-with-tooltip"
>
<>
<span className="font-weight-bold">{phaseIndex + 1}: </span>
<span className="date-label">
{date(phase.dateStarted ?? "")} —{" "}
{phase.dateEnded ? date(phase.dateEnded) : "now"}
</span>
</>
</Tooltip>
<div className="phase-selector-select-option cursor-pointer">
<span className="font-weight-bold">{phaseIndex + 1}: </span>
<span className="phase-label font-weight-bold">{phase.name}</span>
<div className="break mt-1" />
<span className="date-label mt-1">
{date(phase.dateStarted ?? "")} —{" "}
{phase.dateEnded ? date(phase.dateEnded) : "now"}
</span>
<div className="phase-summary text-muted small">
{phaseSummary(phase)}
</div>
</div>
</>
);
}
return (
<div className="d-flex">
<div className="mr-2">{phaseIndex + 1}:</div>
<div className="small">
<div>{phase.name === "Main" ? phaseSummary(phase) : phase.name}</div>
<div>
<strong>{date(phase.dateStarted ?? "")}</strong> to{" "}
<strong>{phase.dateEnded ? date(phase.dateEnded) : "now"}</strong>
</div>
</div>
</div>
);
}
const selectOptions =
editPhases && mutateExperiment
? [
{
label: "Phases",
value: "",
options: phaseOptions,
},
{
label: "",
value: "",
options: [
{
label: "Edit Phases",
value: "edit",
},
],
},
]
: phaseOptions;
return (
<div>
{newUi && <div className="uppercase-title text-muted">Phase</div>}
{selectOptions.length > 1 ? (
<SelectField
options={selectOptions}
value={phase + ""}
onChange={(value) => {
if (mutateExperiment && editPhases && value === "edit") {
editPhases();
return;
}
setPhase(parseInt(value) || 0);
}}
sort={false}
label={newUi ? undefined : "Phase"}
labelClassName="mr-2"
containerClassName={
newUi ? "phase-selector select-dropdown-underline pr-5" : ""
}
isSearchable={false}
formatOptionLabel={formatPhase}
/>
) : (
<div className="dropdown-underline-disabled text-dark">
{selectOptions.length === 1 ? (
formatPhase(selectOptions[0])
) : (
<em>No phases</em>
)}
</div>
)}
</div>
);
}