gear-orders/web/vite/src/UserContext.tsx

34 lines
844 B
TypeScript

import React from "react";
export interface UserContextData {
username?: string;
telegram_photo_url?: string;
mastodon_server?: string;
mastodon_username?: string;
mastodon_attn_list?: string;
mastodon_post_public?: boolean;
}
const UserContext = React.createContext<UserContextData>({});
export const useUserContext = (): UserContextData => {
return React.useContext(UserContext);
};
export const UserContextProvider: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const [userProfile, setUserProfile] = React.useState<UserContextData>({});
React.useEffect(() => {
fetch(`/api/me`)
.then((response) => response.json())
.then((data) => {
setUserProfile(data);
});
}, []);
return (
<UserContext.Provider value={userProfile}>{children}</UserContext.Provider>
);
};