Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions assets/pictograms/account.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions locales/en/nexus_integration.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
{
"_comment": "Nexus Mods integration strings - to be populated during migration",
"login_prompt": {
"title": "Log in for the full experience",
"description": "The button below will open the Nexus Mods website, please click “authorise” or log in / register if required.",
"login": "Log in to Vortex",
"no_thanks": "No, thanks",
"dont_show_again": "Don’t show this again"
},
"login_dialog": {
"address": "Login address",
"progress": {
"title": "Log in or register on the Nexus Mods website",
"description": "Look out for a browser window opening and click “authorise” or log in / register if required.",
"copy_address": "The button didn’t work? Copy the below address into your browser",
"still_not_working": "Still didn’t work?",
"login_with_token": "Login with token"
},
"token": {
"title": "Log in with token",
"description": "Click “authorise” on the website and you’ll be given a token. Paste it below and click Save.",
"step_open": "Don’t have a token yet? Copy this address into your browser and log in / register if required.",
"step_paste": "Token",
"invalid": "Your token wasn’t recognised. Copy it again using the button on the website.",
"failed": "Logging in with this token didn’t work: {{error}}",
"save": "Save"
}
},
"premium_upsell": {
"benefits_title": "With Premium you get:",
"benefits": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ vi.mock("react-redux", () => ({
useSelector: () => undefined,
}));

vi.mock("@/views/components/Header/premium/useNexusLogin.hook", () => ({
useNexusLogin: () => vi.fn(),
}));

vi.mock("../hooks/healthCheckTracker", () => ({
createHealthCheckTracker: () => new Proxy({}, { get: () => vi.fn() }),
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useMemo, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { useDispatch, useSelector } from "react-redux";

import { setDialogVisible, setOpenMainPage, setSettingsPage } from "@/actions/session";
import { setOpenMainPage, setSettingsPage } from "@/actions/session";
import type { IExtensionApi } from "@/types/IExtensionContext";
import type { IState } from "@/types/IState";
import { PremiumBadge } from "@/ui/components/badges/premium_badge/PremiumBadge";
Expand All @@ -16,8 +16,8 @@ import { Toolbar } from "@/ui/components/toolbar/Toolbar";
import type { IToolbarAction } from "@/ui/components/toolbar/ToolbarGroup";
import { ToolbarGroup } from "@/ui/components/toolbar/ToolbarGroup";
import { Typography } from "@/ui/components/typography/Typography";
import { UserCanceled } from "@/util/CustomErrors";
import { useRelativeTime } from "@/util/useRelativeTime";
import { useNexusLogin } from "@/views/components/Header/premium/useNexusLogin.hook";
import { Page } from "@/views/components/Page/Page";
import { PageHeader } from "@/views/components/Page/PageHeader";
import { PageScroll } from "@/views/components/Page/PageScroll";
Expand Down Expand Up @@ -111,6 +111,9 @@ const collectInstallAllItems = (
const HealthCheckPage = ({ api, onRefresh, active, registerReset }: IHealthCheckPageProps) => {
const { t } = useTranslation(["health_check", "common"]);
const dispatch = useDispatch();
// Logging in is a prerequisite for the Nexus-backed checks rather than a fix for an
// issue, so it goes through the same OAuth flow as the header's login button.
const requestLogin = useNexusLogin();
const [selected, setSelected] = useState<IListedEntry | null>(null);
const [selectedTab, setSelectedTab] = useState("active");

Expand Down Expand Up @@ -375,21 +378,6 @@ const HealthCheckPage = ({ api, onRefresh, active, registerReset }: IHealthCheck
</div>
);

// Logging in is a prerequisite for the Nexus-backed checks rather than a fix for an
// issue, so it goes through the same OAuth flow as the header's profile button.
const requestLogin = () => {
dispatch(setDialogVisible("login-dialog"));

api.events.emit("request-nexus-login", (err: Error) => {
if (err != null && !(err instanceof UserCanceled)) {
api.showErrorNotification?.("Login Failed", err, {
id: "failed-get-nexus-key",
allowReport: false,
});
}
});
};

const loggedOutState = (
<NoResults
className="py-24"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";

import { Button } from "@/ui/components/button/Button";
import { Input } from "@/ui/components/form/input/Input";
import { log } from "@/util/log";

/** A read-only address with a button that copies it, which briefly shows a tick once it has. */
export const CopyField = ({ label, value }: { label: string; value: string }) => {
const { t } = useTranslation();
const [copied, setCopied] = useState(false);

useEffect(() => {
if (!copied) {
return undefined;
}
const timer = setTimeout(() => setCopied(false), 2000);
return () => clearTimeout(timer);
}, [copied]);

const copy = () => {
window.api.clipboard
.writeText(value)
.then(() => setCopied(true))
.catch((err: unknown) => log("warn", "failed to copy the login address", { error: err }));
};

return (
<div className="flex w-full items-center gap-x-2">
<Input aria-label={label} className="min-w-0 flex-1" readOnly value={value} />

<Button
appearance="weak"
aria-label={copied ? t("Copied") : t("Copy to clipboard")}
brand="neutral"
data-testid="login-copy-address"
leftIcon={copied ? "check" : "content_copy"}
onClick={copy}
/>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import React from "react";
import { beforeEach, describe, expect, it, vi } from "vitest";

const mocks = vi.hoisted(() => ({
dispatch: vi.fn(),
login: vi.fn(),
oauthPending: undefined as string | undefined,
}));

vi.mock("react-redux", () => ({
useDispatch: () => mocks.dispatch,
useSelector: (selector: (state: unknown) => unknown) =>
selector({ session: { nexus: { oauthPending: mocks.oauthPending } } }),
}));
vi.mock("@/views/components/Header/premium/useNexusLogin.hook", () => ({
useNexusLogin: () => mocks.login,
}));

import { setOauthPending } from "../../actions/session";
import { LoginDialog, type ILoginDialogProps } from "./LoginDialog";

const URL = "https://users.nexusmods.com/oauth/authorize?x=1";

const renderDialog = (overrides: Partial<ILoginDialogProps> = {}) => {
const props: ILoginDialogProps = {
visible: false,
onHide: vi.fn(),
onCancelLogin: vi.fn(),
onReceiveCode: vi.fn(() => Promise.resolve()),
...overrides,
};
render(<LoginDialog {...props} />);
return props;
};

const token = (value: object) => Buffer.from(JSON.stringify(value)).toString("base64");

describe("LoginDialog", () => {
beforeEach(() => {
mocks.dispatch.mockReset();
mocks.login.mockReset();
mocks.oauthPending = undefined;
});

it("stays closed when nothing asked for it", () => {
renderDialog();

expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
});

it("offers to log in when opened before a login has started", async () => {
renderDialog({ visible: true });

await userEvent.click(screen.getByTestId("login-dialog-login"));

expect(mocks.login).toHaveBeenCalledOnce();
});

it("shows the progress and the address while a login waits on the website", async () => {
mocks.oauthPending = URL;
renderDialog();

expect(await screen.findByText("login_dialog::progress::title")).toBeInTheDocument();
expect(screen.getByTestId("login-spinner")).toBeInTheDocument();
expect(screen.getByRole("textbox", { name: "login_dialog::address" })).toHaveValue(URL);
});

it("cancels the login when closed", async () => {
mocks.oauthPending = URL;
const props = renderDialog();

await userEvent.click(screen.getByRole("button", { name: "Close" }));

expect(mocks.dispatch).toHaveBeenCalledWith(setOauthPending(undefined));
expect(props.onCancelLogin).toHaveBeenCalledOnce();
expect(props.onHide).toHaveBeenCalledOnce();
});

it("cancels the login on Escape", async () => {
mocks.oauthPending = URL;
const props = renderDialog();
await screen.findByText("login_dialog::progress::title");

await userEvent.keyboard("{Escape}");

expect(props.onCancelLogin).toHaveBeenCalledOnce();
});

it("keeps the login going when the user clicks outside the dialog", async () => {
mocks.oauthPending = URL;
const props = renderDialog();
await screen.findByText("login_dialog::progress::title");

await userEvent.click(document.querySelector(".nxm-modal-overlay") as Element);

expect(props.onCancelLogin).not.toHaveBeenCalled();
expect(props.onHide).not.toHaveBeenCalled();
});

it("says when logging in with a readable token fails, rather than blaming the token", async () => {
mocks.oauthPending = URL;
const props = renderDialog({
onReceiveCode: vi.fn(() => Promise.reject(new Error("network down"))),
});

await userEvent.click(screen.getByTestId("login-with-token"));
await userEvent.type(
screen.getByTestId("login-token-input"),
token({ authorization_code: "code", state: "s" }),
);
await userEvent.click(screen.getByTestId("login-token-save"));

expect(await screen.findByText("login_dialog::token::failed")).toBeInTheDocument();
expect(screen.queryByText("login_dialog::token::invalid")).not.toBeInTheDocument();
expect(props.onHide).not.toHaveBeenCalled();
});

it("completes the login with a pasted token", async () => {
mocks.oauthPending = URL;
const props = renderDialog();

await userEvent.click(screen.getByTestId("login-with-token"));
await userEvent.type(
screen.getByTestId("login-token-input"),
token({ authorization_code: "code", state: "s" }),
);
await userEvent.click(screen.getByTestId("login-token-save"));

expect(props.onReceiveCode).toHaveBeenCalledWith("code", "s");
expect(props.onHide).toHaveBeenCalledOnce();
});

it("says when a pasted token isn't recognised", async () => {
mocks.oauthPending = URL;
const props = renderDialog();

await userEvent.click(screen.getByTestId("login-with-token"));
await userEvent.type(screen.getByTestId("login-token-input"), "not-a-token");
await userEvent.click(screen.getByTestId("login-token-save"));

expect(screen.getByText("login_dialog::token::invalid")).toBeInTheDocument();
expect(props.onReceiveCode).not.toHaveBeenCalled();
expect(props.onHide).not.toHaveBeenCalled();
});
});
Loading