-
Notifications
You must be signed in to change notification settings - Fork 228
feat(workspaces): move tab rendering to the plugins COMPASS-9413 #6997
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
Changes from all commits
ed107db
db1ee5d
7eb2cb9
657307b
5019960
743fa2a
7749a93
3e0c612
a1757f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import toNS from 'mongodb-ns'; | ||
import { | ||
useConnectionInfo, | ||
useConnectionsListRef, | ||
useTabConnectionTheme, | ||
} from '@mongodb-js/compass-connections/provider'; | ||
import { | ||
WorkspaceTab, | ||
type WorkspaceTabCoreProps, | ||
} from '@mongodb-js/compass-components'; | ||
import type { WorkspacePluginProps } from '@mongodb-js/compass-workspaces'; | ||
|
||
import { type CollectionState } from './modules/collection-tab'; | ||
|
||
export const CollectionWorkspaceTitle = 'Collection' as const; | ||
|
||
type PluginTitleProps = { | ||
isTimeSeries?: boolean; | ||
isReadonly?: boolean; | ||
sourceName?: string | null; | ||
} & WorkspaceTabCoreProps & | ||
WorkspacePluginProps<typeof CollectionWorkspaceTitle>; | ||
|
||
function _PluginTitle({ | ||
editViewName, | ||
isNonExistent, | ||
isReadonly, | ||
isTimeSeries, | ||
sourceName, | ||
namespace, | ||
...tabProps | ||
}: PluginTitleProps) { | ||
const { getThemeOf } = useTabConnectionTheme(); | ||
const { getConnectionById } = useConnectionsListRef(); | ||
const { id: connectionId } = useConnectionInfo(); | ||
|
||
const { database, collection, ns } = toNS(namespace); | ||
const connectionName = getConnectionById(connectionId)?.title || ''; | ||
const collectionType = isTimeSeries | ||
? 'timeseries' | ||
: isReadonly | ||
? 'view' | ||
: 'collection'; | ||
// Similar to what we have in the collection breadcrumbs. | ||
const tooltip: [string, string][] = [ | ||
['Connection', connectionName || ''], | ||
['Database', database], | ||
]; | ||
if (sourceName) { | ||
tooltip.push(['View', collection]); | ||
tooltip.push(['Derived from', toNS(sourceName).collection]); | ||
} else if (editViewName) { | ||
tooltip.push(['View', toNS(editViewName).collection]); | ||
tooltip.push(['Derived from', collection]); | ||
} else { | ||
tooltip.push(['Collection', collection]); | ||
} | ||
|
||
return ( | ||
<WorkspaceTab | ||
{...tabProps} | ||
connectionName={connectionName} | ||
type={CollectionWorkspaceTitle} | ||
title={collection} | ||
tooltip={tooltip} | ||
iconGlyph={ | ||
collectionType === 'view' | ||
? 'Visibility' | ||
: collectionType === 'timeseries' | ||
? 'TimeSeries' | ||
: isNonExistent | ||
? 'EmptyFolder' | ||
: 'Folder' | ||
} | ||
data-namespace={ns} | ||
tabTheme={getThemeOf(connectionId)} | ||
isNonExistent={isNonExistent} | ||
/> | ||
); | ||
} | ||
|
||
export const CollectionPluginTitleComponent = connect( | ||
(state: CollectionState) => ({ | ||
isTimeSeries: state.metadata?.isTimeSeries, | ||
isReadonly: state.metadata?.isReadonly, | ||
sourceName: state.metadata?.sourceName, | ||
}) | ||
)(_PluginTitle); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import { spacing } from '@leafygreen-ui/tokens'; | |
import type { GlyphName } from '@leafygreen-ui/icon'; | ||
import { useSortable } from '@dnd-kit/sortable'; | ||
import { CSS as cssDndKit } from '@dnd-kit/utilities'; | ||
import { useId } from '@react-aria/utils'; | ||
import { useDarkMode } from '../../hooks/use-theme'; | ||
import { Icon, IconButton } from '../leafygreen'; | ||
import { mergeProps } from '../../utils/merge-props'; | ||
|
@@ -149,6 +150,10 @@ const draggingTabStyles = css({ | |
cursor: 'grabbing !important', | ||
}); | ||
|
||
const nonExistentStyles = css({ | ||
color: palette.gray.base, | ||
}); | ||
|
||
const tabIconStyles = css({ | ||
color: 'currentColor', | ||
marginLeft: spacing[300], | ||
|
@@ -185,25 +190,34 @@ const workspaceTabTooltipStyles = css({ | |
textWrap: 'wrap', | ||
}); | ||
|
||
type TabProps = { | ||
// The plugins provide these essential props use to render the tab. | ||
// The workspace-tabs component provides the other parts of TabProps. | ||
export type WorkspaceTabPluginProps = { | ||
connectionName?: string; | ||
type: string; | ||
title: string; | ||
title: React.ReactNode; | ||
isNonExistent?: boolean; | ||
iconGlyph: GlyphName | 'Logo' | 'Server'; | ||
tooltip?: [string, string][]; | ||
tabTheme?: Partial<TabTheme>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could get rid of this prop and instead use a provider. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense to me, and then we can also add it to all the shared workspace providers. Then we can also use these colors inside the workspaces UI easily if we need to |
||
}; | ||
|
||
export type WorkspaceTabCoreProps = { | ||
isSelected: boolean; | ||
isDragging: boolean; | ||
onSelect: () => void; | ||
onClose: () => void; | ||
iconGlyph: GlyphName | 'Logo' | 'Server'; | ||
tabContentId: string; | ||
tooltip?: [string, string][]; | ||
tabTheme?: Partial<TabTheme>; | ||
}; | ||
|
||
type TabProps = WorkspaceTabCoreProps & WorkspaceTabPluginProps; | ||
|
||
function Tab({ | ||
connectionName, | ||
type, | ||
title, | ||
tooltip, | ||
isNonExistent, | ||
isSelected, | ||
isDragging, | ||
onSelect, | ||
|
@@ -213,7 +227,7 @@ function Tab({ | |
tabTheme, | ||
className: tabClassName, | ||
...props | ||
}: TabProps & React.HTMLProps<HTMLDivElement>) { | ||
}: TabProps & Omit<React.HTMLProps<HTMLDivElement>, 'title'>) { | ||
const darkMode = useDarkMode(); | ||
const defaultActionProps = useDefaultAction(onSelect); | ||
const { listeners, setNodeRef, transform, transition } = useSortable({ | ||
|
@@ -240,6 +254,8 @@ function Tab({ | |
cursor: 'grabbing !important', | ||
}; | ||
|
||
const tabId = useId(); | ||
|
||
return ( | ||
<Tooltip | ||
enabled={!!tooltip} | ||
|
@@ -254,6 +270,7 @@ function Tab({ | |
className={cx( | ||
tabStyles, | ||
themeClass, | ||
isNonExistent && nonExistentStyles, | ||
isSelected && selectedTabStyles, | ||
isSelected && tabTheme && selectedThemedTabStyles, | ||
isDragging && draggingTabStyles, | ||
|
@@ -267,6 +284,7 @@ function Tab({ | |
data-testid="workspace-tab-button" | ||
data-connection-name={connectionName} | ||
data-type={type} | ||
id={tabId} | ||
{...tabProps} | ||
> | ||
{iconGlyph === 'Logo' && ( | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a small issue with
useConnectionInfo
implementation (and so the types are correctly hiding thattitle
is already returned here due to the test value fallback), but you can clean this up a bit and just access title from connectionInfo returned by the hook (but feel free to keep this out of this PR to avoid more changes piling up)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good callout. Tests are green now, I'll do that in a follow up!