mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-30 07:51:17 -07:00
Enhancement: signout button (#6962)
This commit is contained in:
@@ -20,6 +20,9 @@
|
|||||||
"minutes": "m",
|
"minutes": "m",
|
||||||
"seconds": "s"
|
"seconds": "s"
|
||||||
},
|
},
|
||||||
|
"auth": {
|
||||||
|
"signout": "Sign out"
|
||||||
|
},
|
||||||
"widget": {
|
"widget": {
|
||||||
"missing_type": "Missing Widget Type: {{type}}",
|
"missing_type": "Missing Widget Type: {{type}}",
|
||||||
"api_error": "API Error",
|
"api_error": "API Error",
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import { signOut, useSession } from "next-auth/react";
|
||||||
|
import { useTranslation } from "next-i18next/pages";
|
||||||
|
import { MdLogout } from "react-icons/md";
|
||||||
|
|
||||||
|
export default function SignOut() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const { status } = useSession();
|
||||||
|
|
||||||
|
if (status !== "authenticated") {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div id="signout" className="rounded-full flex align-middle self-center mr-3">
|
||||||
|
<button type="button" onClick={() => signOut({ callbackUrl: "/" })} className="outline-hidden">
|
||||||
|
<MdLogout className="text-theme-800 dark:text-theme-200 w-6 h-6 cursor-pointer" aria-hidden="true" />
|
||||||
|
<span className="sr-only">{t("auth.signout")}</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
|
||||||
|
import { fireEvent, render } from "@testing-library/react";
|
||||||
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
const { signOut, useSession } = vi.hoisted(() => ({
|
||||||
|
signOut: vi.fn(),
|
||||||
|
useSession: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("next-auth/react", () => ({ signOut, useSession }));
|
||||||
|
vi.mock("next-i18next/pages", () => ({ useTranslation: () => ({ t: (key) => key }) }));
|
||||||
|
|
||||||
|
import SignOut from "./signout";
|
||||||
|
|
||||||
|
describe("components/toggles/signout", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each(["unauthenticated", "loading"])("renders nothing when session status is %s", (status) => {
|
||||||
|
useSession.mockReturnValue({ status });
|
||||||
|
|
||||||
|
const { container } = render(<SignOut />);
|
||||||
|
|
||||||
|
expect(container).toBeEmptyDOMElement();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders and signs out when authenticated", () => {
|
||||||
|
useSession.mockReturnValue({ status: "authenticated" });
|
||||||
|
|
||||||
|
const { getByRole } = render(<SignOut />);
|
||||||
|
fireEvent.click(getByRole("button"));
|
||||||
|
|
||||||
|
expect(signOut).toHaveBeenCalledWith({ callbackUrl: "/" });
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -35,6 +35,10 @@ const ColorToggle = dynamic(() => import("components/toggles/color"), {
|
|||||||
ssr: false,
|
ssr: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const SignOut = dynamic(() => import("components/toggles/signout"), {
|
||||||
|
ssr: false,
|
||||||
|
});
|
||||||
|
|
||||||
const Version = dynamic(() => import("components/version"), {
|
const Version = dynamic(() => import("components/version"), {
|
||||||
ssr: false,
|
ssr: false,
|
||||||
});
|
});
|
||||||
@@ -502,6 +506,7 @@ function Home({ initialSettings }) {
|
|||||||
<div id="style" className="flex w-full justify-end">
|
<div id="style" className="flex w-full justify-end">
|
||||||
{!settings?.color && <ColorToggle />}
|
{!settings?.color && <ColorToggle />}
|
||||||
<Revalidate />
|
<Revalidate />
|
||||||
|
<SignOut />
|
||||||
{!settings.theme && <ThemeToggle />}
|
{!settings.theme && <ThemeToggle />}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user