Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show logged in user in header in cloud mode #645

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nine-poets-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystatic/core': patch
---

Show logged in user in header in cloud mode
48 changes: 48 additions & 0 deletions packages/keystatic/src/app/shell/data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { isDefined } from 'emery';
import { getAuth } from '../auth';
import { ViewerContext, SidebarFooter_viewer } from './viewer-data';
import { parseRepoConfig, serializeRepoConfig } from '../repo-config';
import { z } from 'zod';

export function fetchLocalTree(sha: string) {
if (treeCache.has(sha)) {
Expand Down Expand Up @@ -101,6 +102,53 @@ export function LocalAppShellProvider(props: {
);
}

const cloudInfoSchema = z.object({
user: z.object({
name: z.string(),
email: z.string(),
avatarUrl: z.string().optional(),
}),
project: z.object({
name: z.string(),
}),
team: z.object({
name: z.string(),
slug: z.string(),
images: z.boolean(),
}),
});

const CloudInfo = createContext<null | z.infer<typeof cloudInfoSchema>>(null);

export function useCloudInfo() {
return useContext(CloudInfo);
}

export function CloudInfoProvider(props: {
children: ReactNode;
config: Config;
}) {
const data = useData(
useCallback(async () => {
if (!props.config.cloud?.project) throw new Error('no cloud project set');
const result = await fetch(`${KEYSTATIC_CLOUD_API_URL}/v1/info`, {
headers: {
...KEYSTATIC_CLOUD_HEADERS,
Authorization: `Bearer ${await getAuth(props.config).then(
auth => auth?.accessToken
)}`,
},
}).then(x => x.json());
return cloudInfoSchema.parse(result);
}, [props.config])
);
return (
<CloudInfo.Provider value={data.kind === 'loaded' ? data.data : null}>
{props.children}
</CloudInfo.Provider>
);
}

export const GitHubAppShellDataContext = createContext<null | UseQueryState<
OperationData<typeof GitHubAppShellQuery | typeof CloudAppShellQuery>,
OperationVariables<typeof GitHubAppShellQuery | typeof CloudAppShellQuery>
Expand Down
43 changes: 36 additions & 7 deletions packages/keystatic/src/app/shell/topbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ import {

import { ZapLogo } from './common';
import { useAppState, useConfig } from './context';
import { BranchInfoContext, GitHubAppShellDataContext } from './data';
import {
BranchInfoContext,
GitHubAppShellDataContext,
useCloudInfo,
} from './data';
import { useViewer } from './viewer-data';
import { useThemeContext } from './theme';
import { serializeRepoConfig } from '../repo-config';
Expand Down Expand Up @@ -87,6 +91,7 @@ export const SidebarHeader = () => {
// -----------------------------------------------------------------------------

function CloudHeader({ config }: { config: CloudConfig }) {
const cloudInfo = useCloudInfo();
return (
<HeaderOuter>
<BrandButton />
Expand All @@ -104,7 +109,17 @@ function CloudHeader({ config }: { config: CloudConfig }) {
<GitMenu />
<Box flex="1" />
<ThemeMenu />
{/* <UserMenu /> */}
<UserMenu
user={
cloudInfo
? {
name: cloudInfo.user.name,
login: cloudInfo.user.email,
avatarUrl: cloudInfo.user.avatarUrl,
}
: undefined
}
/>
</HeaderOuter>
);
}
Expand All @@ -113,6 +128,7 @@ function CloudHeader({ config }: { config: CloudConfig }) {
// -----------------------------------------------------------------------------

function GithubHeader({ config }: { config: GitHubConfig }) {
const user = useViewer();
return (
<HeaderOuter>
<BrandButton />
Expand All @@ -133,7 +149,17 @@ function GithubHeader({ config }: { config: GitHubConfig }) {
<GitMenu />
<Box flex="1" />
<ThemeMenu />
<UserMenu />
<UserMenu
user={
user
? {
login: user.login,
name: user.name ?? user.login,
avatarUrl: user.avatarUrl,
}
: undefined
}
/>
</HeaderOuter>
);
}
Expand Down Expand Up @@ -259,8 +285,11 @@ function ThemeMenu() {
// User controls
// -----------------------------------------------------------------------------

function UserMenu() {
let user = useViewer();
function UserMenu({
user,
}: {
user: { name: string; avatarUrl?: string; login: string } | undefined;
}) {
let config = useConfig();
const menuItems = useMemo(() => {
let items = [
Expand All @@ -273,7 +302,7 @@ function UserMenu() {
if (isCloudConfig(config)) {
items.unshift({
id: 'manage',
label: 'Manage account',
label: 'Account',
icon: userIcon,
});
}
Expand Down Expand Up @@ -315,7 +344,7 @@ function UserMenu() {
/>
<Flex direction="column" gap="small">
<Text size="small" weight="semibold" color="neutralEmphasis">
{user.name ?? user.login}
{user.name}
</Text>
<Text size="small" color="neutralTertiary">
{user.login}
Expand Down
9 changes: 8 additions & 1 deletion packages/keystatic/src/app/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { AppSlugProvider } from './onboarding/install-app';
import { useRouter, Router, RouterProvider } from './router';
import { isCloudConfig, isGitHubConfig, redirectToCloudAuth } from './utils';
import {
CloudInfoProvider,
GitHubAppShellDataContext,
GitHubAppShellDataProvider,
} from './shell/data';
Expand Down Expand Up @@ -107,11 +108,17 @@ function PageInner({ config }: { config: Config }) {
return <KeystaticCloudAuthCallback config={config} />;
}
let wrapper: (element: ReactElement) => ReactElement = x => x;
if (isCloudConfig(config)) {
wrapper = element => (
<CloudInfoProvider config={config}>{element}</CloudInfoProvider>
);
}
if (isGitHubConfig(config) || isCloudConfig(config)) {
const origWrapper = wrapper;
wrapper = element => (
<AuthWrapper config={config}>
<GitHubAppShellDataProvider config={config}>
{element}
{origWrapper(element)}
</GitHubAppShellDataProvider>
</AuthWrapper>
);
Expand Down
Loading