Skip to content

Commit

Permalink
Merge pull request #369 from mongodb/brooke/refac
Browse files Browse the repository at this point in the history
Refactor session logic into its own hook
  • Loading branch information
bruugey authored Jun 6, 2024
2 parents e98609d + 7bf9f97 commit 9c654dc
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 93 deletions.
12 changes: 10 additions & 2 deletions src/app/component/[component]/design-docs/client.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
'use client';

import { css } from '@emotion/css';
import { ContentstackRichText } from '@/components/content-stack';
import { CSNode } from '@/components/content-stack/types';
import { ErrorBoundary } from 'next/dist/client/components/error-boundary';

interface DesignDocsContentProps {
content?: CSNode;
}

export const DesignDocsContent = ({ content }: DesignDocsContentProps) => {
return <ContentstackRichText content={content} />;
return (
<div
className={css`
max-width: 700px;
`}
>
<ContentstackRichText content={content} />
</div>
);
};
8 changes: 1 addition & 7 deletions src/app/component/[component]/design-docs/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { css } from '@emotion/css';

import { fetchComponent } from '@/utils/ContentStack/getContentstackResources';
import { DesignDocsContent } from './client';

Expand All @@ -13,11 +11,7 @@ export default async function Page({
});

return (
<div
className={css`
max-width: 700px; // TODO: Make this responsive
`}
>
<div>
<DesignDocsContent content={component?.designguidelines} />
</div>
);
Expand Down
64 changes: 33 additions & 31 deletions src/app/component/[component]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
"use client";
'use client';

import { css } from "@emotion/css";
import { useRouter, usePathname } from "next/navigation";
import React from "react";
import { css } from '@emotion/css';
import { useRouter, usePathname } from 'next/navigation';
import React from 'react';

import IconButton from "@leafygreen-ui/icon-button";
import { CodeSandbox, Figma, Github } from "@/components/glyphs";
import { useDarkMode } from "@leafygreen-ui/leafygreen-provider";
import { Tabs, Tab } from "@leafygreen-ui/tabs";
import { color, spacing } from "@leafygreen-ui/tokens";
import { H2 } from "@leafygreen-ui/typography";
import IconButton from '@leafygreen-ui/icon-button';
import { CodeSandbox, Figma, Github } from '@/components/glyphs';
import { useDarkMode } from '@leafygreen-ui/leafygreen-provider';
import { Tabs, Tab } from '@leafygreen-ui/tabs';
import { color, spacing } from '@leafygreen-ui/tokens';
import { H2 } from '@leafygreen-ui/typography';

import useComponentFields from "@/hooks/useComponentFields";
import { getGithubLink } from "@/utils";
import { useComponentFields } from '@/hooks';
import { getGithubLink } from '@/utils';

const liveExamplePath = "live-example";
const designDocsPath = "design-docs";
const codeDocsPath = "code-docs";
const liveExamplePath = 'live-example';
const designDocsPath = 'design-docs';
const codeDocsPath = 'code-docs';

export default function ComponentLayout({
children,
Expand All @@ -25,13 +25,13 @@ export default function ComponentLayout({
}) {
const router = useRouter();
const pathname = usePathname();
const currentComponent = pathname.split("/")[2];
const currentComponent = pathname.split('/')[2];
const { theme } = useDarkMode();

const component = useComponentFields({ componentName: currentComponent });

const getSelected = () => {
const suffix = pathname.split("/")[3];
const suffix = pathname.split('/')[3];
if (suffix === liveExamplePath) {
return 0;
}
Expand Down Expand Up @@ -61,7 +61,7 @@ export default function ComponentLayout({
href: component?.codesandbox_url?.href,
icon: <CodeSandbox />,
},
]
];

return (
<div
Expand All @@ -75,7 +75,7 @@ export default function ComponentLayout({
margin-bottom: ${spacing[600]}px;
`}
>
{currentComponent.split("-").join(" ")}
{currentComponent.split('-').join(' ')}
</H2>
<Tabs
selected={getSelected()}
Expand All @@ -94,18 +94,20 @@ export default function ComponentLayout({
height: 100%;
`}
>
{externalLinks.map(({ 'aria-label': ariaLabel, href, icon }, index) => (
<IconButton
key={ariaLabel + index}
aria-label={ariaLabel}
size="large"
href={href}
target="_blank"
rel="noopener noreferrer"
>
{icon}
</IconButton>
))}
{externalLinks.map(
({ 'aria-label': ariaLabel, href, icon }, index) => (
<IconButton
key={ariaLabel + index}
aria-label={ariaLabel}
size="large"
href={href}
target="_blank"
rel="noopener noreferrer"
>
{icon}
</IconButton>
),
)}
</div>
}
>
Expand Down
2 changes: 1 addition & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { css } from '@emotion/css';
import { RootStyleRegistry } from '@/components/global';
import { useMediaQuery } from '@/hooks/useMediaQuery';
import { useMediaQuery } from '@/hooks';
import LeafyGreenProvider, {
useDarkMode,
} from '@leafygreen-ui/leafygreen-provider';
Expand Down
31 changes: 12 additions & 19 deletions src/app/private/page.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
"use client";
'use client';

import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import Button from "@leafygreen-ui/button";
import { BasicEmptyState } from "@leafygreen-ui/empty-state";
import { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
import Button from '@leafygreen-ui/button';
import { BasicEmptyState } from '@leafygreen-ui/empty-state';
// @ts-expect-error
import ArrowLeftIcon from "@leafygreen-ui/icon/dist/ArrowLeft";
import ArrowLeftIcon from '@leafygreen-ui/icon/dist/ArrowLeft';
// @ts-expect-error
import LogInIcon from "@leafygreen-ui/icon/dist/LogIn";
import { getSession, login, Session } from "@/auth";
import { ComingSoon, Security } from "@/components/glyphs";
import LogInIcon from '@leafygreen-ui/icon/dist/LogIn';
import { getSession, login, Session } from '@/auth';
import { ComingSoon, Security } from '@/components/glyphs';
import { useSession } from '@/hooks';

export default function Private() {
const router = useRouter();
const [session, setSession] = useState<Session | undefined>();

useEffect(() => {
getSession().then((response) => {
if (response !== null) {
setSession(response);
}
});
}, []);
const session = useSession();

return session?.user ? (
<BasicEmptyState
title="Coming Soon"
description="Check back for updates soon"
primaryButton={
<Button leftGlyph={<ArrowLeftIcon />} onClick={() => router.push("/")}>
<Button leftGlyph={<ArrowLeftIcon />} onClick={() => router.push('/')}>
Return to home
</Button>
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
SideNavigation,
Search,
} from '@/components/global';
import { useMediaQuery } from '@/hooks/useMediaQuery';
import { useMediaQuery } from '@/hooks';
import { SIDE_NAV_WIDTH } from '@/constants';
import { ContentStackContextProvider } from '@/contexts/ContentStackContext';
import { ComponentFields, ContentPageGroup } from '@/utils/ContentStack/types';
Expand Down
12 changes: 2 additions & 10 deletions src/components/global/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import debounce from 'lodash/debounce';
import LockIcon from '@leafygreen-ui/icon/dist/Lock';
import { SearchInput, SearchResult } from '@leafygreen-ui/search-input';
import { spacing } from '@leafygreen-ui/tokens';
import { getSession, Session } from '@/auth';
import { useSession } from '@/hooks';
import { components } from '@/utils/components';

const fuseOptions = {
Expand All @@ -22,7 +22,7 @@ const useFuseSearch = (data: any[], options: IFuseOptions<any>) => {
};

export function Search() {
const [session, setSession] = useState<Session | undefined>();
const session = useSession();
const [searchTerm, setSearchTerm] = useState('');
const [results, setResults] = useState(components);

Expand All @@ -48,14 +48,6 @@ export function Search() {
}
}, [searchTerm]);

useEffect(() => {
getSession().then(response => {
if (response !== null) {
setSession(response);
}
});
}, []);

return (
<SearchInput
aria-label="Search Components"
Expand Down
2 changes: 1 addition & 1 deletion src/components/global/SideNavigation/SideNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { MongoDBLogo, SupportedColors } from '@leafygreen-ui/logo';
import { color, spacing } from '@leafygreen-ui/tokens';
import { SIDE_NAV_WIDTH } from '@/constants';
import { useMediaQuery } from '@/hooks/useMediaQuery';
import { useMediaQuery } from '@/hooks';
import { ComponentMeta, Group, groupedComponents } from '@/utils/components';
import { Search } from '../Search';
import { Drawer } from './Drawer';
Expand Down
30 changes: 11 additions & 19 deletions src/components/global/UserMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
"use client";
'use client';

import { useEffect, useState } from "react";
import { css } from "@emotion/css";
import Button from "@leafygreen-ui/button";
import { css } from '@emotion/css';
import Button from '@leafygreen-ui/button';
// @ts-expect-error
import CaretDownIcon from "@leafygreen-ui/icon/dist/CaretDown";
import CaretDownIcon from '@leafygreen-ui/icon/dist/CaretDown';
// @ts-expect-error
import LogOutIcon from "@leafygreen-ui/icon/dist/LogOut";
import { Menu, MenuItem } from "@leafygreen-ui/menu";
import { Body, Description } from "@leafygreen-ui/typography";
import { getSession, logout, Session } from "@/auth";
import { LogIn } from "./LogIn";
import LogOutIcon from '@leafygreen-ui/icon/dist/LogOut';
import { Menu, MenuItem } from '@leafygreen-ui/menu';
import { Body, Description } from '@leafygreen-ui/typography';
import { logout } from '@/auth';
import { useSession } from '@/hooks';
import { LogIn } from './LogIn';

export function UserMenu() {
const [session, setSession] = useState<Session | undefined>();

useEffect(() => {
getSession().then((response) => {
if (response !== null) {
setSession(response);
}
});
}, []);
const session = useSession();

return session?.user ? (
<div
Expand Down
2 changes: 1 addition & 1 deletion src/components/home/ComponentCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
Notifications,
Patterns,
} from '@/components/glyphs';
import { useMediaQuery } from '@/hooks/useMediaQuery';
import { useMediaQuery } from '@/hooks';

const liveExamplePath = 'live-example';

Expand Down
4 changes: 4 additions & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { useCallbackRef } from './useCallbackRef';
export { useComponentFields } from './useComponentFields';
export { useMediaQuery } from './useMediaQuery';
export { useSession } from './useSession';
2 changes: 1 addition & 1 deletion src/hooks/useComponentFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fetchComponent } from '@/utils/ContentStack/getContentstackResources';
import { ComponentFields } from '@/utils/ContentStack/types';

/** @deprecated */
export default function useComponentFields({
export function useComponentFields({
componentName,
includeContent = false,
}: {
Expand Down
18 changes: 18 additions & 0 deletions src/hooks/useSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use client';

import { useEffect, useState } from 'react';
import { getSession, Session } from '@/auth';

export const useSession = () => {
const [session, setSession] = useState<Session | undefined>();

useEffect(() => {
getSession().then(response => {
if (response !== null) {
setSession(response);
}
});
}, []);

return session;
};

0 comments on commit 9c654dc

Please sign in to comment.