import { createContext, FunctionComponent, lazy, useContext, useMemo, } from "react"; import { createBrowserRouter, RouterProvider } from "react-router-dom"; import { faClock, faCogs, faExclamationTriangle, faFileExcel, faFilm, faLaptop, faPlay, } from "@fortawesome/free-solid-svg-icons"; import { useBadges } from "@/apis/hooks"; import { useEnabledStatus } from "@/apis/hooks/site"; import App from "@/App"; import { Lazy } from "@/components/async"; import Authentication from "@/pages/Authentication"; import BlacklistMoviesView from "@/pages/Blacklist/Movies"; import BlacklistSeriesView from "@/pages/Blacklist/Series"; import Episodes from "@/pages/Episodes"; import NotFound from "@/pages/errors/NotFound"; import MoviesHistoryView from "@/pages/History/Movies"; import SeriesHistoryView from "@/pages/History/Series"; import MovieView from "@/pages/Movies"; import MovieDetailView from "@/pages/Movies/Details"; import MovieMassEditor from "@/pages/Movies/Editor"; import SeriesView from "@/pages/Series"; import SeriesMassEditor from "@/pages/Series/Editor"; import SettingsGeneralView from "@/pages/Settings/General"; import SettingsLanguagesView from "@/pages/Settings/Languages"; import SettingsNotificationsView from "@/pages/Settings/Notifications"; import SettingsProvidersView from "@/pages/Settings/Providers"; import SettingsRadarrView from "@/pages/Settings/Radarr"; import SettingsSchedulerView from "@/pages/Settings/Scheduler"; import SettingsSonarrView from "@/pages/Settings/Sonarr"; import SettingsSubtitlesView from "@/pages/Settings/Subtitles"; import SettingsUIView from "@/pages/Settings/UI"; import SystemAnnouncementsView from "@/pages/System/Announcements"; import SystemBackupsView from "@/pages/System/Backups"; import SystemLogsView from "@/pages/System/Logs"; import SystemProvidersView from "@/pages/System/Providers"; import SystemReleasesView from "@/pages/System/Releases"; import SystemTasksView from "@/pages/System/Tasks"; import WantedMoviesView from "@/pages/Wanted/Movies"; import WantedSeriesView from "@/pages/Wanted/Series"; import { Environment } from "@/utilities"; import Redirector from "./Redirector"; import { RouterNames } from "./RouterNames"; import { CustomRouteObject } from "./type"; const HistoryStats = lazy( () => import("@/pages/History/Statistics/HistoryStats"), ); const SystemStatusView = lazy(() => import("@/pages/System/Status")); function useRoutes(): CustomRouteObject[] { const { data } = useBadges(); const { sonarr, radarr } = useEnabledStatus(); return useMemo( () => [ { path: "/", element: , children: [ { index: true, element: , }, { icon: faPlay, name: "Series", path: "series", badge: data?.sonarr_signalr, hidden: !sonarr, children: [ { index: true, element: , }, { path: "edit", hidden: true, element: , }, { path: ":id", element: , }, ], }, { icon: faFilm, name: "Movies", path: "movies", badge: data?.radarr_signalr, hidden: !radarr, children: [ { index: true, element: , }, { path: "edit", hidden: true, element: , }, { path: ":id", element: , }, ], }, { icon: faClock, name: "History", path: "history", hidden: !sonarr && !radarr, children: [ { path: "series", name: "Episodes", hidden: !sonarr, element: , }, { path: "movies", name: "Movies", hidden: !radarr, element: , }, { path: "stats", name: "Statistics", element: ( ), }, ], }, { icon: faExclamationTriangle, name: "Wanted", path: "wanted", hidden: !sonarr && !radarr, children: [ { name: "Episodes", path: "series", badge: data?.episodes, hidden: !sonarr, element: , }, { name: "Movies", path: "movies", badge: data?.movies, hidden: !radarr, element: , }, ], }, { icon: faFileExcel, name: "Blacklist", path: "blacklist", hidden: !sonarr && !radarr, children: [ { path: "series", name: "Episodes", hidden: !sonarr, element: , }, { path: "movies", name: "Movies", hidden: !radarr, element: , }, ], }, { icon: faCogs, name: "Settings", path: "settings", children: [ { path: "general", name: "General", element: , }, { path: "languages", name: "Languages", element: , }, { path: "providers", name: "Providers", element: , }, { path: "subtitles", name: "Subtitles", element: , }, { path: "sonarr", name: "Sonarr", element: , }, { path: "radarr", name: "Radarr", element: , }, { path: "notifications", name: "Notifications", element: ( ), }, { path: "scheduler", name: "Scheduler", element: , }, { path: "ui", name: "UI", element: , }, ], }, { icon: faLaptop, name: "System", path: "system", children: [ { path: "tasks", name: "Tasks", element: , }, { path: "logs", name: "Logs", element: , }, { path: "providers", name: "Providers", badge: data?.providers, element: , }, { path: "backup", name: "Backups", element: , }, { path: "status", name: "Status", badge: data?.status, element: ( ), }, { path: "releases", name: "Releases", element: , }, { path: "announcements", name: "Announcements", badge: data?.announcements, element: , }, ], }, { path: "*", hidden: true, element: , }, ], }, { path: RouterNames.Auth, hidden: true, element: , }, ], [ data?.episodes, data?.movies, data?.providers, data?.sonarr_signalr, data?.radarr_signalr, data?.announcements, data?.status, radarr, sonarr, ], ); } const RouterItemContext = createContext([]); export const Router: FunctionComponent = () => { const routes = useRoutes(); // TODO: Move this outside the function component scope const router = useMemo( () => createBrowserRouter(routes, { basename: Environment.baseUrl }), [routes], ); return ( ); }; export function useRouteItems() { return useContext(RouterItemContext); }