Skip to content

Commit

Permalink
feat: Deprecates generateWebLink in AppLinker
Browse files Browse the repository at this point in the history
if you used `generateWebLink` from `cozy-ui AppLinker` component, you have now to use it from `cozy-client`. Simply replace `nativePath` prop by `hash` and add `pathname: '/'` prop.
  • Loading branch information
JF-Cozy committed Jan 18, 2023
1 parent 2a40c08 commit 3ab64e9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 41 deletions.
2 changes: 1 addition & 1 deletion react/AppLinker/index.jsx
@@ -1,7 +1,7 @@
import React from 'react'
import PropTypes from 'prop-types'
import { withClient } from 'cozy-client'

import { withClient } from 'cozy-client'
import {
checkApp,
startApp,
Expand Down
57 changes: 21 additions & 36 deletions react/AppLinker/native.js
@@ -1,44 +1,19 @@
import {
generateWebLink as generateWebLinkClient,
ensureFirstSlash
} from 'cozy-client'

import { UNIVERSAL_LINK_URL } from 'cozy-ui/transpiled/react/AppLinker/native.config.js'

export const getUniversalLinkDomain = () => {
return UNIVERSAL_LINK_URL
}

const ensureFirstSlash = path => {
if (!path) {
return '/'
} else {
return path.startsWith('/') ? path : '/' + path
}
}

/**
* generateWebLink - Construct a link to a web app
*
* @param {object} Options Object of options
* @param {string} options.cozyUrl Base URL of the cozy, eg. http://cozy.localhost:8080 or https://test.mycozy.cloud:8080
* @param {string} options.nativePath Path inside the app, eg. /files/test.jpg
* @param {string} options.slug Slug of the app
* @param {string} options.subDomainType Whether the cozy is using flat or nested subdomains. Defaults to flat.
*
* @return {string} Generated URL
*/
export const generateWebLink = ({
cozyUrl,
nativePath,
slug,
subDomainType
}) => {
const url = new URL(cozyUrl)
url.host =
subDomainType === 'nested'
? `${slug}.${url.host}`
: url.host
.split('.')
.map((x, i) => (i === 0 ? x + '-' + slug : x))
.join('.')
url.hash = ensureFirstSlash(nativePath)
return url.toString()
export const generateWebLink = ({ nativePath, ...props }) => {
console.warn(
'Deprecated: you should use generateWebLink from cozy-client instead'
)
return generateWebLinkClient({ pathname: '/', hash: nativePath, ...props })
}

/**
Expand All @@ -55,16 +30,26 @@ export const generateUniversalLink = options => {
const { slug, cozyUrl, subDomainType } = options
let { fallbackUrl, nativePath } = options
nativePath = ensureFirstSlash(nativePath)

if (!cozyUrl && !fallbackUrl) {
throw new Error(
'Must have either cozyUrl or fallbackUrl to generate universal link.'
)
}

if (cozyUrl && !fallbackUrl) {
fallbackUrl = generateWebLink({ cozyUrl, nativePath, slug, subDomainType })
fallbackUrl = generateWebLinkClient({
cozyUrl,
pathname: '/',
hash: nativePath,
slug,
subDomainType
})
}

const url = getUniversalLinkDomain(cozyUrl) + '/' + slug + nativePath
const urlObj = new URL(url)
urlObj.searchParams.append('fallback', fallbackUrl)

return urlObj.toString()
}
13 changes: 9 additions & 4 deletions react/AppLinker/native.spec.js
@@ -1,11 +1,14 @@
import { generateUniversalLink, generateWebLink } from './native'
import { generateWebLink } from 'cozy-client'

import { generateUniversalLink } from './native'

describe('native functions', () => {
describe('generating a web link', () => {
it('should generate a web link', () => {
const webLink = generateWebLink({
cozyUrl: 'https://test.cozy.tools:8080',
nativePath: '/files/1',
pathname: '/',
hash: '/files/1',
slug: 'drive'
})

Expand All @@ -15,15 +18,17 @@ describe('native functions', () => {
it('should handle both types of subdomains', () => {
const flatLink = generateWebLink({
cozyUrl: 'https://test.cozy.tools:8080',
nativePath: '/files/1',
pathname: '/',
hash: '/files/1',
slug: 'drive',
subDomainType: 'flat'
})
expect(flatLink).toEqual('https://test-drive.cozy.tools:8080/#/files/1')

const nestedLink = generateWebLink({
cozyUrl: 'https://cozy.tools:8080',
nativePath: '/files/1',
pathname: '/',
hash: '/files/1',
slug: 'drive',
subDomainType: 'nested'
})
Expand Down

0 comments on commit 3ab64e9

Please sign in to comment.