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

Display LoadingIndicator when fetching points data #29

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 56 additions & 45 deletions src/scene.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useRef, useState } from "react";
import { Button, InputSlider, InputText, InputToggle } from "@czi-sds/components";
import { Button, InputSlider, InputText, InputToggle, LoadingIndicator } from "@czi-sds/components";
import { PointCanvas } from "./PointCanvas";
import { TrackManager, loadTrackManager } from "./TrackManager";

Expand All @@ -26,6 +26,7 @@ export default function Scene(props: SceneProps) {
const [curTime, setCurTime] = useState(0);
const [autoRotate, setAutoRotate] = useState(false);
const [playing, setPlaying] = useState(false);
const [loading, setLoading] = useState(false);

// Use references here for two things:
// * manage objects that should never change, even when the component re-renders
Expand Down Expand Up @@ -113,6 +114,8 @@ export default function Scene(props: SceneProps) {
// update the points when the array or timepoint changes
useEffect(() => {
setSelectedPoints({});
// show a loading indicator if the fetch takes longer than 10ms (avoid flicker)
const loadingTimer = setTimeout(() => setLoading(true), 10);
Comment on lines +117 to +118
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mostly works, but we might want to fine-tune the time here. My thought is that it should show if dropping frames in Play mode, so maybe the timeout should be related to the framerate.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good for enough for now.

By framerate, you mean framerate of the three.js animation loop? Any idea how to get that easily?

I found stats.js, which looks quite nice with some other things that might be nice to surface easily (though maybe chrome dev tools are sufficient). But we could just time the animation loop ourselves instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By framerate I meant the FPS set by the timer/interval used by the "Play" button, but you raise a good point. I think there are a few places where we're blocking the main thread and you can see the animation loop stall. I've seen this while adding tracks when "Spin" is on. Maybe we need another indicator for that. The little stats.js looks neat - I've seen that in some of the examples. It probably makes sense to include for debugging at least.

let ignore = false;
// TODO: this is a very basic attempt to prevent stale data
// in addition, we should debounce the input and verify the data is current
Expand All @@ -127,12 +130,17 @@ export default function Scene(props: SceneProps) {
console.debug("IGNORE SET points at time %d", curTime);
return;
}
clearTimeout(loadingTimer);
setLoading(false);
canvas.current?.setPointsPositions(data);
});
} else {
clearTimeout(loadingTimer);
setLoading(false);
console.debug("IGNORE FETCH points at time %d", curTime);
}
return () => {
clearTimeout(loadingTimer);
ignore = true;
};
}, [trackManager, curTime]);
Expand All @@ -150,55 +158,58 @@ export default function Scene(props: SceneProps) {
marks.push({ value: numTimes - 1, label: numTimes - 1 });

return (
<div ref={divRef}>
<div className="inputcontainer">
<InputText
id="url-input"
label="Zarr URL"
placeholder={DEFAULT_ZARR_URL.toString()}
value={dataUrl.toString()}
onChange={(e) => setDataUrl(new URL(e.target.value))}
fullWidth={true}
intent={trackManager ? "default" : "error"}
/>
<InputSlider
id="time-frame-slider"
aria-labelledby="input-slider-time-frame"
disabled={trackManager === undefined}
min={0}
max={numTimes - 1}
valueLabelDisplay="on"
onChange={(_, value) => setCurTime(value as number)}
marks={marks}
value={curTime}
/>
<div className="buttoncontainer">
<InputToggle
onLabel="Spin"
offLabel="Spin"
disabled={trackManager === undefined}
onChange={(e) => {
setAutoRotate((e.target as HTMLInputElement).checked);
}}
<div>
<div ref={divRef}>
<div className="inputcontainer">
aganders3 marked this conversation as resolved.
Show resolved Hide resolved
<InputText
id="url-input"
label="Zarr URL"
placeholder={DEFAULT_ZARR_URL.toString()}
value={dataUrl.toString()}
onChange={(e) => setDataUrl(new URL(e.target.value))}
fullWidth={true}
intent={trackManager ? "default" : "error"}
/>
<InputToggle
onLabel="Play"
offLabel="Play"
<InputSlider
id="time-frame-slider"
aria-labelledby="input-slider-time-frame"
disabled={trackManager === undefined}
onChange={(e) => {
setPlaying((e.target as HTMLInputElement).checked);
}}
min={0}
max={numTimes - 1}
valueLabelDisplay="on"
onChange={(_, value) => setCurTime(value as number)}
marks={marks}
value={curTime}
/>
<Button
disabled={trackManager === undefined}
sdsType="primary"
sdsStyle="rounded"
onClick={() => canvas.current?.removeAllTracks()}
>
Clear Tracks
</Button>
<div className="buttoncontainer">
<InputToggle
onLabel="Spin"
offLabel="Spin"
disabled={trackManager === undefined}
onChange={(e) => {
setAutoRotate((e.target as HTMLInputElement).checked);
}}
/>
<InputToggle
onLabel="Play"
offLabel="Play"
disabled={trackManager === undefined}
onChange={(e) => {
setPlaying((e.target as HTMLInputElement).checked);
}}
/>
<Button
disabled={trackManager === undefined}
sdsType="primary"
sdsStyle="rounded"
onClick={() => canvas.current?.removeAllTracks()}
>
Clear Tracks
</Button>
</div>
</div>
</div>
{loading && <LoadingIndicator sdsStyle="minimal" />}
</div>
);
}
Loading