2023-01-16 09:45:12 +00:00
|
|
|
import { getAppsConfig } from "@dashboard/config";
|
2023-01-19 11:54:57 +00:00
|
|
|
import { AppInstallationFragment, JobStatusEnum } from "@dashboard/graphql";
|
2023-01-10 10:04:30 +00:00
|
|
|
import { IntlShape } from "react-intl";
|
|
|
|
|
|
|
|
import { GetV2SaleorAppsResponse } from "./marketplace.types";
|
|
|
|
import { appsMessages } from "./messages";
|
|
|
|
import { AppLink } from "./types";
|
|
|
|
|
|
|
|
const getInstallableMarketplaceApps = (
|
|
|
|
marketplaceAppList?: GetV2SaleorAppsResponse.SaleorApp[],
|
|
|
|
) =>
|
|
|
|
marketplaceAppList?.filter(
|
2023-02-07 10:59:06 +00:00
|
|
|
app => "manifestUrl" in app || "githubForkUrl" in app,
|
2023-01-10 10:04:30 +00:00
|
|
|
) as GetV2SaleorAppsResponse.ReleasedSaleorApp[] | undefined;
|
|
|
|
|
|
|
|
const getComingSoonMarketplaceApps = (
|
|
|
|
marketplaceAppList?: GetV2SaleorAppsResponse.SaleorApp[],
|
|
|
|
) =>
|
|
|
|
marketplaceAppList?.filter(
|
|
|
|
app =>
|
|
|
|
!("manifestUrl" in app) &&
|
2023-02-07 10:59:06 +00:00
|
|
|
!("githubForkUrl" in app) &&
|
2023-01-10 10:04:30 +00:00
|
|
|
"releaseDate" in app,
|
|
|
|
) as GetV2SaleorAppsResponse.ComingSoonSaleorApp[] | undefined;
|
|
|
|
|
2023-01-19 11:54:57 +00:00
|
|
|
const getAppManifestUrl = (
|
|
|
|
marketplaceApp: GetV2SaleorAppsResponse.SaleorApp,
|
|
|
|
) => {
|
|
|
|
if ("manifestUrl" in marketplaceApp) {
|
|
|
|
return marketplaceApp.manifestUrl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const resolveInstallationOfMarketplaceApp = (
|
|
|
|
marketplaceApp: GetV2SaleorAppsResponse.SaleorApp,
|
|
|
|
appInstallations?: AppInstallationFragment[],
|
|
|
|
) => {
|
|
|
|
const manifestUrl = getAppManifestUrl(marketplaceApp);
|
|
|
|
|
|
|
|
if (manifestUrl) {
|
|
|
|
return appInstallations?.find(
|
|
|
|
appInstallation => appInstallation.manifestUrl === manifestUrl,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-10 10:04:30 +00:00
|
|
|
export const getMarketplaceAppsLists = (
|
|
|
|
isMarketplaceAvailable: boolean,
|
|
|
|
marketplaceAppList?: GetV2SaleorAppsResponse.SaleorApp[],
|
|
|
|
) => {
|
|
|
|
if (!isMarketplaceAvailable) {
|
|
|
|
return {
|
|
|
|
installableMarketplaceApps: [],
|
|
|
|
comingSoonMarketplaceApps: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2023-01-25 12:32:30 +00:00
|
|
|
installableMarketplaceApps:
|
|
|
|
getInstallableMarketplaceApps(marketplaceAppList),
|
2023-01-10 10:04:30 +00:00
|
|
|
comingSoonMarketplaceApps: getComingSoonMarketplaceApps(marketplaceAppList),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isAppInTunnel = (manifestUrl: string) =>
|
|
|
|
Boolean(
|
|
|
|
getAppsConfig().tunnelUrlKeywords.find(keyword =>
|
|
|
|
new URL(manifestUrl).host.includes(keyword),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
const prepareAppLinks = (
|
|
|
|
intl: IntlShape,
|
|
|
|
app: GetV2SaleorAppsResponse.ReleasedSaleorApp,
|
|
|
|
): AppLink[] => [
|
|
|
|
{
|
|
|
|
name: intl.formatMessage(appsMessages.repository),
|
|
|
|
url: app.repositoryUrl,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: intl.formatMessage(appsMessages.support),
|
|
|
|
url: app.supportUrl,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: intl.formatMessage(appsMessages.dataPrivacy),
|
|
|
|
url: app.privacyUrl,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2023-01-19 11:54:57 +00:00
|
|
|
interface GetAppDetailsOpts {
|
|
|
|
intl: IntlShape;
|
|
|
|
app: GetV2SaleorAppsResponse.SaleorApp;
|
|
|
|
appInstallation?: AppInstallationFragment;
|
|
|
|
navigateToAppInstallPage?: (url: string) => void;
|
2023-02-07 10:59:06 +00:00
|
|
|
navigateToGithubForkPage?: (url?: string) => void;
|
2023-01-19 11:54:57 +00:00
|
|
|
retryAppInstallation: (installationId: string) => void;
|
|
|
|
removeAppInstallation: (installationId: string) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getAppDetails = ({
|
|
|
|
intl,
|
|
|
|
app,
|
|
|
|
appInstallation,
|
|
|
|
navigateToAppInstallPage,
|
2023-02-07 10:59:06 +00:00
|
|
|
navigateToGithubForkPage,
|
2023-01-19 11:54:57 +00:00
|
|
|
retryAppInstallation,
|
|
|
|
removeAppInstallation,
|
|
|
|
}: GetAppDetailsOpts) => {
|
2023-02-07 10:59:06 +00:00
|
|
|
const isAppComingSoon = !("manifestUrl" in app);
|
|
|
|
|
2023-01-10 10:04:30 +00:00
|
|
|
const isAppInstallable = "manifestUrl" in app && !!navigateToAppInstallPage;
|
2023-02-07 10:59:06 +00:00
|
|
|
const isAppForkableOnGithub =
|
|
|
|
"githubForkUrl" in app && !!navigateToGithubForkPage;
|
2023-01-19 11:54:57 +00:00
|
|
|
const installationPending =
|
|
|
|
appInstallation && appInstallation.status === JobStatusEnum.PENDING;
|
2023-01-10 10:04:30 +00:00
|
|
|
|
|
|
|
return {
|
2023-01-19 11:54:57 +00:00
|
|
|
releaseDate:
|
|
|
|
!appInstallation && isAppComingSoon ? app.releaseDate : undefined,
|
|
|
|
installHandler:
|
|
|
|
!appInstallation && isAppInstallable
|
|
|
|
? () => navigateToAppInstallPage(app.manifestUrl)
|
|
|
|
: undefined,
|
2023-02-07 10:59:06 +00:00
|
|
|
githubForkHandler:
|
|
|
|
!appInstallation && isAppForkableOnGithub && !!app.githubForkUrl
|
|
|
|
? () => navigateToGithubForkPage(app.githubForkUrl)
|
2023-01-10 10:04:30 +00:00
|
|
|
: undefined,
|
2023-01-19 11:54:57 +00:00
|
|
|
installationPending,
|
|
|
|
retryInstallHandler:
|
|
|
|
appInstallation && !installationPending
|
|
|
|
? () => retryAppInstallation(appInstallation.id)
|
|
|
|
: undefined,
|
|
|
|
removeInstallHandler:
|
|
|
|
appInstallation && !installationPending
|
|
|
|
? () => removeAppInstallation(appInstallation.id)
|
|
|
|
: undefined,
|
2023-01-10 10:04:30 +00:00
|
|
|
links: isAppComingSoon ? [] : prepareAppLinks(intl, app),
|
|
|
|
};
|
|
|
|
};
|
2023-01-19 11:54:57 +00:00
|
|
|
|
|
|
|
export const getAppInProgressName = (
|
|
|
|
id: string,
|
|
|
|
collection?: AppInstallationFragment[],
|
|
|
|
) => collection?.find(app => app.id === id)?.appName || id;
|