2022-11-13 15:51:31 +00:00
|
|
|
import { useRouter } from "next/router";
|
2022-11-30 20:51:44 +00:00
|
|
|
import { useEffect, useState } from "react";
|
2022-09-05 21:22:28 +00:00
|
|
|
|
|
|
|
import useAddAppMutation from "@calcom/app-store/_utils/useAddAppMutation";
|
|
|
|
import { InstallAppButton } from "@calcom/app-store/components";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { AppFrontendPayload as App } from "@calcom/types/App";
|
2022-12-20 22:15:06 +00:00
|
|
|
import type { CredentialFrontendPayload as Credential } from "@calcom/types/Credential";
|
2023-01-26 22:51:03 +00:00
|
|
|
|
|
|
|
import { Button } from "../button";
|
|
|
|
import { FiPlus } from "../icon";
|
|
|
|
import { showToast } from "../toast";
|
2022-09-05 21:22:28 +00:00
|
|
|
|
|
|
|
interface AppCardProps {
|
|
|
|
app: App;
|
|
|
|
credentials?: Credential[];
|
2022-11-30 20:51:44 +00:00
|
|
|
searchText?: string;
|
2022-09-05 21:22:28 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 11:00:01 +00:00
|
|
|
export function AppCard({ app, credentials, searchText }: AppCardProps) {
|
2022-09-05 21:22:28 +00:00
|
|
|
const { t } = useLocale();
|
2022-11-13 15:51:31 +00:00
|
|
|
const router = useRouter();
|
2022-09-05 21:22:28 +00:00
|
|
|
const mutation = useAddAppMutation(null, {
|
2022-11-17 21:38:34 +00:00
|
|
|
onSuccess: (data) => {
|
2022-11-13 15:51:31 +00:00
|
|
|
// Refresh SSR page content without actual reload
|
|
|
|
router.replace(router.asPath);
|
2022-12-05 13:13:01 +00:00
|
|
|
if (data?.setupPending) return;
|
2022-09-05 21:22:28 +00:00
|
|
|
showToast(t("app_successfully_installed"), "success");
|
|
|
|
},
|
|
|
|
onError: (error) => {
|
|
|
|
if (error instanceof Error) showToast(error.message || t("app_could_not_be_installed"), "error");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const allowedMultipleInstalls = app.categories && app.categories.indexOf("calendar") > -1;
|
|
|
|
const appAdded = (credentials && credentials.length) || 0;
|
2022-11-30 20:51:44 +00:00
|
|
|
const [searchTextIndex, setSearchTextIndex] = useState<number | undefined>(undefined);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setSearchTextIndex(searchText ? app.name.toLowerCase().indexOf(searchText.toLowerCase()) : undefined);
|
|
|
|
}, [app.name, searchText]);
|
2022-09-05 21:22:28 +00:00
|
|
|
|
|
|
|
return (
|
2023-01-14 19:11:28 +00:00
|
|
|
<div className="relative flex h-64 flex-col rounded-md border border-gray-200 p-5">
|
2022-09-05 21:22:28 +00:00
|
|
|
<div className="flex">
|
|
|
|
<img src={app.logo} alt={app.name + " Logo"} className="mb-4 h-12 w-12 rounded-sm" />
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center">
|
2022-11-30 20:51:44 +00:00
|
|
|
<h3 className="font-medium">
|
|
|
|
{searchTextIndex != undefined && searchText ? (
|
|
|
|
<>
|
|
|
|
{app.name.substring(0, searchTextIndex)}
|
|
|
|
<span className="bg-yellow-300">
|
|
|
|
{app.name.substring(searchTextIndex, searchTextIndex + searchText.length)}
|
|
|
|
</span>
|
|
|
|
{app.name.substring(searchTextIndex + searchText.length)}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
app.name
|
|
|
|
)}
|
|
|
|
</h3>
|
2022-09-05 21:22:28 +00:00
|
|
|
</div>
|
|
|
|
{/* TODO: add reviews <div className="flex text-sm text-gray-800">
|
|
|
|
<span>{props.rating} stars</span> <StarIcon className="ml-1 mt-0.5 h-4 w-4 text-yellow-600" />
|
|
|
|
<span className="pl-1 text-gray-500">{props.reviews} reviews</span>
|
|
|
|
</div> */}
|
|
|
|
<p
|
|
|
|
className="mt-2 flex-grow text-sm text-gray-500"
|
|
|
|
style={{
|
|
|
|
overflow: "hidden",
|
|
|
|
display: "-webkit-box",
|
|
|
|
WebkitBoxOrient: "vertical",
|
|
|
|
WebkitLineClamp: "3",
|
|
|
|
}}>
|
|
|
|
{app.description}
|
|
|
|
</p>
|
2023-01-18 22:30:25 +00:00
|
|
|
|
2022-09-26 14:12:54 +00:00
|
|
|
<div className="mt-5 flex max-w-full flex-row justify-between gap-2">
|
2022-09-15 11:52:52 +00:00
|
|
|
<Button
|
|
|
|
color="secondary"
|
|
|
|
className="flex w-32 flex-grow justify-center"
|
|
|
|
href={`/apps/${app.slug}`}
|
|
|
|
data-testid={`app-store-app-card-${app.slug}`}>
|
2022-09-05 21:22:28 +00:00
|
|
|
{t("details")}
|
|
|
|
</Button>
|
|
|
|
{app.isGlobal || (credentials && credentials.length > 0 && allowedMultipleInstalls)
|
|
|
|
? !app.isGlobal && (
|
|
|
|
<InstallAppButton
|
|
|
|
type={app.type}
|
|
|
|
isProOnly={app.isProOnly}
|
2022-09-15 19:53:09 +00:00
|
|
|
wrapperClassName="[@media(max-width:260px)]:w-full"
|
2022-09-05 21:22:28 +00:00
|
|
|
render={({ useDefaultComponent, ...props }) => {
|
|
|
|
if (useDefaultComponent) {
|
|
|
|
props = {
|
2022-11-23 02:55:25 +00:00
|
|
|
...props,
|
2022-09-05 21:22:28 +00:00
|
|
|
onClick: () => {
|
2022-09-15 19:53:09 +00:00
|
|
|
mutation.mutate({ type: app.type, variant: app.variant, slug: app.slug });
|
2022-09-05 21:22:28 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return (
|
2022-09-15 19:53:09 +00:00
|
|
|
<Button
|
|
|
|
color="secondary"
|
|
|
|
className="[@media(max-width:260px)]:w-full [@media(max-width:260px)]:justify-center"
|
2023-01-23 23:08:01 +00:00
|
|
|
StartIcon={FiPlus}
|
2022-09-15 19:53:09 +00:00
|
|
|
{...props}>
|
2022-09-05 21:22:28 +00:00
|
|
|
{t("install")}
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
: credentials &&
|
|
|
|
credentials.length === 0 && (
|
|
|
|
<InstallAppButton
|
|
|
|
type={app.type}
|
|
|
|
isProOnly={app.isProOnly}
|
2022-09-15 19:53:09 +00:00
|
|
|
wrapperClassName="[@media(max-width:260px)]:w-full"
|
2022-09-05 21:22:28 +00:00
|
|
|
render={({ useDefaultComponent, ...props }) => {
|
|
|
|
if (useDefaultComponent) {
|
|
|
|
props = {
|
2022-11-23 02:55:25 +00:00
|
|
|
...props,
|
2022-09-05 21:22:28 +00:00
|
|
|
onClick: () => {
|
2022-09-15 19:53:09 +00:00
|
|
|
mutation.mutate({ type: app.type, variant: app.variant, slug: app.slug });
|
2022-09-05 21:22:28 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Button
|
2023-01-23 23:08:01 +00:00
|
|
|
StartIcon={FiPlus}
|
2022-09-05 21:22:28 +00:00
|
|
|
color="secondary"
|
2022-09-15 19:53:09 +00:00
|
|
|
className="[@media(max-width:260px)]:w-full [@media(max-width:260px)]:justify-center"
|
2022-09-05 21:22:28 +00:00
|
|
|
data-testid="install-app-button"
|
|
|
|
{...props}>
|
|
|
|
{t("install")}
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div className="max-w-44 absolute right-0 mr-4 flex flex-wrap justify-end gap-1">
|
|
|
|
{appAdded > 0 && (
|
|
|
|
<span className="rounded-md bg-green-100 px-2 py-1 text-sm font-normal text-green-800">
|
|
|
|
{t("installed", { count: appAdded })}
|
|
|
|
</span>
|
|
|
|
)}
|
2023-01-18 22:30:25 +00:00
|
|
|
{app.isTemplate && (
|
|
|
|
<span className="rounded-md bg-red-100 px-2 py-1 text-sm font-normal text-red-800">Template</span>
|
|
|
|
)}
|
|
|
|
|
2023-02-17 09:11:03 +00:00
|
|
|
{(app.isDefault || (!app.isDefault && app.isGlobal)) && (
|
2022-09-05 21:22:28 +00:00
|
|
|
<span className="flex items-center rounded-md bg-gray-100 px-2 py-1 text-sm font-normal text-gray-800">
|
|
|
|
{t("default")}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|