Files
spacedrive/interface/hooks/useRedirectToNewLocation.ts
Vítor Vasconcellos dcb4ace969 New test: Add location (#2222)
* New test: Add location
 - Add script to download some test files to allows tests to have a stable path for creating locations
 - Improve onboarding test
 - Add custom Cypress commands for fastOnboarding and deleteLibrary
 - Add some documentation related to tests to CONTRIBUTING.md
 - Update some deps

* Download small test-data for Cypress CI

* Replace globstar with find to make it compatible with bash 3.x

* Fix react too many re-renders
 - add-location test should now pass successfully

* Make job model task text match less flaky

* Check if we were redirected to onboarding after Library was deleted
 - Check if sidebar entry was created after adding a location
 - Put route regex's into a separate file
 - Make onboarding test ensure that no Library exists before running

* Enable test retries
 - Log cypress default config during test run

* Run tests under webkit
 - Pass CI envvar to tests to ensure correct cypress config

* Attempt fix CI config again

* Remove single use checkUrlIsLibrary function
2024-03-23 21:24:16 +00:00

40 lines
1.2 KiB
TypeScript

import { useEffect } from 'react';
import { useNavigate } from 'react-router';
import { useLibraryQuery, useSelector } from '@sd/client';
import { explorerStore } from '~/app/$libraryId/Explorer/store';
import { LibraryIdParamsSchema } from '../app/route-schemas';
import { useZodRouteParams } from './useZodRouteParams';
/**
* When a user adds a location and checks the should redirect box,
* this hook will redirect them to the location
* once the indexer has been invoked
*/
export const useRedirectToNewLocation = () => {
const navigate = useNavigate();
const { libraryId } = useZodRouteParams(LibraryIdParamsSchema);
const newLocation = useSelector(explorerStore, (s) => s.newLocationToRedirect);
const { data: jobGroups } = useLibraryQuery(['jobs.reports'], {
enabled: newLocation != null,
refetchOnWindowFocus: false
});
const hasIndexerJob = jobGroups
?.flatMap((j) => j.jobs)
.some(
(j) =>
j.name === 'indexer' &&
(j.metadata as any)?.location.id === newLocation &&
(j.completed_task_count > 0 || j.completed_at != null)
);
useEffect(() => {
if (hasIndexerJob) {
navigate(`/${libraryId}/location/${newLocation}`);
explorerStore.newLocationToRedirect = null;
}
}, [hasIndexerJob, libraryId, newLocation, navigate]);
};