mirror of
https://github.com/spacedriveapp/spacedrive.git
synced 2026-04-22 07:28:43 -04:00
* fda wip * clippy * add tauri invoke fns for FDA * fda wip * clippy * add tauri invoke fns for FDA * wip * fda wip * clippy * add tauri invoke fns for FDA * wip * wip * wip fda * remove imports * hopefully improve FDA * execute only on macos * ts * ts * Update Platform.tsx * Update AddLocationButton.tsx * remove console log * fix: fda and add unit tests * temp commit for Jake * add fda state and keybind handling (so the frontend is kept up to date) * update FDA * update imports * testing purposes * Jakes work * fix fda checks * work in progress (but not working) * remove dead files * attempt #2 * !!!temporarily enable devtools in prod * remove alert * show FDA screen but don't require it * add an FDA button to general client settings * Update AddLocationButton.tsx * remove dead code * unused dep * old errors * remove import * dead code * dead code + typesafety * eslint * remove fda dialog references * remove mp4 vid * hopefully fix onboarding for non-macos OSes * shorter nav --------- Co-authored-by: jake <77554505+brxken128@users.noreply.github.com>
111 lines
3.1 KiB
TypeScript
111 lines
3.1 KiB
TypeScript
import { useMemo } from 'react';
|
|
import { Navigate, Outlet, useMatches, type RouteObject } from 'react-router-dom';
|
|
import { currentLibraryCache, useCachedLibraries } from '@sd/client';
|
|
import { Dialogs, Toaster } from '@sd/ui';
|
|
import { RouterErrorBoundary } from '~/ErrorFallback';
|
|
|
|
import libraryRoutes from './$libraryId';
|
|
import onboardingRoutes from './onboarding';
|
|
import { RootContext } from './RootContext';
|
|
|
|
import './style.scss';
|
|
|
|
import { useOperatingSystem } from '~/hooks';
|
|
|
|
import { OperatingSystem } from '..';
|
|
|
|
const Index = () => {
|
|
const libraries = useCachedLibraries();
|
|
|
|
if (libraries.status !== 'success') return null;
|
|
|
|
if (libraries.data.length === 0) return <Navigate to="onboarding" replace />;
|
|
|
|
const currentLibrary = libraries.data.find((l) => l.uuid === currentLibraryCache.id);
|
|
|
|
const libraryId = currentLibrary ? currentLibrary.uuid : libraries.data[0]?.uuid;
|
|
|
|
return <Navigate to={`${libraryId}`} replace />;
|
|
};
|
|
|
|
const Wrapper = () => {
|
|
const rawPath = useRawRoutePath();
|
|
|
|
return (
|
|
<RootContext.Provider value={{ rawPath }}>
|
|
<Outlet />
|
|
<Dialogs />
|
|
<Toaster position="bottom-right" expand={true} />
|
|
</RootContext.Provider>
|
|
);
|
|
};
|
|
|
|
// NOTE: all route `Layout`s below should contain
|
|
// the `usePlausiblePageViewMonitor` hook, as early as possible (ideally within the layout itself).
|
|
// the hook should only be included if there's a valid `ClientContext` (so not onboarding)
|
|
|
|
export const routes = (os: OperatingSystem) => {
|
|
return [
|
|
{
|
|
element: <Wrapper />,
|
|
errorElement: <RouterErrorBoundary />,
|
|
children: [
|
|
{
|
|
index: true,
|
|
element: <Index />
|
|
},
|
|
{
|
|
path: 'onboarding',
|
|
lazy: () => import('./onboarding/Layout'),
|
|
children: onboardingRoutes(os)
|
|
},
|
|
{
|
|
path: ':libraryId',
|
|
lazy: () => import('./$libraryId/Layout'),
|
|
children: libraryRoutes
|
|
}
|
|
]
|
|
}
|
|
] satisfies RouteObject[];
|
|
};
|
|
|
|
/**
|
|
* Combines the `path` segments of the current route into a single string.
|
|
* This is useful for things like analytics, where we want the route path
|
|
* but not the values used in the route params.
|
|
*/
|
|
const useRawRoutePath = () => {
|
|
// `useMatches` returns a list of each matched RouteObject,
|
|
// we grab the last one as it contains all previous route segments.
|
|
const lastMatchId = useMatches().slice(-1)[0]?.id;
|
|
const os = useOperatingSystem();
|
|
|
|
const rawPath = useMemo(() => {
|
|
const [rawPath] =
|
|
lastMatchId
|
|
// Gets a list of the index of each route segment
|
|
?.split('-')
|
|
?.map((s) => parseInt(s))
|
|
// Gets the route object for each segment and appends the `path`, if there is one
|
|
?.reduce(
|
|
([rawPath, { children }], path) => {
|
|
// No `children`, nowhere to go
|
|
if (!children) return [rawPath, { children }] as any;
|
|
|
|
const item = children[path]!;
|
|
|
|
// No `path`, continue without adding to path
|
|
if (!('path' in item)) return [rawPath, item];
|
|
|
|
// `path` found, chuck it on the end
|
|
return [`${rawPath}/${item.path}`, item];
|
|
},
|
|
['' as string, { children: routes(os) }] as const
|
|
) ?? [];
|
|
|
|
return rawPath ?? '/';
|
|
}, [lastMatchId, os]);
|
|
|
|
return rawPath;
|
|
};
|