Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Refactor WithSideBar and AccountLayout

Joshen Lim319b018793a0
diff --git a/studio/components/layouts/AccountLayout/AccountLayout.tsx b/studio/components/layouts/AccountLayout/AccountLayout.tsx
index dd57fa311fd..f25924a6652 100644
--- a/studio/components/layouts/AccountLayout/AccountLayout.tsx
+++ b/studio/components/layouts/AccountLayout/AccountLayout.tsx
@@ -1,21 +1,22 @@
import Head from 'next/head'
+import { FC, ReactNode } from 'react'
import { useRouter } from 'next/router'
import { observer } from 'mobx-react-lite'
-import { API_URL, IS_PLATFORM } from 'lib/constants'
+import { IS_PLATFORM } from 'lib/constants'
import { useStore, withAuth, useFlag } from 'hooks'
import WithSidebar from './WithSidebar'
import { auth } from 'lib/gotrue'
-/**
- * layout for dashboard homepage, account and org settings
- *
- * @param {String} title
- * @param {JSX.Element|JSX.Element[]} children
- * @param {Array<Object>} breadcrumbs
- */
+// Layout for dashboard homepage, account and org settings
-const AccountLayout = ({ children, title, breadcrumbs }: any) => {
+interface Props {
+ title: string
+ breadcrumbs: Object[]
+ children: ReactNode
+}
+
+const AccountLayout: FC<Props> = ({ children, title, breadcrumbs }) => {
const router = useRouter()
const { app, ui } = useStore()
@@ -27,36 +28,29 @@ const AccountLayout = ({ children, title, breadcrumbs }: any) => {
router.reload()
}
- const baseLogoutLink = {
- icon: '/icons/feather/power.svg',
- label: 'Logout',
- href: `${API_URL}/logout`,
- key: `${API_URL}/logout`,
- }
-
- const logoutLink = { ...baseLogoutLink, href: undefined, onClick: onClickLogout }
-
const organizationsLinks = app.organizations
.list()
.map((x: any) => ({
- isActive: router.pathname.startsWith('/org/') && ui.selectedOrganization?.slug == x.slug,
- label: x.name,
+ key: `${x.slug}-settings`,
href: `/org/${x.slug}/settings`,
+ label: x.name,
+ isActive: router.pathname.startsWith('/org/') && ui.selectedOrganization?.slug === x.slug,
}))
.sort((a, b) => a.label.localeCompare(b.label))
- let linksWithHeaders = [
+ const sidebarLinks = [
{
heading: 'Projects',
links: [
{
- isActive: router.pathname == '/',
- label: 'All projects',
+ key: 'all-projects',
href: '/',
+ label: 'All projects',
+ isActive: router.pathname === '/',
},
],
},
- ...(IS_PLATFORM
+ ...(IS_PLATFORM && organizationsLinks.length > 0
? [
{
heading: 'Organizations',
@@ -70,18 +64,18 @@ const AccountLayout = ({ children, title, breadcrumbs }: any) => {
heading: 'Account',
links: [
{
- isActive: router.pathname == `/account/me`,
+ key: 'account-me',
+ href: '/account/me',
icon: '/img/user.svg',
label: 'Preferences',
- href: `/account/me`,
- key: `/account/me`,
+ isActive: router.pathname === '/account/me',
},
{
- isActive: router.pathname == `/account/tokens`,
+ key: 'account-tokens',
+ href: '/account/tokens',
icon: '/img/user.svg',
label: 'Access Tokens',
- href: `/account/tokens`,
- key: `/account/tokens`,
+ isActive: router.pathname === '/account/tokens',
},
],
},
@@ -92,26 +86,33 @@ const AccountLayout = ({ children, title, breadcrumbs }: any) => {
links: [
{
key: 'ext-guides',
+ href: 'https://supabase.com/docs',
icon: '/img/book.svg',
label: 'Guides',
- href: 'https://supabase.com/docs',
- external: true,
+ isExternal: true,
},
{
key: 'ext-guides',
+ href: 'https://supabase.com/docs/guides/api',
icon: '/img/book-open.svg',
label: 'API Reference',
- href: 'https://supabase.com/docs/guides/api',
- external: true,
+ isExternal: true,
},
],
},
{
- links: [logoutLink],
+ heading: '',
+ links: [
+ {
+ key: 'logout',
+ icon: '/icons/feather/power.svg',
+ label: 'Logout',
+ onClick: onClickLogout,
+ },
+ ],
},
]
- if (!organizationsLinks?.length)
- linksWithHeaders = linksWithHeaders.filter((x: any) => x.heading != 'Organizations')
+
return (
<>
<Head>
@@ -124,7 +125,7 @@ const AccountLayout = ({ children, title, breadcrumbs }: any) => {
style={{ height: maxHeight, maxHeight }}
className="flex w-full flex-1 flex-col overflow-y-auto"
>
- <WithSidebar title={title} breadcrumbs={breadcrumbs} links={linksWithHeaders}>
+ <WithSidebar title={title} breadcrumbs={breadcrumbs} links={sidebarLinks}>
{children}
</WithSidebar>
</main>
diff --git a/studio/components/layouts/AccountLayout/AccountLayout.types.ts b/studio/components/layouts/AccountLayout/AccountLayout.types.ts
new file mode 100644
index 00000000000..89144025c95
--- /dev/null
+++ b/studio/components/layouts/AccountLayout/AccountLayout.types.ts
@@ -0,0 +1,15 @@
+export interface SidebarLinkGroup {
+ heading: string
+ links: SidebarLink[]
+}
+
+export interface SidebarLink {
+ key: string
+ label: string
+ icon?: string
+ href?: string
+ subitemsKey?: string // I dont think this is being used at all
+ isActive?: boolean
+ isExternal?: boolean
+ onClick?: () => void
+}
diff --git a/studio/components/layouts/AccountLayout/SidebarItem.tsx b/studio/components/layouts/AccountLayout/SidebarItem.tsx
new file mode 100644
index 00000000000..a5d63c3b125
--- /dev/null
+++ b/studio/components/layouts/AccountLayout/SidebarItem.tsx
@@ -0,0 +1,73 @@
+import { FC } from 'react'
+import Link from 'next/link'
+import { Menu, IconLogOut, IconArrowUpRight } from '@supabase/ui'
+
+interface Props {
+ id: any
+ label: string
+ href?: string
+ isActive?: boolean
+ isSubitem?: boolean
+ isExternal?: boolean
+ onClick?: () => void
+}
+
+const SidebarItem: FC<Props> = ({
+ id,
+ label,
+ href,
+ isActive = false,
+ isSubitem = false,
+ isExternal = false,
+ onClick = () => {},
+}) => {
+ if (href === undefined) {
+ const icon = isExternal ? (
+ <IconArrowUpRight size="tiny" />
+ ) : label === 'Logout' ? (
+ <IconLogOut size="tiny" />
+ ) : undefined
+
+ return (
+ <Menu.Item
+ rounded
+ key={id}
+ style={{ marginLeft: isSubitem ? '.5rem' : '0' }}
+ active={isActive}
+ onClick={onClick}
+ icon={icon}
+ >
+ {isSubitem ? <p className="text-sm">{label}</p> : label}
+ </Menu.Item>
+ )
+ }
+
+ return (
+ <Link href={href || ''}>
+ <a className="block" target={isExternal ? '_blank' : '_self'}>
+ <button
+ className={[
+ 'ring-scale-1200 border-scale-500 group-hover:border-scale-900 group',
+ 'flex max-w-full cursor-pointer items-center space-x-2 py-1 font-normal',
+ 'outline-none focus-visible:z-10 focus-visible:ring-1',
+ ].join(' ')}
+ onClick={onClick}
+ >
+ {isExternal && (
+ <span className="text-scale-900 group-hover:text-scale-1100 truncate text-sm transition">
+ <IconArrowUpRight size="tiny" />
+ </span>
+ )}
+ <span
+ title={label}
+ className="text-scale-1100 group-hover:text-scale-1200 w-full truncate text-sm transition"
+ >
+ {isSubitem ? <p>{label}</p> : label}
+ </span>
+ </button>
+ </a>
+ </Link>
+ )
+}
+
+export default SidebarItem
diff --git a/studio/components/layouts/AccountLayout/WithSidebar.tsx b/studio/components/layouts/AccountLayout/WithSidebar.tsx
index b6d3b9bf74a..036844fdbb2 100644
--- a/studio/components/layouts/AccountLayout/WithSidebar.tsx
+++ b/studio/components/layouts/AccountLayout/WithSidebar.tsx
@@ -1,14 +1,15 @@
-import Link from 'next/link'
import { FC, ReactNode } from 'react'
-import { isUndefined } from 'lodash'
-import { Menu, Typography, IconArrowUpRight, Badge, IconLogOut } from '@supabase/ui'
+import { Menu, Badge } from '@supabase/ui'
+
import { useFlag } from 'hooks'
import LayoutHeader from '../ProjectLayout/LayoutHeader'
+import SidebarItem from './SidebarItem'
+import { SidebarLink, SidebarLinkGroup } from './AccountLayout.types'
interface Props {
title: string
breadcrumbs: any[]
- links: any[]
+ links: SidebarLinkGroup[] | SidebarLink[]
header?: ReactNode
subitems?: any[]
subitemsParentKey?: number
@@ -29,7 +30,7 @@ const WithSidebar: FC<Props> = ({
customSidebarContent,
}) => {
const noContent = !links && !customSidebarContent
- const linksHaveHeaders = links && links[0].heading
+ const linksHaveHeaders = links && (links[0] as any)?.heading
const ongoingIncident = useFlag('ongoingIncident')
const maxHeight = ongoingIncident ? 'calc(100vh - 44px)' : '100vh'
@@ -46,28 +47,24 @@ const WithSidebar: FC<Props> = ({
].join(' ')}
>
{title && (
- <div className="mb-2">
- <div className="dark:border-dark flex h-12 max-h-12 items-center border-b px-6">
- <Typography.Title level={4} className="mb-0">
- {title}
- </Typography.Title>
- </div>
+ <div className="dark:border-dark flex h-12 max-h-12 items-center border-b px-6 mb-2">
+ <h4>{title}</h4>
</div>
)}
- {header && header}
+ {header !== undefined && header}
<div className="-mt-1">
<Menu>
{customSidebarContent}
{links && linksHaveHeaders ? (
<LinksWithHeaders
- links={links}
+ links={links as SidebarLinkGroup[]}
subitems={subitems}
subitemsParentKey={subitemsParentKey}
/>
) : null}
{!linksHaveHeaders && links ? (
<LinksWithoutHeaders
- links={links}
+ links={links as SidebarLink[]}
subitems={subitems}
subitemsParentKey={subitemsParentKey}
/>
@@ -87,7 +84,7 @@ export default WithSidebar
const LinksWithHeaders: FC<any> = ({ links, subitems, subitemsParentKey }) => {
return links.map((x: any, i: number) => (
- <div key={x.heading || `${i}`} className="dark:border-dark border-b py-5 px-6">
+ <div key={x.heading || `heading_${i}`} className="dark:border-dark border-b py-5 px-6">
{x.heading && <Menu.Group title={x.heading} />}
{x.versionLabel && (
<div className="mb-1 px-3">
@@ -104,23 +101,26 @@ const LinksWithHeaders: FC<any> = ({ links, subitems, subitemsParentKey }) => {
</div>
))
}
-const LinksWithoutHeaders: FC<any> = ({ links, subitems, subitemsParentKey }) => {
+
+const LinksWithoutHeaders: FC<{
+ links: SidebarLink[]
+ subitems?: any[]
+ subitemsParentKey?: number
+}> = ({ links, subitems, subitemsParentKey }) => {
return (
<ul className="space-y-1">
- {links.map((x: any, i: number) => {
- // disable active state for link with subitems
+ {links.map((x: SidebarLink, i: number) => {
+ // Disable active state for link with subitems
const isActive = x.isActive && !subitems
-
let render: any = (
<SidebarItem
- key={`${x.key || x.as}-${i}-sidebarItem`}
- id={`${x.key || x.as}-${i}`}
- slug={x.key}
+ key={`${x.key}-${i}-sidebarItem`}
+ id={`${x.key}-${i}`}
isActive={isActive}
label={x.label}
href={x.href}
onClick={x.onClick}
- external={x.external || false}
+ isExternal={x.isExternal || false}
/>
)
@@ -129,11 +129,10 @@ const LinksWithoutHeaders: FC<any> = ({ links, subitems, subitemsParentKey }) =>
<SidebarItem
key={`${y.key || y.as}-${i}-sidebarItem`}
id={`${y.key || y.as}-${i}`}
- slug={y.key}
- isSubitem={true}
label={y.label}
onClick={y.onClick}
- external={x.external || false}
+ isSubitem={true}
+ isExternal={x.isExternal || false}
/>
))
render = [render, ...subItemsRender]
@@ -144,54 +143,3 @@ const LinksWithoutHeaders: FC<any> = ({ links, subitems, subitemsParentKey }) =>
</ul>
)
}
-
-const SidebarItem: FC<any> = ({ id, label, href, isActive, isSubitem, onClick, external }) => {
- if (isUndefined(href)) {
- let icon
- if (external) {
- icon = <IconArrowUpRight size={'tiny'} />
- }
-
- if (label === 'Logout') {
- icon = <IconLogOut size={'tiny'} />
- }
-
- return (
- <Menu.Item
- rounded
- key={id}
- style={{
- marginLeft: isSubitem && '.5rem',
- }}
- active={isActive ? true : false}
- onClick={onClick || (() => {})}
- icon={icon}
- >
- {isSubitem ? <p className="text-sm">{label}</p> : label}
- </Menu.Item>
- )
- }
-
- return (
- <Link href={href || ''}>
- <a className="block" target={external ? '_blank' : '_self'}>
- <button
- className="ring-scale-1200 border-scale-500 group-hover:border-scale-900 group flex max-w-full cursor-pointer items-center space-x-2 py-1 font-normal outline-none focus-visible:z-10 focus-visible:ring-1"
- onClick={onClick || (() => {})}
- >
- {external && (
- <span className="text-scale-900 group-hover:text-scale-1100 truncate text-sm transition">
- <IconArrowUpRight size={'tiny'} />
- </span>
- )}
- <span
- title={label}
- className="text-scale-1100 group-hover:text-scale-1200 w-full truncate text-sm transition"
- >
- {isSubitem ? <p>{label}</p> : label}
- </span>
- </button>
- </a>
- </Link>
- )
-}