|
| 1 | +<!-- |
| 2 | + - SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 3 | + - SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | +--> |
| 5 | + |
| 6 | +<script setup lang="ts"> |
| 7 | +/** |
| 8 | + * This component either shows a native link to the installed app or external size |
| 9 | + * or a router link to the appstore page of the app if not installed |
| 10 | + */ |
| 11 | +
|
| 12 | +import type { RouterLinkProps } from 'vue-router' |
| 13 | +import type { INavigationEntry } from '../../../../core/src/types/navigation.d.ts' |
| 14 | +
|
| 15 | +import { loadState } from '@nextcloud/initial-state' |
| 16 | +import { generateUrl } from '@nextcloud/router' |
| 17 | +import { ref, watchEffect } from 'vue' |
| 18 | +import { RouterLink, useRoute } from 'vue-router' |
| 19 | +
|
| 20 | +const props = defineProps<{ |
| 21 | + href: string |
| 22 | +}>() |
| 23 | +
|
| 24 | +const route = useRoute() |
| 25 | +const knownRoutes = Object.fromEntries(loadState<INavigationEntry[]>('core', 'apps').map((app) => [app.app ?? app.id, app.href])) |
| 26 | +
|
| 27 | +const routerProps = ref<RouterLinkProps>() |
| 28 | +const linkProps = ref<Record<string, string>>() |
| 29 | +
|
| 30 | +watchEffect(() => { |
| 31 | + const match = props.href.match(/^app:(\/\/)?([^/]+)(\/.+)?$/) |
| 32 | + routerProps.value = undefined |
| 33 | + linkProps.value = undefined |
| 34 | +
|
| 35 | + // not an app url |
| 36 | + if (match === null) { |
| 37 | + linkProps.value = { |
| 38 | + href: props.href, |
| 39 | + target: '_blank', |
| 40 | + rel: 'noreferrer noopener', |
| 41 | + } |
| 42 | + return |
| 43 | + } |
| 44 | +
|
| 45 | + const appId = match[2]! |
| 46 | + // Check if specific route was requested |
| 47 | + if (match[3]) { |
| 48 | + // we do no know anything about app internal path so we only allow generic app paths |
| 49 | + linkProps.value = { |
| 50 | + href: generateUrl(`/apps/${appId}${match[3]}`), |
| 51 | + } |
| 52 | + return |
| 53 | + } |
| 54 | +
|
| 55 | + // If we know any route for that app we open it |
| 56 | + if (appId in knownRoutes) { |
| 57 | + linkProps.value = { |
| 58 | + href: knownRoutes[appId]!, |
| 59 | + } |
| 60 | + return |
| 61 | + } |
| 62 | +
|
| 63 | + // Fallback to show the app store entry |
| 64 | + routerProps.value = { |
| 65 | + to: { |
| 66 | + name: 'apps-details', |
| 67 | + params: { |
| 68 | + category: route.params?.category ?? 'discover', |
| 69 | + id: appId, |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | +}) |
| 74 | +</script> |
| 75 | + |
| 76 | +<template> |
| 77 | + <a v-if="linkProps" v-bind="linkProps"> |
| 78 | + <slot /> |
| 79 | + </a> |
| 80 | + <RouterLink v-else-if="routerProps" v-bind="routerProps"> |
| 81 | + <slot /> |
| 82 | + </RouterLink> |
| 83 | +</template> |
0 commit comments