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

Feat UI tweaks #1315

Merged
merged 5 commits into from
Mar 11, 2025
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
51 changes: 51 additions & 0 deletions frontend/app/src/components/molecules/nav-bar/banner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from 'react';
import { Button } from '@/components/ui/button';

export interface BannerProps {
message: JSX.Element;
type?: 'info' | 'warning' | 'success' | 'error';
actionText?: string;
onAction?: () => void;
}

export const Banner: React.FC<BannerProps> = ({
Copy link
Contributor

Choose a reason for hiding this comment

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

stylistic nit: I've been defining components like this:

export const Banner = ({foo, bar}: BannerProps) => {}

I don't care which we end up going for, but I think we should try to be consistent. WDYT? I kinda like the way I've been doing it because it's a little more terse, but I'm fine either way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

personally prefer React.FC since it adds some additional type safety if needed later

Copy link
Contributor

Choose a reason for hiding this comment

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

ah nice, vs. just JSX.Element? sounds good to me

message,
type = 'info',
actionText,
onAction,
}) => {
const getBgColor = () => {
switch (type) {
case 'warning':
return 'bg-amber-50 dark:bg-amber-900/20 text-amber-800 dark:text-amber-200';
case 'success':
return 'bg-green-50 dark:bg-green-900/20 text-green-800 dark:text-green-200';
case 'error':
return 'bg-red-50 dark:bg-red-900/20 text-red-800 dark:text-red-200';
default:
return 'bg-blue-50 dark:bg-blue-900/20 text-blue-800 dark:text-blue-200';
}
};

return (
<div className={`w-full py-2 px-4 h-12 ${getBgColor()}`}>
<div className="flex items-center justify-between">
<div className="flex flex-1 items-center">
<p className="text-sm font-medium">{message}</p>
</div>
<div className="flex items-center gap-2">
{actionText && onAction && (
<Button
variant="outline"
size="sm"
onClick={onAction}
className="text-xs font-medium hover:bg-opacity-20"
>
{actionText}
</Button>
)}
</div>
</div>
</div>
);
};
103 changes: 85 additions & 18 deletions frontend/app/src/components/molecules/nav-bar/nav-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ import {
BiUserCircle,
} from 'react-icons/bi';
import { useTheme } from '@/components/theme-provider';
import { useMemo } from 'react';
import { useEffect, useMemo } from 'react';
import useApiMeta from '@/pages/auth/hooks/use-api-meta';
import { VersionInfo } from '@/pages/main/info/components/version-info';
import { useTenant } from '@/lib/atoms';
import { routes } from '@/router';
import { TooltipContent, TooltipProvider } from '@/components/ui/tooltip';
import { Tooltip, TooltipTrigger } from '@/components/ui/tooltip';
import { Banner, BannerProps } from './banner';

function HelpDropdown() {
const meta = useApiMeta();
Expand Down Expand Up @@ -107,6 +108,8 @@ function HelpDropdown() {

function AccountDropdown({ user }: MainNavProps) {
const navigate = useNavigate();
const { tenant } = useTenant();

const { handleApiError } = useApiError({});

const { toggleTheme } = useTheme();
Expand Down Expand Up @@ -148,6 +151,12 @@ function AccountDropdown({ user }: MainNavProps) {
<DropdownMenuItem>
<VersionInfo />
</DropdownMenuItem>
{tenant?.version == TenantVersion.V1 &&
location.pathname.includes('v1') && (
<DropdownMenuItem onClick={() => navigate('/')}>
View Legacy V0 Data
</DropdownMenuItem>
)}
<DropdownMenuSeparator />
<DropdownMenuItem onClick={() => toggleTheme()}>
Toggle Theme
Expand Down Expand Up @@ -215,29 +224,87 @@ function VersionUpgradeButton() {

interface MainNavProps {
user: User;
setHasBanner?: (state: boolean) => void;
}

export default function MainNav({ user }: MainNavProps) {
export default function MainNav({ user, setHasBanner }: MainNavProps) {
const { toggleSidebarOpen } = useSidebar();
const { theme } = useTheme();
const { tenant } = useTenant();
const { pathname } = useLocation();
const navigate = useNavigate();
const [params] = useSearchParams();

const versionedRoutes = useMemo(
() =>
routes
.at(0)
?.children?.find((r) => r.path === '/v1/')
?.children?.find((r) => r.path === '/v1/' && r.children?.length)
?.children?.map((c) => c.path)
?.map((p) => p?.replace('/v1', '')) || [],
[],
);

const tenantVersion = tenant?.version || TenantVersion.V0;

const banner: BannerProps | undefined = useMemo(() => {
const shouldShowVersionUpgradeButton =
versionedRoutes.includes(pathname) && // It is a versioned route
!pathname.includes('/v1') && // The user is not already on the v1 version
tenantVersion === TenantVersion.V1; // The tenant is on the v1 version

if (shouldShowVersionUpgradeButton) {
return {
message: (
<>
You are viewing legacy V0 data for a tenant that was upgraded to V1
runtime.
</>
),
type: 'warning',
actionText: 'Upgrade',
onAction: () => {
navigate({
pathname: '/v1' + pathname,
search: params.toString(),
});
},
};
}

return;
}, [navigate, params, pathname, tenantVersion, versionedRoutes]);

useEffect(() => {
if (!setHasBanner) {
return;
}
setHasBanner(!!banner);
}, [setHasBanner, banner]);

return (
<div className="fixed top-0 w-screen h-16 border-b">
<div className="flex h-16 items-center pr-4 pl-4">
<button
onClick={() => toggleSidebarOpen()}
className="flex flex-row gap-4 items-center"
>
<img
src={theme == 'dark' ? hatchet : hatchetDark}
alt="Hatchet"
className="h-9 rounded"
/>
</button>
<div className="ml-auto flex items-center">
<HelpDropdown />
<AccountDropdown user={user} />
<VersionUpgradeButton />
<div className="fixed top-0 w-screen">
{banner && <Banner {...banner} />}

{/* Main Navigation Bar */}
<div className="h-16 border-b">
<div className="flex h-16 items-center pr-4 pl-4">
<button
onClick={() => toggleSidebarOpen()}
className="flex flex-row gap-4 items-center"
>
<img
src={theme == 'dark' ? hatchet : hatchetDark}
alt="Hatchet"
className="h-9 rounded"
/>
</button>
<div className="ml-auto flex items-center">
<HelpDropdown />
<AccountDropdown user={user} />
<VersionUpgradeButton />
</div>
</div>
</div>
</div>
Expand Down
40 changes: 37 additions & 3 deletions frontend/app/src/lib/atoms.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { atom, useAtom } from 'jotai';
import { Tenant, queries } from './api';
import { useSearchParams } from 'react-router-dom';
import { useCallback, useEffect, useMemo } from 'react';
import { Tenant, TenantVersion, queries } from './api';
import { useLocation, useNavigate, useSearchParams } from 'react-router-dom';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useQuery } from '@tanstack/react-query';

const getInitialValue = <T>(key: string, defaultValue?: T): T | undefined => {
Expand Down Expand Up @@ -121,6 +121,40 @@ export function useTenant(): TenantContext {
}
}, [searchParams, tenant, setTenant]);

// Set the correct path for tenant version
// NOTE: this is hacky and not ideal

const { pathname } = useLocation();
const navigate = useNavigate();
const [params] = useSearchParams();

const [lastRedirected, setLastRedirected] = useState<string | undefined>();

useEffect(() => {
// Only redirect on initial tenant load
if (lastRedirected == tenant?.slug) {
return;
}

setLastRedirected(tenant?.slug);

if (tenant?.version == TenantVersion.V0 && pathname.startsWith('/v1')) {
return navigate({
pathname: pathname.replace('/v1', ''),
search: params.toString(),
});
}

if (tenant?.version == TenantVersion.V1 && !pathname.startsWith('/v1')) {
return navigate({
pathname: '/v1' + pathname,
search: params.toString(),
});
}

console.log(tenant?.version);
}, [lastRedirected, navigate, params, pathname, tenant]);

if (!tenant) {
return {
tenant: undefined,
Expand Down
9 changes: 7 additions & 2 deletions frontend/app/src/pages/authenticated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Loading } from '@/components/ui/loading.tsx';
import { useQuery } from '@tanstack/react-query';
import SupportChat from '@/components/molecules/support-chat';
import AnalyticsProvider from '@/components/molecules/analytics-provider';
import { useState } from 'react';

const authMiddleware = async (currentUrl: string) => {
try {
Expand Down Expand Up @@ -104,6 +105,8 @@ export default function Authenticated() {
memberships: listMembershipsQuery.data?.rows || memberships,
});

const [hasHasBanner, setHasBanner] = useState(false);

if (!user || !memberships) {
return <Loading />;
}
Expand All @@ -112,8 +115,10 @@ export default function Authenticated() {
<AnalyticsProvider user={user}>
<SupportChat user={user}>
<div className="flex flex-row flex-1 w-full h-full">
<MainNav user={user} />
<div className="pt-16 flex-grow overflow-y-auto overflow-x-hidden">
<MainNav user={user} setHasBanner={setHasBanner} />
<div
className={`pt-${hasHasBanner ? 28 : 16} flex-grow overflow-y-auto overflow-x-hidden`}
>
<Outlet context={ctx} />
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/src/pages/onboarding/verify-email/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function VerifyEmail() {
}

return (
<div className="flex flex-row flex-1 w-full h-full pt-16">
<div className="flex flex-row flex-1 w-full h-full">
<MainNav user={res.user} />
<div className="container relative hidden flex-col items-center justify-center md:grid lg:max-w-none lg:grid-cols-2 lg:px-0">
<div className="lg:p-8 mx-auto w-screen">
Expand Down
Loading