/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import CssBaseline from '@mui/material/CssBaseline';
import React, { useEffect, useLayoutEffect } from 'react';
import { Navigate, Outlet, Route, Routes, useLocation, useNavigate } from 'react-router-dom';
import { loadErrorMessages, loadDevMessages } from '@apollo/client/dev';
import { loadable } from 'react-lazily/loadable';
import Box from '@mui/material/Box';
import { AwaitableComponent } from 'awaitable-component';
import { AppContext } from '@/base/contexts/AppContext.tsx';
import { DefaultNavBar } from '@/features/navigation-bar/components/DefaultNavBar.tsx';
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { WebUIUpdateChecker } from '@/features/app-updates/components/WebUIUpdateChecker.tsx';
import { ServerUpdateChecker } from '@/features/app-updates/components/ServerUpdateChecker.tsx';
import { lazyLoadFallback } from '@/base/utils/LazyLoad.tsx';
import { ErrorBoundary } from '@/base/components/feedback/ErrorBoundary.tsx';
import { useNavBarContext } from '@/features/navigation-bar/NavbarContext.tsx';
import { AppRoutes } from '@/base/AppRoute.constants.ts';
import { useMetadataServerSettings } from '@/features/settings/services/ServerSettingsMetadata.ts';
import { MediaQuery } from '@/base/utils/MediaQuery.tsx';
import { BrowseTab } from '@/features/browse/Browse.types.ts';
import { LoginPage } from '@/features/authentication/screens/LoginPage.tsx';
import { AuthGuard } from '@/features/authentication/components/AuthGuard.tsx';
import { SearchParam } from '@/base/Base.types.ts';
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
import { ReactRouter } from '@/lib/react-router/ReactRouter.ts';
import { AuthManager } from '@/features/authentication/AuthManager.ts';
import { ImageProcessingType } from '@/features/settings/Settings.types.ts';
const { Browse } = loadable(() => import('@/features/browse/screens/Browse.tsx'), lazyLoadFallback);
const { DownloadQueue } = loadable(() => import('@/features/downloads/screens/DownloadQueue.tsx'), lazyLoadFallback);
const { Library } = loadable(() => import('@/features/library/screens/Library.tsx'), lazyLoadFallback);
const { Manga } = loadable(() => import('@/features/manga/screens/Manga.tsx'), lazyLoadFallback);
const { SearchAll } = loadable(() => import('@/features/global-search/screens/SearchAll.tsx'), lazyLoadFallback);
const { Settings } = loadable(() => import('@/features/settings/screens/Settings.tsx'), lazyLoadFallback);
const { About } = loadable(() => import('@/features/settings/screens/About.tsx'), lazyLoadFallback);
const { Backup } = loadable(() => import('@/features/backup/screens/Backup.tsx'), lazyLoadFallback);
const { CategorySettings } = loadable(
() => import('@/features/category/screens/CategorySettings.tsx'),
lazyLoadFallback,
);
const { SourceConfigure } = loadable(
() => import('@/features/source/configuration/screens/SourceConfigure.tsx'),
lazyLoadFallback,
);
const { SourceMangas } = loadable(() => import('@/features/source/browse/screens/SourceMangas.tsx'), lazyLoadFallback);
const { ExtensionInfo } = loadable(
() => import('@/features/extension/info/screens/ExtensionInfo.tsx'),
lazyLoadFallback,
);
const { Updates } = loadable(() => import('@/features/updates/screens/Updates.tsx'), lazyLoadFallback);
const { History } = loadable(() => import('@/features/history/screens/History.tsx'), lazyLoadFallback);
const { LibrarySettings } = loadable(() => import('@/features/library/screens/LibrarySettings.tsx'), lazyLoadFallback);
const { DownloadSettings } = loadable(
() => import('@/features/downloads/screens/DownloadSettings.tsx'),
lazyLoadFallback,
);
const { ImagesSettings } = loadable(() => import('@/features/settings/screens/ImagesSettings.tsx'), lazyLoadFallback);
const { ImageProcessingSetting } = loadable(
() => import('@/features/settings/screens/ImageProcessingSetting.tsx'),
lazyLoadFallback,
);
const { ServerSettings } = loadable(() => import('@/features/settings/screens/ServerSettings.tsx'), lazyLoadFallback);
const { BrowseSettings } = loadable(() => import('@/features/browse/screens/BrowseSettings.tsx'), lazyLoadFallback);
const { WebUISettings } = loadable(() => import('@/features/settings/screens/WebUISettings.tsx'), lazyLoadFallback);
const { Migrate } = loadable(() => import('@/features/migration/screens/Migrate.tsx'), lazyLoadFallback);
const { DeviceSetting } = loadable(() => import('@/features/device/screens/DeviceSetting.tsx'), lazyLoadFallback);
const { TrackingSettings } = loadable(
() => import('@/features/tracker/screens/TrackingSettings.tsx'),
lazyLoadFallback,
);
const { TrackerOAuthLogin } = loadable(
() => import('@/features/tracker/screens/TrackerOAuthLogin.tsx'),
lazyLoadFallback,
);
const { LibraryDuplicates } = loadable(
() => import('@/features/library/screens/LibraryDuplicates.tsx'),
lazyLoadFallback,
);
const { Appearance } = loadable(() => import('@/features/settings/screens/Appearance.tsx'), lazyLoadFallback);
const { GlobalReaderSettings } = loadable(
() => import('@/features/reader/settings/screens/GlobalReaderSettings.tsx'),
lazyLoadFallback,
);
const { More } = loadable(() => import('@/features/settings/screens/More.tsx'), lazyLoadFallback);
const { Reader } = loadable(() => import('@/features/reader/screens/Reader.tsx'), lazyLoadFallback);
const { HistorySettings } = loadable(() => import('@/features/history/screens/HistorySettings.tsx'), lazyLoadFallback);
if (import.meta.env.DEV) {
// Adds messages only in a dev environment
loadDevMessages();
loadErrorMessages();
}
const ScrollToTop = () => {
const { pathname } = useLocation();
useLayoutEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);
return null;
};
const InitialBackgroundRequests = () => {
// Load the full download status once on startup to fill the cache
requestManager.useGetDownloadStatus({ nextFetchPolicy: 'standby' });
const [fetchExtensionList] = requestManager.useExtensionListFetch();
useEffect(() => {
// Fetch extension list on startup to show up-to-date number of available extension updates in the navigation bar
// without having to open the extensions page.
fetchExtensionList().catch(defaultPromiseErrorHandler('App::InitialBackgroundRequests: extension list'));
}, []);
return null;
};
/**
* Creates permanent subscriptions to always have the latest data.
*
* E.g. in case a view is open, which does not subscribe to the download updates, finished downloads are never received
* and thus, data of existing chapters/mangas in the cache get outdated
*/
const BackgroundSubscriptions = () => {
const { isAuthRequired, accessToken } = AuthManager.useSession();
const skipConnection = isAuthRequired === null || (isAuthRequired && !accessToken);
requestManager.useDownloadSubscription({ skip: skipConnection });
requestManager.useUpdaterSubscription({ skip: skipConnection });
requestManager.useWebUIUpdateSubscription({ skip: skipConnection });
return null;
};
const ReactRouterSetter = () => {
const navigate = useNavigate();
useEffect(() => {
ReactRouter.setNavigateFn(navigate);
}, []);
return null;
};
const PrivateRoutes = () => {
const isAuthenticated = AuthManager.useIsAuthenticated();
if (!isAuthenticated) {
return (
);
}
return ;
};
const MainApp = () => {
const { navBarWidth, appBarHeight, bottomBarHeight } = useNavBarContext();
const isMobileWidth = MediaQuery.useIsMobileWidth();
const {
settings: { hideHistory },
} = useMetadataServerSettings();
return (
} />
}>
{/* General Routes */}
}
/>
}
/>
{isMobileWidth && } />}
} />
} />
}
/>
}
/>
} />
}
/>
} />
{/* TODO: deprecated - got moved to "settings/images/processing/downloads" */}
}
/>
} />
}
/>
}
/>
} />
} />
} />
} />
} />
} />
}
/>
} />
{/* Manga Routes */}
{/* TODO: deprecated - "source" and "extension" page got merged into "browse" */}
} />
} />
} />
} />
{/* TODO: deprecated - "source" and "extension" page got merged into "browse" */}
}
/>
} />
} />
} />
} />
} />
{!hideHistory && } />}
} />
} />
} />
} />
);
};
const ReaderApp = () => (
}>
} />
);
export const App: React.FC = () => (
} />
} />
);