Skip to content

Commit

Permalink
fix: Get shortcut img for links from applications in the same cozy
Browse files Browse the repository at this point in the history
Cozy applications only make their assets available if you are authenticated. As a result, the stack cannot retrieve the favicon. When a link comes from an application in the same Cozy, we can find out its slug and deduce its icon. For the moment, we can't do this for third-party Cozy applications because the URL formats are too diverse.
  • Loading branch information
cballevre committed Nov 16, 2023
1 parent 97998fb commit 0196470
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
29 changes: 20 additions & 9 deletions packages/cozy-client/src/hooks/useFetchShortcut.jsx
Expand Up @@ -24,15 +24,26 @@ const useFetchShortcut = (client, id) => {
}
})

const shortcutRemoteUrl = new URL(
shortcutInfosResult.data.attributes.url
)

const imgUrl = `${client.getStackClient().uri}/bitwarden/icons/${
shortcutRemoteUrl.host
}/icon.png`

setShortcutImg(imgUrl)
const targetApp =
shortcutInfosResult?.data?.attributes?.metadata?.target?.app
if (targetApp) {
const targetAppIconUrl = await client.getStackClient().getIconURL({
type: 'app',
slug: targetApp,
priority: 'stack'
})
setShortcutImg(targetAppIconUrl)
} else {
const shortcutRemoteUrl = new URL(
shortcutInfosResult.data.attributes.url
)

const imgUrl = `${client.getStackClient().uri}/bitwarden/icons/${
shortcutRemoteUrl.host
}/icon.png`

setShortcutImg(imgUrl)
}
setShortcutInfos({ data: shortcutInfosResult.data })
setFetchStatus('loaded')
} catch (e) {
Expand Down
37 changes: 37 additions & 0 deletions packages/cozy-client/src/hooks/useFetchShortcut.spec.jsx
Expand Up @@ -27,6 +27,32 @@ describe('useFetchShortcut', () => {
}
]
},
'io.cozy.files.shortcuts/linkToCozyApp': {
doctype: 'io.cozy.files.shortcuts',
definition: {
doctype: 'io.cozy.files.shortcuts',
id: 'io.cozy.files.shortcuts/linkToCozyApp'
},
data: [
{
type: 'io.cozy.files.shortcuts',
id: 'linkToCozyApp',
attributes: {
_id: '',
name: 'cozy.url',
dir_id: '8034db0016d0548ded99b9627e003270',
url: 'https://cozy.io',
metadata: {
extractor_version: 2,
target: {
app: 'notes'
}
}
},
meta: { rev: '1-60e1359e63fa7fa9fa000a2726d5d4c7' }
}
]
},
'io.cozy.files.shortcuts/no-found': {
doctype: 'io.cozy.files.shortcuts',
queryError: new Error('not found')
Expand Down Expand Up @@ -83,4 +109,15 @@ describe('useFetchShortcut', () => {
`${mockClient.getStackClient().uri}/bitwarden/icons/cozy.io/icon.png`
)
})

it('should return shortcutImg for the targeted application icon when available', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useFetchShortcut(mockClient, 'linkToCozyApp')
)

await waitForNextUpdate()
expect(result.current.shortcutImg).toEqual(
`${mockClient.getStackClient().uri}/registry/notes/icon`
)
})
})

0 comments on commit 0196470

Please sign in to comment.