refactor: cleanup some components
This commit is contained in:
12 files changed
+71
-40
No files matched your search
@@ -14,7 +14,12 @@ import Menu from "@mui/material/Menu";
|
||||
import MenuItem from "@mui/material/MenuItem";
|
||||
import useScrollTrigger from "@mui/material/useScrollTrigger";
|
||||
|
||||
import { Link, useLocation, useNavigate } from "@tanstack/react-router";
|
||||
import {
|
||||
Link,
|
||||
useLocation,
|
||||
useNavigate,
|
||||
useRouter,
|
||||
} from "@tanstack/react-router";
|
||||
|
||||
import { getUserViewsApi } from "@jellyfin/sdk/lib/utils/api/user-views-api";
|
||||
|
||||
@@ -79,14 +84,19 @@ const MemoizeBackButton = React.memo(BackButton);
|
||||
|
||||
export const AppBar = () => {
|
||||
const api = useApiInContext((s) => s.api);
|
||||
const createApi = useApiInContext((s) => s.createApi);
|
||||
|
||||
const navigate = useNavigate();
|
||||
|
||||
const [display, setDisplay] = useState(false);
|
||||
|
||||
const location = useLocation();
|
||||
const router = useRouter();
|
||||
|
||||
const user = useCentralStore((s) => s.currentUser);
|
||||
const [user, resetCurrentUser] = useCentralStore((s) => [
|
||||
s.currentUser,
|
||||
s.resetCurrentUser,
|
||||
]);
|
||||
const libraries = useQuery({
|
||||
queryKey: ["libraries"],
|
||||
queryFn: async () => {
|
||||
@@ -123,8 +133,12 @@ export const AppBar = () => {
|
||||
const handleLogout = async () => {
|
||||
console.log("Logging out user...");
|
||||
await api?.logout();
|
||||
createApi(api?.basePath ?? "", undefined);
|
||||
console.log(api);
|
||||
resetCurrentUser();
|
||||
delUser();
|
||||
sessionStorage.removeItem("accessToken");
|
||||
await router.invalidate();
|
||||
queryClient.clear();
|
||||
setAnchorEl(null);
|
||||
navigate({ to: "/login" });
|
||||
|
||||
@@ -5,7 +5,7 @@ import { useRouter } from "@tanstack/react-router";
|
||||
import React from "react";
|
||||
import QuickConnectButton from "../buttons/quickConnectButton";
|
||||
|
||||
export const AppBarBackOnly = () => {
|
||||
export default function AppBarBackOnly() {
|
||||
const { history } = useRouter();
|
||||
|
||||
const handleBack = () => {
|
||||
|
||||
@@ -141,9 +141,11 @@ const QuickConnectButton = (props: LoadingButtonProps) => {
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
useInterval(
|
||||
() => {
|
||||
if (quickConnectSecret) {
|
||||
console.log(checkForQuickConnect);
|
||||
checkQuickConnectStatus.mutate(quickConnectSecret);
|
||||
}
|
||||
},
|
||||
@@ -175,6 +177,7 @@ const QuickConnectButton = (props: LoadingButtonProps) => {
|
||||
open={Boolean(quickConnectCode)}
|
||||
onClose={() => {
|
||||
setQuickConnectCode(null);
|
||||
setCheckForQuickConnect(false);
|
||||
initQuickConnect.reset();
|
||||
checkQuickConnectStatus.reset();
|
||||
}}
|
||||
|
||||
@@ -1,23 +1,30 @@
|
||||
import { getSetting, setSetting } from "@/utils/storage/settings";
|
||||
import { FormControlLabel, Switch, Typography } from "@mui/material";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import React, { useState } from "react";
|
||||
|
||||
const SettingOption = ({
|
||||
setting,
|
||||
}: { setting: { key: string; name: string; description: string } }) => {
|
||||
const [settingVal, setSettingVal] = useState(
|
||||
async () => (await getSetting(setting.key)) ?? false,
|
||||
);
|
||||
const initValue = useQuery({
|
||||
queryKey: ["setting", setting.key],
|
||||
queryFn: async () => {
|
||||
const result = await getSetting(setting.key);
|
||||
return Boolean(result);
|
||||
},
|
||||
});
|
||||
const [settingVal, setSettingVal] = useState(initValue.data ?? false);
|
||||
return (
|
||||
<FormControlLabel
|
||||
control={
|
||||
<Switch
|
||||
color="primary"
|
||||
checked={settingVal}
|
||||
onChange={(e) => {
|
||||
setSetting(setting.key, e.target.checked);
|
||||
setSettingVal(e.target.checked);
|
||||
checked={Boolean(settingVal)}
|
||||
onChange={(_, checked) => {
|
||||
setSetting(setting.key, checked);
|
||||
setSettingVal(Boolean(checked));
|
||||
}}
|
||||
disabled={initValue.isLoading}
|
||||
name={setting.name}
|
||||
/>
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ export const queryClient = new QueryClient({
|
||||
},
|
||||
});
|
||||
|
||||
// biome-ignore lint/nursery/useComponentExportOnlyModules : This is a valid use case for a component export
|
||||
function ProviderWrapper() {
|
||||
const [api, createApi, jellyfinSDK] = useApiInContext((s) => [
|
||||
s.api,
|
||||
|
||||
@@ -59,6 +59,7 @@ export const Route = createFileRoute("/_api")({
|
||||
} else if (context.api) {
|
||||
if (context.api.accessToken && location.pathname !== "/login/manual") {
|
||||
try {
|
||||
console.info("AccessToken", context.api.accessToken);
|
||||
await getUserApi(context.api).getCurrentUser(); // Verify user is able to authenticate
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { AppBarBackOnly } from "@/components/appBar/backOnly";
|
||||
import AppBarBackOnly from "@/components/appBar/backOnly";
|
||||
import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api";
|
||||
import { Outlet, createFileRoute, redirect } from "@tanstack/react-router";
|
||||
import React from "react";
|
||||
|
||||
@@ -134,25 +134,20 @@ function LoginUser() {
|
||||
}}
|
||||
>
|
||||
<AvatarImage userId={userId} />
|
||||
<Typography
|
||||
textAlign="center"
|
||||
variant="h5"
|
||||
sx={{ opacity: 0.8 }}
|
||||
mb={2}
|
||||
mt={2}
|
||||
>
|
||||
Login as{" "}
|
||||
<div style={{ display: "inline-flex", margin: "1em 0" }}>
|
||||
<Typography variant="h5">Login as</Typography>
|
||||
<Typography
|
||||
variant="h5"
|
||||
style={{
|
||||
display: "inline",
|
||||
}}
|
||||
ml={0.5}
|
||||
fontWeight={700}
|
||||
className="gradient-text"
|
||||
>
|
||||
{userName}
|
||||
</Typography>
|
||||
</Typography>
|
||||
</div>
|
||||
<form onSubmit={(e) => handleLogin.mutate(e)}>
|
||||
<FormControl
|
||||
variant="outlined"
|
||||
|
||||
@@ -6,7 +6,7 @@ import Paper from "@mui/material/Paper";
|
||||
import Typography from "@mui/material/Typography";
|
||||
import { red } from "@mui/material/colors";
|
||||
|
||||
import { AppBarBackOnly } from "@/components/appBar/backOnly";
|
||||
import AppBarBackOnly from "@/components/appBar/backOnly";
|
||||
import {
|
||||
delServer,
|
||||
getAllServers,
|
||||
|
||||
@@ -1,27 +1,30 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
|
||||
function useInterval(callback: () => void, delay: number) {
|
||||
const savedCallback = useRef<null | (() => void)>(null);
|
||||
export default function useInterval(
|
||||
callback: () => void,
|
||||
delay: number | null,
|
||||
) {
|
||||
const savedCallback = useRef(callback);
|
||||
|
||||
// Remember the latest callback.
|
||||
// Remember the latest callback if it changes.
|
||||
useEffect(() => {
|
||||
if (savedCallback.current) {
|
||||
savedCallback.current = callback;
|
||||
}
|
||||
savedCallback.current = callback;
|
||||
}, [callback]);
|
||||
|
||||
// Set up the interval.
|
||||
useEffect(() => {
|
||||
function tick() {
|
||||
if (savedCallback.current) {
|
||||
savedCallback.current();
|
||||
}
|
||||
// Don't schedule if no delay is specified.
|
||||
// Note: 0 is a valid value for delay.
|
||||
if (delay === null) {
|
||||
return;
|
||||
}
|
||||
if (delay !== null) {
|
||||
const id = setInterval(tick, delay);
|
||||
return () => clearInterval(id);
|
||||
}
|
||||
}, [delay]);
|
||||
}
|
||||
|
||||
export default useInterval;
|
||||
const id = setInterval(() => {
|
||||
savedCallback.current();
|
||||
}, delay);
|
||||
|
||||
return () => {
|
||||
clearInterval(id);
|
||||
};
|
||||
}, [delay]);
|
||||
}
|
||||
@@ -26,6 +26,7 @@ type CentralStore = {
|
||||
clientVersion: string;
|
||||
currentUser: null | UserDto;
|
||||
fetchCurrentUser: (api: Api | undefined) => Promise<void>;
|
||||
resetCurrentUser: () => void;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -58,11 +59,14 @@ export const CentralProvider = ({ children }: { children: ReactNode }) => {
|
||||
clientVersion: version,
|
||||
currentUser: null,
|
||||
fetchCurrentUser: async (api) => {
|
||||
if (api) {
|
||||
if (api?.accessToken) {
|
||||
const user = (await getUserApi(api).getCurrentUser()).data;
|
||||
set((s) => ({ ...s, currentUser: user }));
|
||||
}
|
||||
},
|
||||
resetCurrentUser: () => {
|
||||
set((s) => ({ ...s, currentUser: null }));
|
||||
},
|
||||
})),
|
||||
);
|
||||
return (
|
||||
|
||||
Reference in new issue
Block a user