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

QOL session improvements #393

Merged
merged 1 commit into from
Aug 6, 2024
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
8 changes: 4 additions & 4 deletions src/auth/logout.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use server";
'use server';

import { signOut } from "@/auth/auth";
import { signOut } from '@/auth/auth';

export async function logout() {
"use server";
await signOut({ redirectTo: "/" });
'use server';
await signOut({ redirect: true, redirectTo: '/' });
}
6 changes: 5 additions & 1 deletion src/components/global/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Fuse, { IFuseOptions } from 'fuse.js';
import debounce from 'lodash/debounce';
// @ts-expect-error
import LockIcon from '@leafygreen-ui/icon/dist/Lock';
// @ts-expect-error
import UnlockIcon from '@leafygreen-ui/icon/dist/Unlock';
import { SearchInput, SearchResult } from '@leafygreen-ui/search-input';
import { useSession } from '@/hooks';
import { components } from '@/utils/components';
Expand Down Expand Up @@ -52,6 +54,8 @@ export function Search() {
}
}, [searchTerm]);

const PrivateIcon = session?.user ? UnlockIcon : LockIcon;

return (
<SearchInput
aria-label="Search Components"
Expand All @@ -73,7 +77,7 @@ export function Search() {
>
<div className={searchResultStyle}>
{item.name}
{item.isPrivate && !session?.user && <LockIcon size="small" />}
{item.isPrivate && <PrivateIcon size="small" />}
</div>
</SearchResult>
))}
Expand Down
9 changes: 7 additions & 2 deletions src/components/global/SideNavigation/SideNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import AppsIcon from '@leafygreen-ui/icon/dist/Apps';
import LockIcon from '@leafygreen-ui/icon/dist/Lock';
// @ts-expect-error
import MenuIcon from '@leafygreen-ui/icon/dist/Menu';
// @ts-expect-error
import UnlockIcon from '@leafygreen-ui/icon/dist/Unlock';
import IconButton from '@leafygreen-ui/icon-button';
import {
PortalContextProvider,
Expand All @@ -22,7 +24,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';
import { useMediaQuery, useSession } from '@/hooks';
import { ComponentMeta, Group, groupedComponents } from '@/utils/components';
import { Search } from '../Search/Search';
import { Drawer } from './Drawer';
Expand All @@ -31,6 +33,7 @@ import { SideNavLabel } from './SideNavLabel';
import { SideNavList } from './SideNavList';

export function SideNavigation() {
const session = useSession();
const navRef = useRef<HTMLElement>(null);
const [open, setOpen] = React.useState(false);
const [isMobile] = useMediaQuery(['(max-width: 640px)'], {
Expand All @@ -42,6 +45,8 @@ export function SideNavigation() {
topLevelPage === 'component' ? activeSubDirOrPage : '';
const { darkMode, theme } = useDarkMode();

const PrivateIcon = session?.user ? UnlockIcon : LockIcon;

const navContent = (
<>
<SideNavLabel
Expand Down Expand Up @@ -176,7 +181,7 @@ export function SideNavigation() {
>
{component.name}
{component.isPrivate && (
<LockIcon
<PrivateIcon
className={css`
margin-left: ${spacing[400]}px;
`}
Expand Down
35 changes: 29 additions & 6 deletions src/components/global/UserMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';

import { useEffect, useState } from 'react';
import { css } from '@emotion/css';
import { Session } from 'next-auth';
import Button from '@leafygreen-ui/button';
// @ts-expect-error
import CaretDownIcon from '@leafygreen-ui/icon/dist/CaretDown';
Expand All @@ -14,8 +15,24 @@ import { LogIn } from './LogIn';

export function UserMenu() {
const session = useSession();
// TODO: use next-auth session when available
// Session does not clear reliably without forcing a state change or a hard refresh
// https://github.com/nextauthjs/next-auth/discussions/4687
Comment on lines +19 to +20
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eww

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know im sorry

const [manualSession, setManualSession] = useState<Session | undefined>(
undefined,
);

useEffect(() => {
if (session?.user) {
setManualSession(session);
}

if (!session?.user) {
setManualSession(undefined);
}
}, [session]);

return session?.user ? (
return manualSession?.user ? (
<div
className={css`
z-index: 1;
Expand All @@ -30,15 +47,21 @@ export function UserMenu() {
`}
rightGlyph={<CaretDownIcon />}
>
{session.user.name}
{manualSession.user.name}
</Button>
}
>
<MenuItem>
<Body darkMode>{session.user.name}</Body>
<Description darkMode>{session.user.email}</Description>
<Body darkMode>{manualSession.user.name}</Body>
<Description darkMode>{manualSession.user.email}</Description>
</MenuItem>
<MenuItem glyph={<LogOutIcon />} onClick={() => logout()}>
<MenuItem
glyph={<LogOutIcon />}
onClick={() => {
logout();
setManualSession(undefined);
}}
>
Log out
</MenuItem>
</Menu>
Expand Down
Loading