Skip to content

Commit

Permalink
[ENG-1338] Fix fresh Spacedrive install failing to start due to attem…
Browse files Browse the repository at this point in the history
…pting to query a nonexistent Library (#1649)

Fix Spacedrive failing to start due to attempting to query a nonexistent Library
 - Rename useShoudRedirect to useRedirectToNewLocations
 - Improve behaviour for the immedite redirection after adding a new location
  • Loading branch information
HeavenVolkoff authored Oct 21, 2023
1 parent e51a58f commit ce56898
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 79 deletions.
5 changes: 2 additions & 3 deletions interface/app/$libraryId/Explorer/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
type ExplorerItem,
type ExplorerLayout,
type ExplorerSettings,
type SortOrder,
JobGroup
type SortOrder
} from '@sd/client';

export enum ExplorerKind {
Expand Down Expand Up @@ -122,7 +121,7 @@ const state = {
tagAssignMode: false,
showInspector: false,
showMoreInfo: false,
jobsToRedirect: [] as {locationId: number | null}[],
newLocationToRedirect: null as null | number,
mediaPlayerVolume: 0.7,
newThumbnails: proxySet() as Set<string>,
cutCopyState: { type: 'Idle' } as CutCopyState,
Expand Down
13 changes: 8 additions & 5 deletions interface/app/$libraryId/Layout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import clsx from 'clsx';
import { Suspense, useEffect, useMemo, useRef, useState } from 'react';
import { Navigate, Outlet, useLocation, useNavigate, useResolvedPath } from 'react-router-dom';
import { Suspense, useEffect, useMemo, useRef } from 'react';
import { Navigate, Outlet, useNavigate } from 'react-router-dom';
import {
ClientContextProvider,
initPlausible,
Expand All @@ -16,10 +16,11 @@ import { LibraryIdParamsSchema } from '~/app/route-schemas';
import {
useKeybindEventHandler,
useOperatingSystem,
useRedirectToNewLocation,
useShowControls,
useWindowState,
useZodRouteParams
} from '~/hooks';
import { useWindowState } from '~/hooks/useWindowState';
import { usePlatform } from '~/util/Platform';

import { QuickPreviewContextProvider } from '../Explorer/QuickPreview/Context';
Expand All @@ -30,7 +31,7 @@ const Layout = () => {
const { libraries, library } = useClientContext();
const os = useOperatingSystem();
const showControls = useShowControls();
const platform = usePlatform();
const { platform } = usePlatform();
const windowState = useWindowState();

useKeybindEventHandler(library?.uuid);
Expand All @@ -41,7 +42,7 @@ const Layout = () => {
const layoutRef = useRef<HTMLDivElement>(null);

initPlausible({
platformType: platform.platform === 'tauri' ? 'desktop' : 'web',
platformType: platform === 'tauri' ? 'desktop' : 'web',
buildInfo: buildInfo?.data
});

Expand All @@ -50,6 +51,8 @@ const Layout = () => {
usePlausiblePageViewMonitor({ currentPath: rawPath });
usePlausiblePingMonitor({ currentPath: rawPath });

useRedirectToNewLocation();

useEffect(() => {
const interval = setInterval(() => {
plausibleEvent({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useCallback, useEffect, useMemo } from 'react';
import { Controller, get } from 'react-hook-form';
import { useNavigate } from 'react-router';
import { useDebouncedCallback } from 'use-debounce';
import {
extractInfoRSPCError,
Expand All @@ -11,7 +10,7 @@ import {
useZodForm
} from '@sd/client';
import { CheckBox, Dialog, ErrorMessage, Label, toast, useDialog, UseDialogProps, z } from '@sd/ui';
import { getExplorerStore, useExplorerStore } from '~/app/$libraryId/Explorer/store';
import { getExplorerStore } from '~/app/$libraryId/Explorer/store';
import Accordion from '~/components/Accordion';
import { useCallbackToWatchForm } from '~/hooks';
import { usePlatform } from '~/util/Platform';
Expand Down Expand Up @@ -60,7 +59,6 @@ export const AddLocationDialog = ({
const relinkLocation = useLibraryMutation('locations.relink');
const listIndexerRules = useLibraryQuery(['locations.indexer_rules.list']);
const addLocationToLibrary = useLibraryMutation('locations.addLibrary');
const explorerStore = useExplorerStore();

// This is required because indexRules is undefined on first render
const indexerRulesIds = useMemo(
Expand Down Expand Up @@ -123,14 +121,10 @@ export const AddLocationDialog = ({
default:
throw new Error('Unimplemented custom remote error handling');
}
if (shouldRedirect) {
getExplorerStore().jobsToRedirect = [
{ locationId: id },
...explorerStore.jobsToRedirect
];
}

if (shouldRedirect) getExplorerStore().newLocationToRedirect = id;
},
[createLocation, relinkLocation, addLocationToLibrary, submitPlausibleEvent, explorerStore]
[createLocation, relinkLocation, addLocationToLibrary, submitPlausibleEvent]
);

const handleAddError = useCallback(
Expand Down
3 changes: 1 addition & 2 deletions interface/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Navigate, Outlet, useMatches, type RouteObject } from 'react-router-dom
import { currentLibraryCache, useCachedLibraries, useInvalidateQuery } from '@sd/client';
import { Dialogs, Toaster } from '@sd/ui';
import { RouterErrorBoundary } from '~/ErrorFallback';
import { useShouldRedirect, useTheme } from '~/hooks';
import { useTheme } from '~/hooks';

import libraryRoutes from './$libraryId';
import onboardingRoutes from './onboarding';
Expand All @@ -28,7 +28,6 @@ const Index = () => {
const Wrapper = () => {
useInvalidateQuery();
useTheme();
useShouldRedirect();

const rawPath = useRawRoutePath();

Expand Down
3 changes: 2 additions & 1 deletion interface/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ export * from './useIsTextTruncated';
export * from './useKeyMatcher';
export * from './useKeyCopyCutPaste';
export * from './useMouseNavigate';
export * from './useShouldRedirect';
export * from './useRedirectToNewLocation';
export * from './useWindowState';
36 changes: 36 additions & 0 deletions interface/hooks/useRedirectToNewLocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useNavigate } from 'react-router';
import { useLibraryQuery } from '@sd/client';
import { getExplorerStore, useExplorerStore } 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 { newLocationToRedirect: newLocation } = useExplorerStore();
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.location.id === newLocation &&
(j.completed_task_count > 0 || j.completed_at != null)
);

if (hasIndexerJob) {
navigate(`/${libraryId}/location/${newLocation}`);
getExplorerStore().newLocationToRedirect = null;
}
};
58 changes: 0 additions & 58 deletions interface/hooks/useShouldRedirect.ts

This file was deleted.

1 comment on commit ce56898

@vercel
Copy link

@vercel vercel bot commented on ce56898 Oct 21, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.