Validate that namespace from URL exists before rendering any content #126
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a followup to #118 (finishes fixing https://issues.redhat.com/browse/MTRHO-123). In that PR we enforced that the namespace should never be undefined, but we didn't handle the case where the user somehow ended up with a defined namespace in the URL that doesn't actually exist.
This PR replaces the
useNamespaceContext
hook with auseValidatedNamespace
hook that actively tries to fetch the namespace specified in the URL, and it returns status and validity in addition to the namespace itself. Incidentally, it now gets the namespace directly fromuseRouteMatch
instead of from context, which makes the entireNamespaceContext
unnecessary, so this PR also removesNamespaceContext
.The AppImportsPage and the ImportWizard now show a loading state when the namespace is being validated, and if the namespace is found to be invalid the page automatically redirects to the "all-namespaces" URL (via the new
useRedirectOnInvalidNamespaceEffect
hook). This redirect is used so that we don't leave the invalid namespace in the URL at all, which would cause the whole console UI to try and use that namespace if you navigate away.These "all-namespaces" URLs currently render the "No project selected" message and direct the user to go select a namespace. (If they reach this view on the Imports page, clicking "select a project" takes them to the Projects page, and if they reach it on the wizard page, clicking that takes them to the Add page. This UX will be improved when we address #106).
Note that
useValidatedNamespace
is called all over the place, so theuseHostNamespaceQuery
within it will be mounted several times per page. This is okay because react-query will avoid unnecessary refetches and share the cached result. As a bonus though, the namespace will be re-validated whenever the page regains focus (as per react-query default behavior) so if the user goes and deletes their namespace and then returns to our page, they'll get kicked out.This PR also factors out new components for
LoadingEmptyState
andNoProjectEmptyState
since we're now duplicating this content on both pages.With this change, it should now be impossible to end up in a state where our pages are rendered with a missing or invalid namespace, either through user interaction or manually typing an invalid URL. You can test it by:
/app-imports/ns/fakenamespace
or/app-imports/new/ns/fakenamespace
and seeing that you get redirected to/.../all-namespaces
./app-imports/all-namespaces
.