mirror of https://github.com/usememos/memos
chore: tweak route layout
parent
db56e1b575
commit
dfe29ec766
@ -0,0 +1,32 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { initialGlobalState } from "@/store/module";
|
||||||
|
import { useUserStore, useWorkspaceSettingStore } from "@/store/v1";
|
||||||
|
import { WorkspaceSettingKey } from "@/types/proto/store/workspace_setting";
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
children: React.ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
const CommonContextProvider = (props: Props) => {
|
||||||
|
const workspaceSettingStore = useWorkspaceSettingStore();
|
||||||
|
const userStore = useUserStore();
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const initialState = async () => {
|
||||||
|
await initialGlobalState();
|
||||||
|
await workspaceSettingStore.fetchWorkspaceSetting(WorkspaceSettingKey.WORKSPACE_SETTING_GENERAL);
|
||||||
|
try {
|
||||||
|
await userStore.fetchCurrentUser();
|
||||||
|
} catch (error) {
|
||||||
|
// Do nothing.
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Promise.all([initialState()]).then(() => setLoading(false));
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return loading ? null : <>{props.children}</>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CommonContextProvider;
|
@ -1,27 +0,0 @@
|
|||||||
import { useEffect } from "react";
|
|
||||||
import useCurrentUser from "@/hooks/useCurrentUser";
|
|
||||||
import useNavigateTo from "@/hooks/useNavigateTo";
|
|
||||||
|
|
||||||
interface Props {
|
|
||||||
children: React.ReactNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
const AuthStatusProvider = (props: Props) => {
|
|
||||||
const navigateTo = useNavigateTo();
|
|
||||||
const currentUser = useCurrentUser();
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!currentUser) {
|
|
||||||
// If not logged in, redirect to explore page by default.
|
|
||||||
navigateTo("/explore");
|
|
||||||
}
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
if (!currentUser) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return <>{props.children}</>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default AuthStatusProvider;
|
|
Loading…
Reference in New Issue