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

Disable compile buttons if no server connection #225

Merged
merged 3 commits into from
Oct 8, 2024

Conversation

WardBrian
Copy link
Collaborator

Closes #194

  • Moves the connection status inside the context
  • Adds a tooltip explaining why the button is disabled

image

Comment on lines +18 to +45
const useIsConnected = (stanWasmServerUrl: string) => {
const probeUrl = `${stanWasmServerUrl}/probe`;
const [isConnected, setIsConnected] = useState<boolean>(false);
const [retryCode, setRetryCode] = useState<number>(0);
const retryConnection = useCallback(() => {
setRetryCode((r) => r + 1);
}, []);
useEffect(() => {
setIsConnected(false);
if (!probeUrl.startsWith("http://") && !probeUrl.startsWith("https://")) {
// important to do this check because otherwise fetch may succeed because
// the server of this web app may respond with success
return;
}
(async () => {
try {
const response = await fetch(probeUrl);
if (response.status === 200) {
setIsConnected(true);
}
} catch (err) {
setIsConnected(false);
}
})();
}, [probeUrl, retryCode]);
return { isConnected, retryConnection };
};

Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't mean to open up a can of worms here by suggesting a rewrite of working code, but I'm wondering--now that this is moved into the CompileContext--whether this still needs to be a hook, or if it'd be more natural/clear to write it as just a couple functions (with the state/useState held direct on the CompileContextProvider, perhaps).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I kinda liked the encapsulation, but it does seem a bit odd to have a custom hook which is only used in one place, with that one place being it's own hook-like-thing.

Happy to go either way

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't have strong feelings about it, and it's working as is, so it's probably not worth any mental energy to re-envision it.

You can just ship it 😸

@WardBrian WardBrian merged commit b0f9fd2 into main Oct 8, 2024
2 checks passed
@WardBrian WardBrian deleted the feature/194-disable-compile-button-no-connection branch October 8, 2024 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Disable compile button if not connected to compilation server
2 participants