Skip to content

Commit

Permalink
feat(default-layout): add tooltip content to create document button w…
Browse files Browse the repository at this point in the history
…hen no schema is created
  • Loading branch information
hermanwikner committed Apr 26, 2022
1 parent 6eef249 commit e088e07
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions packages/@sanity/default-layout/src/navbar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import config from 'config:sanity'
import * as sidecar from 'part:@sanity/default-layout/sidecar?'
import ToolMenu from 'part:@sanity/default-layout/tool-switcher'
import styled from 'styled-components'
import {TemplatePermissionsResult} from '@sanity/base/_internal'
import {getNewDocumentOptions, TemplatePermissionsResult} from '@sanity/base/_internal'
import {HAS_SPACES} from '../util/spaces'
import {DatasetSelect} from '../datasetSelect'
import {useDefaultLayoutRouter} from '../useDefaultLayoutRouter'
Expand Down Expand Up @@ -102,6 +102,8 @@ const SpacingBox = styled(Box)`
* ```
*/

const newDocumentOptions = getNewDocumentOptions()

export const Navbar = memo(function Navbar(props: NavbarProps) {
const {
createMenuIsOpen,
Expand Down Expand Up @@ -129,6 +131,8 @@ export const Navbar = memo(function Navbar(props: NavbarProps) {

const {projectId} = versionedClient.config()

const noDocumentOptions = newDocumentOptions?.length === 0

const canCreateSome = useMemo(() => {
if (isTemplatePermissionsLoading) return false

Expand Down Expand Up @@ -196,6 +200,23 @@ export const Navbar = memo(function Navbar(props: NavbarProps) {
[rootState]
)

const createTooltipContent = useMemo(() => {
if (noDocumentOptions) {
return <Text size={1}>You need a schema to create a new document</Text>
}

if (canCreateSome) {
return <Text size={1}>Create new document</Text>
}

return (
<InsufficientPermissionsMessage
currentUser={currentUser}
operationLabel="create any document"
/>
)
}, [canCreateSome, currentUser, noDocumentOptions])

// The HTML elements that are part of the search view (i.e. the "close" button that is visible
// when in fullscreen mode on narrow devices) needs to be passed to `<Autocomplete />` so it knows
// how to make the search experience work properly for non-sighted users.
Expand Down Expand Up @@ -236,22 +257,7 @@ export const Navbar = memo(function Navbar(props: NavbarProps) {
)}

<LegacyLayerProvider zOffset="navbarPopover">
<Tooltip
portal
scheme="light"
content={
<Box padding={2}>
{canCreateSome ? (
<Text size={1}>Create new document</Text>
) : (
<InsufficientPermissionsMessage
currentUser={currentUser}
operationLabel="create any document"
/>
)}
</Box>
}
>
<Tooltip portal scheme="light" content={<Box padding={2}>{createTooltipContent}</Box>}>
<SpacingBox marginRight={shouldRender.brandingCenter ? undefined : 2}>
<Button
aria-label="Create new document"
Expand Down

3 comments on commit e088e07

@vercel
Copy link

@vercel vercel bot commented on e088e07 Apr 26, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

studio-workshop – ./dev/workshop

studio-workshop-git-next.sanity.build
studio-workshop.sanity.build

@vercel
Copy link

@vercel vercel bot commented on e088e07 Apr 26, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

test-studio – ./

test-studio.sanity.build
test-studio-git-next.sanity.build

@vercel
Copy link

@vercel vercel bot commented on e088e07 Apr 26, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

perf-studio – ./

perf-studio-git-next.sanity.build
perf-studio.sanity.build

Please sign in to comment.