mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-31 04:04:21 -04:00
Fix accounts under ssr
This commit is contained in:
parent
d63c601812
commit
07259a7635
@ -98,6 +98,7 @@ const nextConfig = {
|
|||||||
"react-native-web",
|
"react-native-web",
|
||||||
"react-native-svg",
|
"react-native-svg",
|
||||||
"react-native-reanimated",
|
"react-native-reanimated",
|
||||||
|
"react-native-mmkv",
|
||||||
"moti",
|
"moti",
|
||||||
"yoshiki",
|
"yoshiki",
|
||||||
"@expo/vector-icons",
|
"@expo/vector-icons",
|
||||||
|
@ -109,7 +109,7 @@ const YoshikiDebug = ({ children }: { children: JSX.Element }) => {
|
|||||||
|
|
||||||
const App = ({ Component, pageProps }: AppProps) => {
|
const App = ({ Component, pageProps }: AppProps) => {
|
||||||
const [queryClient] = useState(() => createQueryClient());
|
const [queryClient] = useState(() => createQueryClient());
|
||||||
const { queryState, token, randomItems, ...props } = superjson.deserialize<any>(
|
const { queryState, token, randomItems, account, ...props } = superjson.deserialize<any>(
|
||||||
pageProps ?? { json: {} },
|
pageProps ?? { json: {} },
|
||||||
);
|
);
|
||||||
const layoutInfo = (Component as QueryPage).getLayout ?? (({ page }) => page);
|
const layoutInfo = (Component as QueryPage).getLayout ?? (({ page }) => page);
|
||||||
@ -132,7 +132,7 @@ const App = ({ Component, pageProps }: AppProps) => {
|
|||||||
<meta name="description" content="A portable and vast media library solution." />
|
<meta name="description" content="A portable and vast media library solution." />
|
||||||
</Head>
|
</Head>
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
<AccountProvider>
|
<AccountProvider ssrAccount={account}>
|
||||||
<HydrationBoundary state={queryState}>
|
<HydrationBoundary state={queryState}>
|
||||||
<ThemeSelector theme="auto" font={{ normal: "inherit" }}>
|
<ThemeSelector theme="auto" font={{ normal: "inherit" }}>
|
||||||
<GlobalCssTheme />
|
<GlobalCssTheme />
|
||||||
@ -160,27 +160,32 @@ const App = ({ Component, pageProps }: AppProps) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
App.getInitialProps = async (ctx: AppContext) => {
|
App.getInitialProps = async (ctx: AppContext) => {
|
||||||
const account = readAccountCookie(ctx.ctx.req?.headers.cookie);
|
|
||||||
const appProps = await NextApp.getInitialProps(ctx);
|
const appProps = await NextApp.getInitialProps(ctx);
|
||||||
const Component = ctx.Component as QueryPage;
|
const Component = ctx.Component as QueryPage;
|
||||||
|
|
||||||
|
const items = arrayShuffle(Component.randomItems ?? []);
|
||||||
|
appProps.pageProps.randomItems = {
|
||||||
|
[Component.displayName!]: items,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (typeof window !== "undefined") return { pageProps: superjson.serialize(appProps.pageProps) };
|
||||||
|
|
||||||
|
|
||||||
const getUrl = Component.getFetchUrls;
|
const getUrl = Component.getFetchUrls;
|
||||||
const getLayoutUrl =
|
const getLayoutUrl =
|
||||||
Component.getLayout && "Layout" in Component.getLayout
|
Component.getLayout && "Layout" in Component.getLayout
|
||||||
? Component.getLayout.Layout.getFetchUrls
|
? Component.getLayout.Layout.getFetchUrls
|
||||||
: Component.getLayout?.getFetchUrls;
|
: Component.getLayout?.getFetchUrls;
|
||||||
|
|
||||||
const items = arrayShuffle(Component.randomItems ?? []);
|
|
||||||
appProps.pageProps.randomItems = {
|
|
||||||
[Component.displayName!]: items,
|
|
||||||
};
|
|
||||||
const urls: QueryIdentifier[] = [
|
const urls: QueryIdentifier[] = [
|
||||||
...(getUrl ? getUrl(ctx.router.query as any, items) : []),
|
...(getUrl ? getUrl(ctx.router.query as any, items) : []),
|
||||||
...(getLayoutUrl ? getLayoutUrl(ctx.router.query as any, items) : []),
|
...(getLayoutUrl ? getLayoutUrl(ctx.router.query as any, items) : []),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const account = readAccountCookie(ctx.ctx.req?.headers.cookie);
|
||||||
const [authToken, token] = await getTokenWJ(account ?? null);
|
const [authToken, token] = await getTokenWJ(account ?? null);
|
||||||
appProps.pageProps.queryState = await fetchQuery(urls, authToken);
|
appProps.pageProps.queryState = await fetchQuery(urls, authToken);
|
||||||
appProps.pageProps.token = token;
|
appProps.pageProps.token = token;
|
||||||
|
appProps.pageProps.account = account;
|
||||||
|
|
||||||
return { pageProps: superjson.serialize(appProps.pageProps) };
|
return { pageProps: superjson.serialize(appProps.pageProps) };
|
||||||
};
|
};
|
||||||
|
@ -35,9 +35,9 @@ const writeAccounts = (accounts: Account[]) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const setAccountCookie = (account?: Account) => {
|
export const setAccountCookie = (account?: Account) => {
|
||||||
const value = JSON.stringify(account);
|
let value = JSON.stringify(account);
|
||||||
// Remove illegal values from json. There should not be one in the account anyways.
|
// Remove illegal values from json. There should not be one in the account anyways.
|
||||||
value.replaceAll(";", "");
|
value = value?.replaceAll(";", "");
|
||||||
const d = new Date();
|
const d = new Date();
|
||||||
// A year
|
// A year
|
||||||
d.setTime(d.getTime() + 365 * 24 * 60 * 60 * 1000);
|
d.setTime(d.getTime() + 365 * 24 * 60 * 60 * 1000);
|
||||||
|
@ -53,16 +53,43 @@ export const ConnectionErrorContext = createContext<{
|
|||||||
retry?: () => void;
|
retry?: () => void;
|
||||||
}>({ error: null });
|
}>({ error: null });
|
||||||
|
|
||||||
export const AccountProvider = ({ children }: { children: ReactNode }) => {
|
/* eslint-disable react-hooks/rules-of-hooks */
|
||||||
|
export const AccountProvider = ({
|
||||||
|
children,
|
||||||
|
ssrAccount,
|
||||||
|
}: {
|
||||||
|
children: ReactNode;
|
||||||
|
ssrAccount?: Account;
|
||||||
|
}) => {
|
||||||
|
if (Platform.OS === "web" && typeof window === "undefined") {
|
||||||
|
const accs = ssrAccount
|
||||||
|
? [{ ...ssrAccount, selected: true, select: () => { }, remove: () => { } }]
|
||||||
|
: [];
|
||||||
|
return (
|
||||||
|
<AccountContext.Provider value={accs}>
|
||||||
|
<ConnectionErrorContext.Provider
|
||||||
|
value={{
|
||||||
|
error: null,
|
||||||
|
retry: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["auth", "me"] });
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</ConnectionErrorContext.Provider>
|
||||||
|
</AccountContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const [accStr] = useMMKVString("accounts");
|
const [accStr] = useMMKVString("accounts");
|
||||||
const acc = z.array(AccountP).parse(accStr);
|
const acc = accStr ? z.array(AccountP).parse(accStr) : null;
|
||||||
const accounts = useMemo(
|
const accounts = useMemo(
|
||||||
() =>
|
() =>
|
||||||
acc.map((account) => ({
|
acc?.map((account) => ({
|
||||||
...account,
|
...account,
|
||||||
select: () => updateAccount(account.id, { ...account, selected: true }),
|
select: () => updateAccount(account.id, { ...account, selected: true }),
|
||||||
remove: () => removeAccounts((x) => x.id == x.id),
|
remove: () => removeAccounts((x) => x.id == x.id),
|
||||||
})),
|
})) ?? [],
|
||||||
[acc],
|
[acc],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ export const login = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const getTokenWJ = async (account?: Account | null): Promise<[string, Token] | [null, null]> => {
|
export const getTokenWJ = async (account?: Account | null): Promise<[string, Token] | [null, null]> => {
|
||||||
if (account === null)
|
if (account === undefined)
|
||||||
account = getCurrentAccount();
|
account = getCurrentAccount();
|
||||||
if (!account) return [null, null];
|
if (!account) return [null, null];
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user