Skip to content

Commit

Permalink
feat: Add OpenExternalLinkItem component
Browse files Browse the repository at this point in the history
And update OpenExternalLinkButton
  • Loading branch information
Merkur39 committed Nov 27, 2024
1 parent 668bf97 commit 004a843
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/modules/public/OpenExternalLinkButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { openExternalLink } from 'modules/public/helpers'
* @param {boolean} isSharingShortcutCreated
* @returns {React.Component}
*/
const getIcon = (link, isSharingShortcutCreated) => {
export const getLabel = (link, isSharingShortcutCreated) => {
if (link === HOME_LINK_HREF) {
return ToTheCloudIcon
}
Expand All @@ -27,21 +27,36 @@ const getIcon = (link, isSharingShortcutCreated) => {
return SyncIcon
}

/**
* Get the icon and label to display in the button
* @param {object} params
* @param {string} params.link
* @param {boolean} params.isSharingShortcutCreated
* @param {object} params.t
* @returns {{ icon: React.Component, label: string }}
*/
export const getIconWithlabel = ({ link, isSharingShortcutCreated, t }) => {
if (link === HOME_LINK_HREF) {
return { icon: ToTheCloudIcon, label: t('Share.create-cozy') }
}
if (!isSharingShortcutCreated) {
return { icon: CloudPlusOutlinedIcon, label: t('toolbar.add_to_mine') }
}
return { icon: SyncIcon, label: t('toolbar.menu_sync_cozy') }
}

export const OpenExternalLinkButton = ({ link, isSharingShortcutCreated }) => {
const { t } = useI18n()

const handleClick = () => {
openExternalLink(link)
}

const label =
link === HOME_LINK_HREF
? t('Share.create-cozy')
: isSharingShortcutCreated
? t('toolbar.menu_sync_cozy')
: t('toolbar.add_to_mine')

const icon = getIcon(link, isSharingShortcutCreated)
const { icon, label } = getIconWithlabel({
link,
isSharingShortcutCreated,
t
})

return (
<Button
Expand Down
41 changes: 41 additions & 0 deletions src/modules/public/OpenExternalLinkItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import PropTypes from 'prop-types'
import React from 'react'

import ActionsMenuItem from 'cozy-ui/transpiled/react/ActionsMenu/ActionsMenuItem'
import Icon from 'cozy-ui/transpiled/react/Icon'
import ListItemIcon from 'cozy-ui/transpiled/react/ListItemIcon'
import ListItemText from 'cozy-ui/transpiled/react/ListItemText'
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'

import { getIconWithlabel } from 'modules/public/OpenExternalLinkButton'
import { openExternalLink } from 'modules/public/helpers'

const OpenExternalLinkItem = ({ link, isSharingShortcutCreated }) => {
const { t } = useI18n()

const handleClick = () => {
openExternalLink(link)
}

const { icon, label } = getIconWithlabel({
link,
isSharingShortcutCreated,
t
})

return (
<ActionsMenuItem isListItem onClick={handleClick}>
<ListItemIcon>
<Icon icon={icon} />
</ListItemIcon>
<ListItemText primary={label} />
</ActionsMenuItem>
)
}

OpenExternalLinkItem.propTypes = {
link: PropTypes.string.isRequired,
isSharingShortcutCreated: PropTypes.bool
}

export default OpenExternalLinkItem

0 comments on commit 004a843

Please sign in to comment.