Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | import { ButtonProps, styled, Typography } from "@mui/material";
import {
InfiniteData,
useInfiniteQuery,
useMutation,
useQueryClient,
} from "@tanstack/react-query";
import Alert from "components/Alert";
import Button from "components/Button";
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import { activeLoginsKey } from "features/queryKeys";
import { RpcError } from "grpc-web";
import { AUTH, GLOBAL } from "i18n/namespaces";
import { useTranslation } from "next-i18next";
import { ListActiveSessionsRes } from "proto/account_pb";
import { service } from "service";
import { timestamp2Date } from "utils/date";
import LoginCard from "./LoginCard";
const StyledLoginsContainer = styled("div")(({ theme }) => ({
display: "flex",
flexDirection: "column",
padding: theme.spacing(4),
margin: "0 auto",
width: "100%",
[theme.breakpoints.up("md")]: {
width: "50%",
},
}));
const StyledButton = styled(Button)<ButtonProps>(({ theme }) => ({
marginTop: theme.spacing(1),
marginBottom: theme.spacing(1),
}));
export default function LoginsPage() {
const { t } = useTranslation([GLOBAL, AUTH]);
const queryClient = useQueryClient();
const {
isLoading,
error,
data,
hasNextPage,
fetchNextPage,
isFetchingNextPage,
} = useInfiniteQuery<
ListActiveSessionsRes.AsObject,
RpcError,
InfiniteData<ListActiveSessionsRes.AsObject>,
[typeof activeLoginsKey],
string
>({
queryKey: [activeLoginsKey],
queryFn: ({ pageParam }) => service.account.listActiveSessions(pageParam),
initialPageParam: "",
getNextPageParam: (lastPage) => lastPage.nextPageToken || undefined,
});
const sessions = data?.pages.flatMap((page) => page.activeSessionsList) || [];
const {
error: logoutAllError,
isPending: logoutAllIsLoading,
mutate: logoutAll,
} = useMutation({
mutationFn: async () => {
await service.account.logOutOtherSessions(true);
},
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: [activeLoginsKey],
});
},
});
return (
<StyledLoginsContainer>
<Typography variant="h1" gutterBottom>
{t("auth:active_logins.heading")}
</Typography>
<Typography variant="body1" gutterBottom>
{t("auth:active_logins.description")}
</Typography>
{error && <Alert severity="error">{error.message}</Alert>}
{logoutAllError && (
<Alert severity="error">{logoutAllError?.message}</Alert>
)}
{isLoading ? (
<CenteredSpinner />
) : (
sessions.map((session) => (
<LoginCard
key={timestamp2Date(session.created!).toString()}
session={session}
/>
))
)}
{hasNextPage && (
<StyledButton
loading={isFetchingNextPage}
onClick={() => fetchNextPage()}
>
{t("global:load_more")}
</StyledButton>
)}
<StyledButton
color="secondary"
loading={logoutAllIsLoading}
onClick={() => logoutAll()}
>
{t("auth:active_logins.log_out_of_all_session")}
</StyledButton>
</StyledLoginsContainer>
);
}
|