mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-08 10:44:20 -04:00
Fix web build
This commit is contained in:
parent
32fa639d4a
commit
96d5ca0f3c
85
front/packages/models/src/accounts.tsx
Normal file
85
front/packages/models/src/accounts.tsx
Normal file
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Kyoo - A portable and vast media library solution.
|
||||
* Copyright (c) Kyoo.
|
||||
*
|
||||
* See AUTHORS.md and LICENSE file in the project root for full license information.
|
||||
*
|
||||
* Kyoo is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* Kyoo is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { getSecureItem, setSecureItem, storage } from "./secure-store";
|
||||
import { setApiUrl } from "./query";
|
||||
import { createContext, useEffect, useState } from "react";
|
||||
import { useMMKVListener } from "react-native-mmkv";
|
||||
import { Account, loginFunc } from "./login";
|
||||
|
||||
export const AccountContext = createContext<ReturnType<typeof useAccounts>>({ type: "loading" });
|
||||
|
||||
export const useAccounts = () => {
|
||||
const [accounts, setAccounts] = useState<Account[]>(JSON.parse(getSecureItem("accounts") ?? "[]"));
|
||||
const [verified, setVerified] = useState<{
|
||||
status: "ok" | "error" | "loading" | "unverified";
|
||||
error?: string;
|
||||
}>({ status: "loading" });
|
||||
const [retryCount, setRetryCount] = useState(0);
|
||||
|
||||
const sel = getSecureItem("selected");
|
||||
let [selected, setSelected] = useState<number | null>(
|
||||
sel ? parseInt(sel) : accounts.length > 0 ? 0 : null,
|
||||
);
|
||||
if (selected === null && accounts.length > 0) selected = 0;
|
||||
if (accounts.length === 0) selected = null;
|
||||
|
||||
useEffect(() => {
|
||||
async function check() {
|
||||
setVerified({status: "loading"});
|
||||
const selAcc = accounts![selected!];
|
||||
setApiUrl(selAcc.apiUrl);
|
||||
const verif = await loginFunc("refresh", selAcc.refresh_token);
|
||||
setVerified(verif.ok ? { status: "ok" } : { status: "error", error: verif.error });
|
||||
}
|
||||
|
||||
if (accounts.length && selected !== null) check();
|
||||
else setVerified({ status: "unverified" });
|
||||
// Use the length of the array and not the array directly because we don't care if the refresh token changes.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [accounts.length, selected, retryCount]);
|
||||
|
||||
useMMKVListener((key) => {
|
||||
if (key === "accounts") setAccounts(JSON.parse(getSecureItem("accounts") ?? "[]"));
|
||||
}, storage);
|
||||
|
||||
if (verified.status === "loading") return { type: "loading" } as const;
|
||||
if (accounts.length && verified.status === "unverified") return { type: "loading" } as const;
|
||||
if (verified.status === "error") {
|
||||
return {
|
||||
type: "error",
|
||||
error: verified.error,
|
||||
retry: () => {
|
||||
setVerified({ status: "loading" });
|
||||
setRetryCount((x) => x + 1);
|
||||
},
|
||||
} as const;
|
||||
}
|
||||
return {
|
||||
type: "ok",
|
||||
accounts,
|
||||
selected,
|
||||
setSelected: (selected: number) => {
|
||||
setSelected(selected);
|
||||
setSecureItem("selected", selected.toString());
|
||||
},
|
||||
} as const;
|
||||
};
|
||||
|
23
front/packages/models/src/accounts.web.ts
Normal file
23
front/packages/models/src/accounts.web.ts
Normal file
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Kyoo - A portable and vast media library solution.
|
||||
* Copyright (c) Kyoo.
|
||||
*
|
||||
* See AUTHORS.md and LICENSE file in the project root for full license information.
|
||||
*
|
||||
* Kyoo is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* Kyoo is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export const useAccounts = () => {}
|
||||
|
||||
export const AccountContext = 0;
|
@ -18,6 +18,7 @@
|
||||
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export * from "./accounts";
|
||||
export * from "./resources";
|
||||
export * from "./traits";
|
||||
export * from "./page";
|
||||
|
@ -19,13 +19,11 @@
|
||||
*/
|
||||
|
||||
import { z } from "zod";
|
||||
import { deleteSecureItem, getSecureItem, setSecureItem, storage } from "./secure-store";
|
||||
import { deleteSecureItem, getSecureItem, setSecureItem } from "./secure-store";
|
||||
import { zdate } from "./utils";
|
||||
import { queryFn, setApiUrl } from "./query";
|
||||
import { queryFn } from "./query";
|
||||
import { KyooErrors } from "./kyoo-errors";
|
||||
import { Platform } from "react-native";
|
||||
import { createContext, useEffect, useState } from "react";
|
||||
import { useMMKVListener } from "react-native-mmkv";
|
||||
|
||||
const TokenP = z.object({
|
||||
token_type: z.literal("Bearer"),
|
||||
@ -42,65 +40,6 @@ type Result<A, B> =
|
||||
|
||||
export type Account = Token & { apiUrl: string; username: string };
|
||||
|
||||
export const AccountContext = createContext<ReturnType<typeof useAccounts>>({ type: "loading" });
|
||||
|
||||
export const useAccounts = () => {
|
||||
const [accounts, setAccounts] = useState<Account[]>(JSON.parse(getSecureItem("accounts") ?? "[]"));
|
||||
const [verified, setVerified] = useState<{
|
||||
status: "ok" | "error" | "loading" | "unverified";
|
||||
error?: string;
|
||||
}>({ status: "loading" });
|
||||
const [retryCount, setRetryCount] = useState(0);
|
||||
|
||||
const sel = getSecureItem("selected");
|
||||
let [selected, setSelected] = useState<number | null>(
|
||||
sel ? parseInt(sel) : accounts.length > 0 ? 0 : null,
|
||||
);
|
||||
if (selected === null && accounts.length > 0) selected = 0;
|
||||
if (accounts.length === 0) selected = null;
|
||||
|
||||
useEffect(() => {
|
||||
async function check() {
|
||||
setVerified({status: "loading"});
|
||||
const selAcc = accounts![selected!];
|
||||
setApiUrl(selAcc.apiUrl);
|
||||
const verif = await loginFunc("refresh", selAcc.refresh_token);
|
||||
setVerified(verif.ok ? { status: "ok" } : { status: "error", error: verif.error });
|
||||
}
|
||||
|
||||
if (accounts.length && selected !== null) check();
|
||||
else setVerified({ status: "unverified" });
|
||||
// Use the length of the array and not the array directly because we don't care if the refresh token changes.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [accounts.length, selected, retryCount]);
|
||||
|
||||
useMMKVListener((key) => {
|
||||
if (key === "accounts") setAccounts(JSON.parse(getSecureItem("accounts") ?? "[]"));
|
||||
}, storage);
|
||||
|
||||
if (verified.status === "loading") return { type: "loading" } as const;
|
||||
if (accounts.length && verified.status === "unverified") return { type: "loading" } as const;
|
||||
if (verified.status === "error") {
|
||||
return {
|
||||
type: "error",
|
||||
error: verified.error,
|
||||
retry: () => {
|
||||
setVerified({ status: "loading" });
|
||||
setRetryCount((x) => x + 1);
|
||||
},
|
||||
} as const;
|
||||
}
|
||||
return {
|
||||
type: "ok",
|
||||
accounts,
|
||||
selected,
|
||||
setSelected: (selected: number) => {
|
||||
setSelected(selected);
|
||||
setSecureItem("selected", selected.toString());
|
||||
},
|
||||
} as const;
|
||||
};
|
||||
|
||||
const addAccount = (token: Token, apiUrl: string, username: string | null) => {
|
||||
const accounts: Account[] = JSON.parse(getSecureItem("accounts") ?? "[]");
|
||||
if (accounts.find((x) => x.username === username && x.apiUrl === apiUrl)) return;
|
||||
|
Loading…
x
Reference in New Issue
Block a user