/*
* 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 .
*/
import { Account, loginFunc } from "@kyoo/models";
import { getSecureItem, setSecureItem } from "@kyoo/models/src/secure-store";
import { Button, CircularProgress, H1, P } from "@kyoo/primitives";
import { Redirect } from "expo-router";
import { createContext, useContext, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { View } from "react-native";
import { useYoshiki } from "yoshiki/native";
import { useRouter } from "solito/router";
export const useAccounts = () => {
const [accounts, setAccounts] = useState(null);
const [verified, setVerified] = useState<{
status: "ok" | "error" | "loading" | "unverified";
error?: string;
}>({ status: "loading" });
// TODO: Remember the last selected account.
const selected = accounts?.length ? 0 : null;
useEffect(() => {
async function run() {
const accounts = await getSecureItem("accounts");
setAccounts(accounts ? JSON.parse(accounts) : []);
}
run();
}, []);
useEffect(() => {
async function check() {
const selAcc = accounts![selected!];
await setSecureItem("apiUrl", selAcc.apiUrl);
const verif = await loginFunc("refresh", selAcc.refresh_token);
setVerified(verif.ok ? { status: "ok" } : { status: "error", error: verif.error });
}
if (accounts && selected !== null) check();
else setVerified({status: "unverified"});
}, [accounts, selected, verified.status]);
if (accounts === null || verified.status === "loading") return { type: "loading" } as const;
if (verified.status === "error") {
return {
type: "error",
error: verified.error,
retry: () => setVerified({ status: "loading" }),
};
}
return { type: "ok", accounts, selected } as const;
};
export const ConnectionError = ({ error, retry }: { error?: string; retry: () => void }) => {
const { css } = useYoshiki();
const { t } = useTranslation();
const router = useRouter();
return (
theme.colors.red })}>
{t("error.connection")}
{error ?? t("error.unknown")}
{t("error.connection-tips")}
);
};
export const AccountContext = createContext>({ type: "loading" });
let initialRender = true;
const App = () => {
// Using context on the initial one to keep the splashscreen and not show a spinner.
// eslint-disable-next-line react-hooks/rules-of-hooks
const info = initialRender ? useContext(AccountContext) : useAccounts();
initialRender = false;
if (info.type === "loading") return
if (info.type === "error") return ;
if (info.selected === null) return ;
// While there is no home page, show the browse page.
return ;
};
export default App;