Skip to content

Commit

Permalink
refactor: Use same method to join two paths together
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed May 23, 2024
1 parent cfe6d7a commit f36ada2
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 16 deletions.
9 changes: 9 additions & 0 deletions src/lib/path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Join two paths together ensuring there is only one slash between them
* @param {string} start
* @param {string} end
* @returns
*/
export function joinPath(start, end) {
return `${start}${start.endsWith('/') ? '' : '/'}${end}`
}
8 changes: 4 additions & 4 deletions src/modules/actions/helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { joinPath } from 'lib/path'

export const navigateToModal = ({ navigate, pathname, files, path }) => {
const file = Array.isArray(files) ? files[0] : files
return navigate(
`${pathname}${pathname.endsWith('/') ? '' : '/'}file/${file.id}/${path}`
)
return navigate(joinPath(pathname, `file/${file.id}/${path}`))
}

export const navigateToModalWithMultipleFile = ({
Expand All @@ -11,7 +11,7 @@ export const navigateToModalWithMultipleFile = ({
files,
path
}) => {
navigate(`${pathname}${pathname.endsWith('/') ? '' : '/'}${path}`, {
navigate(joinPath(pathname, path), {
state: { fileIds: files.map(file => file.id) }
})
}
4 changes: 3 additions & 1 deletion src/modules/drive/Toolbar/share/helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { joinPath } from 'lib/path'

/**
* Get the path to share the displayed folder
* @param {string} pathname Current path
* @returns Next path
*/
export function getPathToShareDisplayedFolder(pathname) {
return `${pathname}${pathname.endsWith('/') ? '' : '/'}share`
return joinPath(pathname, 'share')
}
5 changes: 2 additions & 3 deletions src/modules/filelist/cells/ShareContent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useNavigate, useLocation } from 'react-router-dom'

import { SharedStatus, useSharingContext } from 'cozy-sharing'

import { joinPath } from 'lib/path'
import HammerComponent from 'modules/filelist/HammerComponent'

import styles from 'styles/filelist.styl'
Expand All @@ -16,9 +17,7 @@ const ShareContent = ({ file, disabled, isInSyncFromSharing }) => {
const handleClick = () => {
if (!disabled) {
// should be only disabled
navigate(
`${pathname}${pathname.endsWith('/') ? '' : '/'}file/${file._id}/share`
)
navigate(joinPath(pathname, `file/${file._id}/share`))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/modules/nextcloud/components/NextcloudFolderBody.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { makeActions } from 'cozy-ui/transpiled/react/ActionsMenu/Actions'
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'

import { duplicateNextcloudFile } from './actions/duplicateNextcloudFile'
import { joinPath } from 'lib/path'
import { hr } from 'modules/actions'
import { FolderBody } from 'modules/folder/components/FolderBody'
import { downloadNextcloudFile } from 'modules/nextcloud/components/actions/downloadNextcloudFile'
Expand All @@ -14,15 +15,14 @@ import { openWithinNextcloud } from 'modules/nextcloud/components/actions/openWi
import { rename } from 'modules/nextcloud/components/actions/rename'
import { share } from 'modules/nextcloud/components/actions/share'
import { trash } from 'modules/nextcloud/components/actions/trash'
import { makePath } from 'modules/nextcloud/utils'

const NextcloudFolderBody = ({ path, queryResults }) => {
const [searchParams, setSearchParams] = useSearchParams()
const client = useClient()
const { t } = useI18n()

const handleFolderOpen = folder => {
searchParams.set('path', makePath(path, folder.name))
searchParams.set('path', joinPath(path, folder.name))
setSearchParams(searchParams)
}

Expand Down
3 changes: 0 additions & 3 deletions src/modules/nextcloud/utils.js

This file was deleted.

5 changes: 2 additions & 3 deletions src/targets/public/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import ErrorShare from 'components/Error/ErrorShare'
import appMetadata from 'lib/appMetadata'
import { schema } from 'lib/doctypes'
import logger from 'lib/logger'
import { joinPath } from 'lib/path'
import registerClientPlugins from 'lib/registerClientPlugins'
import { configureReporter, setCozyUrl } from 'lib/reporter'
import StyledApp from 'modules/drive/StyledApp'
Expand Down Expand Up @@ -96,9 +97,7 @@ const init = async () => {
if (isNote) {
try {
window.location.href = await models.note.fetchURL(client, data, {
pathname: `${location.pathname}${
location.pathname.endsWith('/') ? '' : '/'
}`
pathname: joinPath(location.pathname, '')
})
} catch (e) {
Alerter.error('alert.offline')
Expand Down

0 comments on commit f36ada2

Please sign in to comment.