Skip to content

Commit

Permalink
feat: Add badge to Sharings nav item
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed Aug 10, 2023
1 parent f7c051c commit 0cd7650
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/drive/web/modules/navigation/Nav.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import React from 'react'
import UINav from 'cozy-ui/transpiled/react/Nav'

import { NavItem } from 'drive/web/modules/navigation/NavItem'
import { SharingsNavItem } from 'drive/web/modules/navigation/SharingsNavItem'

export const Nav = () => (
<UINav>
<NavItem to="/folder" icon="folder" label="drive" />
<NavItem to="/recent" icon="clock" label="recent" />
<NavItem to="/sharings" icon="share" label="sharings" />
<SharingsNavItem />
<NavItem to="/trash" icon="trash" label="trash" />
{__TARGET__ === 'mobile' ? (
<NavItem to="/settings" icon="gear" label="settings" />
Expand Down
54 changes: 54 additions & 0 deletions src/drive/web/modules/navigation/NavContent.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react'
import PropTypes from 'prop-types'

import { NavIcon, NavText } from 'cozy-ui/transpiled/react/Nav'

import Badge from 'cozy-ui/transpiled/react/Badge'
import useBreakpoints from 'cozy-ui/transpiled/react/hooks/useBreakpoints'

const NavContent = ({ icon, badgeContent, label }) => {
const { isDesktop } = useBreakpoints()

if (badgeContent) {
if (isDesktop) {
return (
<>
<NavIcon icon={icon} />
<NavText>{label}</NavText>
<Badge
center
className="u-ml-auto u-ph-1"
badgeContent={badgeContent}
color="error"
withBorder={false}
size="large"
/>
</>
)
} else {
return (
<>
<Badge badgeContent={badgeContent} color="error" withGreyBorder>
<NavIcon icon={icon} />
</Badge>
<NavText>{label}</NavText>
</>
)
}
}

return (
<>
<NavIcon icon={icon} />
<NavText>{label}</NavText>
</>
)
}

NavContent.propTypes = {
icon: PropTypes.string,
badgeContent: PropTypes.number,
label: PropTypes.string
}

export { NavContent }
19 changes: 11 additions & 8 deletions src/drive/web/modules/navigation/NavItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@ import React from 'react'
import { NavLink as RouterLink } from 'react-router-dom'
import PropTypes from 'prop-types'

import { useI18n } from 'cozy-ui/transpiled/react'

import {
NavItem as UINavItem,
NavIcon,
NavText,
genNavLinkForV6
} from 'cozy-ui/transpiled/react/Nav'
import { useI18n } from 'cozy-ui/transpiled/react/I18n'

import { NavContent } from 'drive/web/modules/navigation/NavContent'

const NavLink = genNavLinkForV6(RouterLink)

const NavItem = ({ to, icon, label }) => {
const NavItem = ({ to, icon, label, badgeContent }) => {
const { t } = useI18n()

return (
<UINavItem>
<NavLink to={to}>
<NavIcon icon={icon} />
<NavText>{t(`Nav.item_${label}`)}</NavText>
<NavContent
icon={icon}
label={t(`Nav.item_${label}`)}
badgeContent={badgeContent}
/>
</NavLink>
</UINavItem>
)
Expand All @@ -29,7 +31,8 @@ const NavItem = ({ to, icon, label }) => {
NavItem.propTypes = {
to: PropTypes.string.isRequired,
icon: PropTypes.string.isRequired,
label: PropTypes.string.isRequired
label: PropTypes.string.isRequired,
badgeContent: PropTypes.number
}

export { NavItem }
25 changes: 25 additions & 0 deletions src/drive/web/modules/navigation/SharingsNavItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react'

import { useQuery } from 'cozy-client'

import { buildNewSharingShortcutQuery } from 'drive/web/modules/queries'
import { NavItem } from 'drive/web/modules/navigation/NavItem'

const SharingsNavItem = () => {
const newSharingShortcutQuery = buildNewSharingShortcutQuery()
const newSharingShortcutResult = useQuery(
newSharingShortcutQuery.definition,
newSharingShortcutQuery.options
)

return (
<NavItem
to="/sharings"
icon="share"
label="sharings"
badgeContent={newSharingShortcutResult.data?.length}
/>
)
}

export { SharingsNavItem }
14 changes: 14 additions & 0 deletions src/drive/web/modules/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,17 @@ export const buildFolderByPathQuery = path => ({
fetchPolicy: defaultFetchPolicy
}
})

export const buildNewSharingShortcutQuery = () => ({
definition: () =>
Q('io.cozy.files')
.where({
class: 'shortcut',
'metadata.sharing.status': 'new'
})
.indexFields(['class', 'metadata.sharing.status']),
options: {
as: 'io.cozy.files/new_sharing_shortcut',
fetchPolicy: defaultFetchPolicy
}
})

0 comments on commit 0cd7650

Please sign in to comment.