Skip to content

Commit

Permalink
feat: Add Documents link in sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
zatteo committed Oct 31, 2024
1 parent c8e4bc8 commit 690a69a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/components/AppLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { Outlet, useLocation, useNavigate } from 'react-router-dom'

import { BarCenter, BarComponent } from 'cozy-bar'
import { useClient } from 'cozy-client'
import Icon from 'cozy-ui/transpiled/react/Icon'
import CalendarIcon from 'cozy-ui/transpiled/react/Icons/Calendar'
import CheckboxIcon from 'cozy-ui/transpiled/react/Icons/Checkbox'
import FolderIcon from 'cozy-ui/transpiled/react/Icons/Folder'
import OpenappIcon from 'cozy-ui/transpiled/react/Icons/Openapp'
import PieChartIcon from 'cozy-ui/transpiled/react/Icons/PieChart'
import WalkIcon from 'cozy-ui/transpiled/react/Icons/Walk'
import { Content, Layout, Main } from 'cozy-ui/transpiled/react/Layout'
Expand All @@ -24,6 +27,7 @@ import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'

import { useAccountContext } from './Provider/AccountProvider'
import { EmptyDataView } from './Views/EmptyDataView'
import useCurrentAccountFolderLink from '../hooks/useCurrentAccountFolderLink'

const ExampleRouterNavLink = ({
children,
Expand All @@ -50,6 +54,7 @@ const AppLayout = () => {
const location = useLocation()
const currentTab = location.pathname.slice(1)
const { accountsList, fetchStatus } = useAccountContext()
const currentAccountFolderLink = useCurrentAccountFolderLink()

const makeProps = route => {
const routeIsMatching = currentTab.includes(route[0])
Expand Down Expand Up @@ -106,6 +111,28 @@ const AppLayout = () => {
<NavText>{t('Sidebar.presence')}</NavText>
</NavLink>
</NavItem>
{currentAccountFolderLink && (
<a
href={currentAccountFolderLink}
target="_blank"
rel="noreferrer"
style={{ textDecoration: 'none' }}
>
<NavItem>
<NavLink>
<NavIcon icon={FolderIcon} />
<NavText>{t('Sidebar.documents')}</NavText>
<Icon
icon={OpenappIcon}
style={{
marginLeft: 'auto',
marginRight: 8
}}
/>
</NavLink>
</NavItem>
</a>
)}
</Nav>
</Sidebar>
<BarComponent />
Expand Down
45 changes: 45 additions & 0 deletions src/hooks/useCurrentAccountFolderLink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { useState, useEffect } from 'react'
import { buildAccountFolderQuery } from 'src/queries'

import { useClient, generateWebLink } from 'cozy-client'

import { useAccountContext } from '../components/Provider/AccountProvider'

const useCurrentAccountFolderLink = () => {
const client = useClient()
const { currentAccount } = useAccountContext()

const [currentAccountFolderLink, setCurrentAccountFolderLink] = useState(null)

useEffect(() => {
const fetchCurrentAccountFolderLink = async () => {
const accountFolderQuery = buildAccountFolderQuery(
currentAccount?.cozyMetadata?.sourceAccountIdentifier
)

const { data } = await client.query(
accountFolderQuery.definition(),
accountFolderQuery.options
)

const webLink = generateWebLink({
cozyUrl: client.getStackClient().uri,
slug: 'drive',
subDomainType: client.capabilities.flat_subdomains ? 'flat' : 'nested',
pathname: '',
hash: `/folder/${data[0]._id}`,
searchParams: []
})

setCurrentAccountFolderLink(webLink)
}

if (currentAccount) {
fetchCurrentAccountFolderLink()
}
}, [client, currentAccount])

return currentAccountFolderLink
}

export default useCurrentAccountFolderLink
3 changes: 2 additions & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"timetable": "Timetable",
"homeworks": "Homeworks",
"grades": "Grades",
"presence": "Attendance"
"presence": "Attendance",
"documents": "Documents"
},
"Timetable": {
"title": "Timetable",
Expand Down
3 changes: 2 additions & 1 deletion src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"timetable": "Emploi du temps",
"homeworks": "Travail à faire",
"grades": "Notes",
"presence": "Vie scolaire"
"presence": "Vie scolaire",
"documents": "Documents"
},
"Timetable": {
"title": "Emploi du temps",
Expand Down
15 changes: 15 additions & 0 deletions src/queries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,18 @@ export const buildPresenceQuery = sourceAccountIdentifier => ({
fetchPolicy: defaultFetchPolicy
}
})

export const buildAccountFolderQuery = accountIdentifier => ({
definition: () =>
Q('io.cozy.files')
.partialIndex({ type: 'directory', trashed: false })
.referencedBy({
_id: accountIdentifier,
_type: 'io.cozy.accounts.sourceAccountIdentifier'
}),
options: {
as: 'io.cozy.files/byReferencedAccountIdentifier/' + accountIdentifier,
fetchPolicy: defaultFetchPolicy,
singleDocData: true
}
})

0 comments on commit 690a69a

Please sign in to comment.