Skip to content

Commit

Permalink
fix: Toolbar better auth issue (#21096)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite authored Mar 22, 2024
1 parent 3a995a5 commit d5cc9e4
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 112 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 0 additions & 4 deletions frontend/src/toolbar/bar/Toolbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,4 @@
transform: var(--toolbar-translate) scale(0);
}
}

&--unauthenticated {
width: calc(5rem + 1px); // Account for border
}
}
39 changes: 29 additions & 10 deletions frontend/src/toolbar/bar/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,18 @@ function MoreMenu(): JSX.Element {
}
maxContentWidth={true}
>
<ToolbarButton icon={<IconMenu />} title="More options" />
<ToolbarButton title="More options">
<IconMenu />
</ToolbarButton>
</LemonMenu>
)
}

export function ToolbarInfoMenu(): JSX.Element {
export function ToolbarInfoMenu(): JSX.Element | null {
const ref = useRef<HTMLDivElement | null>(null)
const { visibleMenu, isDragging, menuProperties, minimized, isBlurred } = useValues(toolbarLogic)
const { setMenu } = useActions(toolbarLogic)
const { isAuthenticated } = useValues(toolbarConfigLogic)

const content = minimized ? null : visibleMenu === 'flags' ? (
<FlagsToolbarMenu />
Expand All @@ -102,6 +105,10 @@ export function ToolbarInfoMenu(): JSX.Element {
return () => setMenu(null)
}, [ref.current])

if (!isAuthenticated) {
return null
}

return (
<div
className={clsx(
Expand Down Expand Up @@ -163,7 +170,6 @@ export function Toolbar(): JSX.Element {
className={clsx(
'Toolbar',
minimized && 'Toolbar--minimized',
!isAuthenticated && 'Toolbar--unauthenticated',
hedgehogMode && 'Toolbar--hedgehog-mode',
isDragging && 'Toolbar--dragging'
)}
Expand All @@ -178,19 +184,32 @@ export function Toolbar(): JSX.Element {
}
>
<ToolbarButton
icon={<IconLogomark />}
onClick={isAuthenticated ? toggleMinimized : authenticate}
title={isAuthenticated ? 'Minimize' : 'Authenticate the PostHog Toolbar'}
titleMinimized={isAuthenticated ? 'Expand the toolbar' : 'Authenticate the PostHog Toolbar'}
/>
>
<IconLogomark />
</ToolbarButton>
{isAuthenticated ? (
<>
<ToolbarButton icon={<IconSearch />} menuId="inspect" />
<ToolbarButton icon={<IconCursorClick />} menuId="heatmap" />
<ToolbarButton icon={<IconBolt />} menuId="actions" />
<ToolbarButton icon={<IconToggle />} menuId="flags" title="Feature flags" />
<ToolbarButton menuId="inspect">
<IconSearch />
</ToolbarButton>
<ToolbarButton menuId="heatmap">
<IconCursorClick />
</ToolbarButton>
<ToolbarButton menuId="actions">
<IconBolt />
</ToolbarButton>
<ToolbarButton menuId="flags" title="Feature flags">
<IconToggle />
</ToolbarButton>
</>
) : null}
) : (
<ToolbarButton flex onClick={authenticate}>
Authenticate
</ToolbarButton>
)}

<MoreMenu />
</div>
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/toolbar/bar/ToolbarButton.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
width: 2rem;
height: 2rem;
min-height: var(--lemon-button-height);
margin: 0.25rem;
font-weight: 600;
color: var(--muted-alt);
appearance: none !important; // Important as this gets overridden by Ant styles...
cursor: pointer;
Expand Down Expand Up @@ -43,4 +45,13 @@
}
}
}

&--flex {
flex-grow: 1;
width: auto;

button {
width: 100%;
}
}
}
13 changes: 9 additions & 4 deletions frontend/src/toolbar/bar/ToolbarButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ import React from 'react'
import { MenuState, toolbarLogic } from './toolbarLogic'

export type ToolbarButtonProps = {
icon: React.ReactElement | null
children: React.ReactNode
onClick?: () => void
title?: string
titleMinimized?: JSX.Element | string
menuId?: MenuState
flex?: boolean
}

export const ToolbarButton: FunctionComponent<ToolbarButtonProps> = React.forwardRef<
HTMLDivElement,
ToolbarButtonProps
>(({ icon, title, onClick, titleMinimized, menuId, ...props }, ref): JSX.Element => {
>(({ children, title, onClick, titleMinimized, menuId, flex, ...props }, ref): JSX.Element => {
const { visibleMenu, minimized, isDragging } = useValues(toolbarLogic)
const { setVisibleMenu } = useActions(toolbarLogic)

Expand Down Expand Up @@ -54,9 +55,13 @@ export const ToolbarButton: FunctionComponent<ToolbarButtonProps> = React.forwar
}

const theButton = (
<div className={clsx('ToolbarButton', active && 'ToolbarButton--active')} aria-label={theTitle} ref={ref}>
<div
className={clsx('ToolbarButton', active && 'ToolbarButton--active', flex && 'ToolbarButton--flex')}
aria-label={theTitle}
ref={ref}
>
<button className="ToolbarButton__button" {...props} onClick={_onClick}>
{icon}
{children}
</button>
</div>
)
Expand Down
5 changes: 0 additions & 5 deletions frontend/src/toolbar/flags/flagsToolbarLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ export const flagsToolbarLogic = kea<flagsToolbarLogicType>([
`/api/projects/@current/feature_flags/my_flags${encodeParams(params, '?')}`
)

if (response.status >= 400) {
toolbarConfigLogic.actions.tokenExpired()
return []
}

breakpoint()
if (!response.ok) {
return []
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/toolbar/toolbarConfigLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,12 @@ export async function toolbarFetch(
})
if (response.status === 403) {
const responseData = await response.json()
// Do not try to authenticate if the user has no project access altogether
if (responseData.detail !== "You don't have access to the project.") {
if (responseData.detail === "You don't have access to the project.") {
toolbarConfigLogic.actions.authenticate()
}
}
if (response.status == 401) {
toolbarConfigLogic.actions.tokenExpired()
}
return response
}
87 changes: 0 additions & 87 deletions frontend/src/toolbar/toolbarLogic.ts

This file was deleted.

0 comments on commit d5cc9e4

Please sign in to comment.