Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use defaultValue for Zarr URL to allow more edits #50

Merged
merged 10 commits into from
Mar 13, 2024
38 changes: 28 additions & 10 deletions src/scene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function Scene(props: SceneProps) {
const [playing, setPlaying] = useState(false);

// Other state that is not or does not need to be persisted.
const [trackManager, setTrackManager] = useState<TrackManager>();
const [trackManager, setTrackManager] = useState<TrackManager | null>(null);
const [numTimes, setNumTimes] = useState(0);
const [loading, setLoading] = useState(false);
const { selectedPoints, setSelectedPoints } = useSelectionBox(canvas.current);
Expand Down Expand Up @@ -115,12 +115,11 @@ export default function Scene(props: SceneProps) {
const trackManager = loadTrackManager(dataUrl.toString());
// TODO: add clean-up by returning another closure
trackManager.then((tm: TrackManager | null) => {
if (!tm) return;
setTrackManager(tm);
setNumTimes(tm.points.shape[0]);
setNumTimes(tm?.points.shape[0] || numTimes);
// Defend against the case when a curTime valid for previous data
// is no longer valid.
setCurTime(Math.min(curTime, tm.points.shape[0] - 1));
setCurTime(Math.min(curTime, tm?.points.shape[0] - 1 || 0));
aganders3 marked this conversation as resolved.
Show resolved Hide resolved
});
}, [dataUrl]);

Expand Down Expand Up @@ -176,6 +175,12 @@ export default function Scene(props: SceneProps) {
setLoading(false);
console.debug("IGNORE FETCH points at time %d", curTime);
}

// stop playback if there is no data
if (!trackManager) {
setPlaying(false);
andy-sweet marked this conversation as resolved.
Show resolved Hide resolved
}

return () => {
clearTimeout(loadingTimer);
ignore = true;
Expand All @@ -202,15 +207,28 @@ export default function Scene(props: SceneProps) {
id="url-input"
label="Zarr URL"
placeholder={initialViewerState.dataUrl.toString()}
value={dataUrl.toString()}
onChange={(e) => setDataUrl(new URL(e.target.value))}
defaultValue={initialViewerState.dataUrl.toString()}
onChange={(e) => {
const urlString = e.target.value;
let url;
try {
url = new URL(urlString);
} catch (error) {
if (urlString.length > 0) {
console.error("Failed to parse URL %s:", e.target.value);
}
setTrackManager(null);
return;
}
aganders3 marked this conversation as resolved.
Show resolved Hide resolved
setDataUrl(url);
}}
fullWidth={true}
intent={trackManager ? "default" : "error"}
/>
<InputSlider
id="time-frame-slider"
aria-labelledby="input-slider-time-frame"
disabled={trackManager === undefined}
disabled={!trackManager}
min={0}
max={numTimes - 1}
valueLabelDisplay="on"
Expand All @@ -223,7 +241,7 @@ export default function Scene(props: SceneProps) {
onLabel="Spin"
offLabel="Spin"
checked={autoRotate}
disabled={trackManager === undefined}
disabled={!trackManager}
onChange={(e) => {
setAutoRotate((e.target as HTMLInputElement).checked);
}}
Expand All @@ -232,13 +250,13 @@ export default function Scene(props: SceneProps) {
onLabel="Play"
offLabel="Play"
checked={playing}
disabled={trackManager === undefined}
disabled={!trackManager}
onChange={(e) => {
setPlaying((e.target as HTMLInputElement).checked);
}}
/>
<Button
disabled={trackManager === undefined}
disabled={!trackManager}
sdsType="primary"
sdsStyle="rounded"
onClick={() => canvas.current?.removeAllTracks()}
Expand Down
Loading