2023-01-31 21:45:38 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { ReactNode, useEffect, useRef, useState } from "react";
|
|
|
|
import { z } from "zod";
|
2023-01-19 15:02:01 +00:00
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-01-31 21:45:38 +00:00
|
|
|
import { useTypedQuery } from "@calcom/lib/hooks/useTypedQuery";
|
2023-01-19 15:02:01 +00:00
|
|
|
import { Badge, ListItemText } from "@calcom/ui";
|
2023-01-31 21:45:38 +00:00
|
|
|
import { FiAlertCircle } from "@calcom/ui/components/icon";
|
2023-01-19 15:02:01 +00:00
|
|
|
|
2023-01-31 21:45:38 +00:00
|
|
|
type ShouldHighlight = { slug: string; shouldHighlight: true } | { shouldHighlight?: never; slug?: never };
|
|
|
|
|
|
|
|
type AppListCardProps = {
|
2023-01-19 15:02:01 +00:00
|
|
|
logo?: string;
|
|
|
|
title: string;
|
|
|
|
description: string;
|
|
|
|
actions?: ReactNode;
|
|
|
|
isDefault?: boolean;
|
2023-01-31 21:45:38 +00:00
|
|
|
isTemplate?: boolean;
|
|
|
|
invalidCredential?: boolean;
|
|
|
|
children?: ReactNode;
|
|
|
|
} & ShouldHighlight;
|
|
|
|
|
|
|
|
const schema = z.object({ hl: z.string().optional() });
|
2023-01-19 15:02:01 +00:00
|
|
|
|
|
|
|
export default function AppListCard(props: AppListCardProps) {
|
|
|
|
const { t } = useLocale();
|
2023-01-31 21:45:38 +00:00
|
|
|
const {
|
|
|
|
logo,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
actions,
|
|
|
|
isDefault,
|
|
|
|
slug,
|
|
|
|
shouldHighlight,
|
|
|
|
isTemplate,
|
|
|
|
invalidCredential,
|
|
|
|
children,
|
|
|
|
} = props;
|
|
|
|
const {
|
|
|
|
data: { hl },
|
|
|
|
} = useTypedQuery(schema);
|
|
|
|
const router = useRouter();
|
|
|
|
const [highlight, setHighlight] = useState(shouldHighlight && hl === slug);
|
|
|
|
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (shouldHighlight && highlight) {
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
setHighlight(false);
|
|
|
|
const url = new URL(window.location.href);
|
|
|
|
url.searchParams.delete("hl");
|
|
|
|
router.replace(url.pathname, undefined, { shallow: true });
|
|
|
|
}, 3000);
|
|
|
|
timeoutRef.current = timer;
|
|
|
|
}
|
|
|
|
return () => {
|
|
|
|
if (timeoutRef.current) {
|
|
|
|
clearTimeout(timeoutRef.current);
|
|
|
|
timeoutRef.current = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, []);
|
2023-01-19 15:02:01 +00:00
|
|
|
|
|
|
|
return (
|
2023-01-31 21:45:38 +00:00
|
|
|
<div className={`p-4 ${highlight ? "bg-yellow-100" : ""}`}>
|
|
|
|
<div className="flex gap-x-3">
|
2023-01-19 15:02:01 +00:00
|
|
|
{logo ? <img className="h-10 w-10" src={logo} alt={`${title} logo`} /> : null}
|
|
|
|
|
|
|
|
<div className="flex grow flex-col gap-y-1 truncate">
|
|
|
|
<div className="flex items-center gap-x-2">
|
|
|
|
<h3 className="truncate text-sm font-semibold text-gray-900">{title}</h3>
|
2023-01-31 21:45:38 +00:00
|
|
|
<div className="flex items-center gap-x-2">
|
|
|
|
{isDefault && <Badge variant="green">{t("default")}</Badge>}
|
|
|
|
{isTemplate && <Badge variant="red">Template</Badge>}
|
|
|
|
</div>
|
2023-01-19 15:02:01 +00:00
|
|
|
</div>
|
|
|
|
<ListItemText component="p">{description}</ListItemText>
|
2023-01-31 21:45:38 +00:00
|
|
|
{invalidCredential && (
|
|
|
|
<div className="flex gap-x-2 pt-2">
|
|
|
|
<FiAlertCircle className="h-8 w-8 text-red-500 sm:h-4 sm:w-4" />
|
|
|
|
<ListItemText component="p" className="whitespace-pre-wrap text-red-500">
|
|
|
|
{t("invalid_credential")}
|
|
|
|
</ListItemText>
|
|
|
|
</div>
|
|
|
|
)}
|
2023-01-19 15:02:01 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
{actions}
|
|
|
|
</div>
|
2023-01-31 21:45:38 +00:00
|
|
|
{children && <div className="w-full">{children}</div>}
|
2023-01-19 15:02:01 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|