mirror of
https://github.com/Zagrios/bs-manager.git
synced 2026-07-03 14:08:25 +02:00
[chore] reimplement login with meta
This commit is contained in:
+42
-20
@@ -1,25 +1,52 @@
|
||||
import { ModalComponent, ModalExitCode } from "../../../../services/modale.service";
|
||||
import { useState } from "react";
|
||||
import { ModalComponent, ModalExitCode, ModalService } from "../../../../services/modale.service";
|
||||
import { MouseEventHandler, useState } from "react";
|
||||
import { BsmButton } from "renderer/components/shared/bsm-button.component";
|
||||
import { useTranslation } from "renderer/hooks/use-translation.hook";
|
||||
import { useTranslationV2 } from "renderer/hooks/use-translation.hook";
|
||||
import { MetaIcon } from "renderer/components/svgs/icons/meta-icon.component";
|
||||
import { BsmCheckbox } from "renderer/components/shared/bsm-checkbox.component";
|
||||
import Tippy from "@tippyjs/react";
|
||||
import { useService } from "renderer/hooks/use-service.hook";
|
||||
import { IpcService } from "renderer/services/ipc.service";
|
||||
import { lastValueFrom } from "rxjs";
|
||||
import { EnterMetaTokenModal } from "./enter-meta-token-modal.component";
|
||||
import { CustomError } from "shared/models/exceptions/custom-error.class";
|
||||
import { MetaAuthErrorCodes } from "shared/models/bs-version-download/oculus-download.model";
|
||||
import { NotificationService } from "renderer/services/notification.service";
|
||||
|
||||
type ReturnType = { method: MetaAuthMethod.META, stay: boolean } | { method: MetaAuthMethod.MANUAL };
|
||||
export const LoginToMetaModal: ModalComponent<string> = ({ resolver }) => {
|
||||
|
||||
export const LoginToMetaModal: ModalComponent<ReturnType> = ({ resolver }) => {
|
||||
const { text: t } = useTranslationV2();
|
||||
|
||||
const t = useTranslation();
|
||||
const ipc = useService(IpcService);
|
||||
const modal = useService(ModalService);
|
||||
const notifications = useService(NotificationService);
|
||||
|
||||
const [stay, setStay] = useState(false);
|
||||
const [stay, setStay] = useState(true);
|
||||
const [authWindowOpen, setAuthWindowOpen] = useState(false);
|
||||
|
||||
const submit = () => {
|
||||
resolver({ exitCode: ModalExitCode.COMPLETED, data: { method: MetaAuthMethod.META, stay } });
|
||||
};
|
||||
const handleLoginWithMeta = () => {
|
||||
setAuthWindowOpen(() => true);
|
||||
lastValueFrom(ipc.sendV2("login-with-meta", stay)).then(token => {
|
||||
resolver({ exitCode: ModalExitCode.COMPLETED, data: token });
|
||||
}).catch((e: CustomError) => {
|
||||
if(e.code === MetaAuthErrorCodes.META_LOGIN_WINDOW_CLOSED_BY_USER){
|
||||
return;
|
||||
}
|
||||
|
||||
const enterTokenManually = () => {
|
||||
resolver({ exitCode: ModalExitCode.COMPLETED, data: { method: MetaAuthMethod.MANUAL } });
|
||||
if(e.code){
|
||||
notifications.notifyError({title: "notifications.types.error", desc: `notifications.bs-download.oculus-download.errors.msg.${e.code}`});
|
||||
}
|
||||
|
||||
resolver({ exitCode: ModalExitCode.CANCELED });
|
||||
}).finally(() => {
|
||||
setAuthWindowOpen(() => false);
|
||||
})
|
||||
}
|
||||
|
||||
const handleEnterTokenManually: MouseEventHandler<HTMLButtonElement> = async (e) => {
|
||||
e.preventDefault();
|
||||
const res = await modal.openModal(EnterMetaTokenModal);
|
||||
resolver(res);
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -38,17 +65,12 @@ export const LoginToMetaModal: ModalComponent<ReturnType> = ({ resolver }) => {
|
||||
<span>{t("modals.connect-to-meta.stay")}</span>
|
||||
</div>
|
||||
|
||||
<BsmButton className="rounded-md flex justify-center items-center transition-all h-10 w-full" typeColor="primary" text="modals.connect-to-meta.connect-to-meta" withBar={false} onClick={submit}/>
|
||||
<BsmButton className="rounded-md flex justify-center items-center transition-all h-10 w-full" typeColor="primary" text="modals.connect-to-meta.connect-to-meta" withBar={false} onClick={handleLoginWithMeta} disabled={authWindowOpen}/>
|
||||
|
||||
<Tippy className="!bg-neutral-900" arrow={false} content={t("modals.connect-to-meta.body.enter-token-manually-tooltip")} >
|
||||
<p className="text-sm italic underline text-center -translate-y-1 leading-3 cursor-pointer" onClick={enterTokenManually}>{t("modals.connect-to-meta.body.enter-token-manually")}</p>
|
||||
<button className="text-sm italic underline text-center -translate-y-1 leading-3 cursor-pointer" onClick={handleEnterTokenManually}>{t("modals.connect-to-meta.body.enter-token-manually")}</button>
|
||||
</Tippy>
|
||||
|
||||
|
||||
</form>
|
||||
);
|
||||
};
|
||||
|
||||
export enum MetaAuthMethod {
|
||||
MANUAL = "manual",
|
||||
META = "meta"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user