From 8542ba3af3c6245b0b39599e1be351c7d5169eb8 Mon Sep 17 00:00:00 2001 From: Brett Sun Date: Wed, 17 Apr 2019 19:22:35 +0200 Subject: [PATCH] App Center: correctly use repo's latest version for baseUrl (#729) --- src/Wrapper.js | 1 - src/apps/AppCenter/AppCenter.js | 3 +- src/aragonjs-wrapper.js | 35 +----------- .../Upgrade/UpgradeOrganizationPanel.js | 22 ++++---- src/url-utils.js | 54 +++++++++++++++++++ 5 files changed, 67 insertions(+), 48 deletions(-) create mode 100644 src/url-utils.js diff --git a/src/Wrapper.js b/src/Wrapper.js index afe26322a..81d45b286 100644 --- a/src/Wrapper.js +++ b/src/Wrapper.js @@ -291,7 +291,6 @@ class Wrapper extends React.PureComponent { /> { - // Support overriding app URLs, see network-config.js - if (appLocator[app.appId]) { - return appLocator[app.appId] - } - if (!app.content) { - return '' - } - - const { provider, location } = app.content - if (provider === 'ipfs') { - return `${gateway}/${location}/` - } - if (provider === 'http') { - return /^https?:\/\//.test(location) - ? appendTrailingSlash(location) - : `http://${location}/` - } - return '' -} - const applyAppOverrides = apps => apps.map(app => ({ ...app, ...(appOverrides[app.appId] || {}) })) diff --git a/src/components/Upgrade/UpgradeOrganizationPanel.js b/src/components/Upgrade/UpgradeOrganizationPanel.js index d78113508..ca0a0b07b 100644 --- a/src/components/Upgrade/UpgradeOrganizationPanel.js +++ b/src/components/Upgrade/UpgradeOrganizationPanel.js @@ -10,25 +10,22 @@ import { SidePanelSplit, blockExplorerUrl, } from '@aragon/ui' -import { AppsListType, ReposListType } from '../../prop-types' +import { ReposListType } from '../../prop-types' import { TextLabel } from '../../components/TextStyles' import AppIcon from '../../components/AppIcon/AppIcon' import { network } from '../../environment' import { KNOWN_ICONS } from '../../repo-utils' import { getAppPath } from '../../routing' +import { repoBaseUrl } from '../../url-utils' import { GU } from '../../utils' const VERSION = '0.7 Bella' -function getBaseUrlFromAppId(appId, apps) { - const app = apps.find(app => app.appId === appId) - return (app && app.baseUrl) || null -} - -function getAppVersionData({ content, version }, apps) { +function getAppVersionData(repo) { + const { content, version } = repo return { appId: content.appId, - baseUrl: getBaseUrlFromAppId(content.appId, apps), + baseUrl: repoBaseUrl(repo), contractAddress: content.contractAddress, icons: content.icons, knownIcon: KNOWN_ICONS.has(content.appId) @@ -40,19 +37,19 @@ function getAppVersionData({ content, version }, apps) { } const UpgradeOrganizationPanel = React.memo( - ({ apps = [], repos = [], opened, onClose, dao }) => { + ({ repos = [], opened, onClose, dao }) => { const sourceUrl = 'https://github.com/aragon/aragon-apps' const [currentVersions, newVersions] = useMemo( () => repos.reduce( (results, repo) => [ - [...results[0], getAppVersionData(repo.currentVersion, apps)], - [...results[1], getAppVersionData(repo.latestVersion, apps)], + [...results[0], getAppVersionData(repo.currentVersion)], + [...results[1], getAppVersionData(repo.latestVersion)], ], [[], []] ), - [repos, apps] + [repos] ) return ( @@ -136,7 +133,6 @@ const UpgradeOrganizationPanel = React.memo( UpgradeOrganizationPanel.propTypes = { opened: PropTypes.bool, onClose: PropTypes.func.isRequired, - apps: AppsListType, repos: ReposListType, dao: PropTypes.string.isRequired, } diff --git a/src/url-utils.js b/src/url-utils.js new file mode 100644 index 000000000..d546930af --- /dev/null +++ b/src/url-utils.js @@ -0,0 +1,54 @@ +import { appLocator, ipfsDefaultConf } from './environment' +import { appendTrailingSlash } from './utils' + +/* + * Supported locations: + * ipfs:{IPFS_HASH} + * http:{HOST} + * http:{HOST}:{PORT} + * http:{HOST}:{PORT}/{PATH} + * http:http(s)://{HOST} + * http:http(s)://{HOST}:{PORT} + * http:http(s)://{HOST}:{PORT}/{PATH} + */ +function contentBaseUrl(content, gateway) { + if (!content) { + return '' + } + + const { provider, location } = content + if (provider === 'ipfs') { + return `${gateway}/${location}/` + } + if (provider === 'http') { + return /^https?:\/\//.test(location) + ? appendTrailingSlash(location) + : `http://${location}/` + } + return '' +} + +export function appBaseUrl(app, gateway = ipfsDefaultConf.gateway) { + // Support overriding app URLs, see network-config.js + if (appLocator[app.appId]) { + return appLocator[app.appId] + } + + return contentBaseUrl(app.content, gateway) +} + +export function repoBaseUrl(repo, gateway = ipfsDefaultConf.gateway) { + const { appId, latestVersion = {} } = repo + + // Support overriding app URLs, see network-config.js + if (appLocator[appId]) { + return appLocator[appId] + } + + return contentBaseUrl( + // The latest version's content is the artifact.json and manifest.json, so we need to + // look up content again for the actual content location + latestVersion.content && latestVersion.content.content, + gateway + ) +}