2022-11-23 02:55:25 +00:00
|
|
|
import { LinkIcon, SearchIcon } from "@heroicons/react/outline";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { Dispatch, SetStateAction } from "react";
|
|
|
|
import { useState } from "react";
|
2022-05-01 20:42:35 +00:00
|
|
|
|
2022-05-30 16:23:23 +00:00
|
|
|
import classNames from "@calcom/lib/classNames";
|
2022-05-01 20:42:35 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { Alert, Button, Dialog, DialogClose, DialogContent, DialogFooter } from "@calcom/ui";
|
2022-05-01 20:42:35 +00:00
|
|
|
|
|
|
|
interface ISearchDialog {
|
|
|
|
isOpenDialog: boolean;
|
|
|
|
setIsOpenDialog: Dispatch<SetStateAction<boolean>>;
|
|
|
|
onSave: (url: string) => void;
|
|
|
|
}
|
|
|
|
|
2022-05-30 16:23:23 +00:00
|
|
|
const MODE_SEARCH = "search" as const;
|
|
|
|
const MODE_URL = "url" as const;
|
|
|
|
type Mode = typeof MODE_SEARCH | typeof MODE_URL;
|
|
|
|
|
2022-05-01 20:42:35 +00:00
|
|
|
export const SearchDialog = (props: ISearchDialog) => {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const [gifImage, setGifImage] = useState<string>("");
|
2022-05-30 16:23:23 +00:00
|
|
|
const [nextOffset, setNextOffset] = useState<number>(0);
|
2022-05-01 20:42:35 +00:00
|
|
|
const [keyword, setKeyword] = useState<string>("");
|
|
|
|
const { isOpenDialog, setIsOpenDialog } = props;
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
const [errorMessage, setErrorMessage] = useState("");
|
2022-05-30 16:23:23 +00:00
|
|
|
const [selectedMode, setSelectedMode] = useState<Mode>(MODE_SEARCH);
|
2022-05-01 20:42:35 +00:00
|
|
|
|
|
|
|
const searchGiphy = async (keyword: string, offset: number) => {
|
|
|
|
if (isLoading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setIsLoading(true);
|
|
|
|
setErrorMessage("");
|
2022-05-02 20:39:35 +00:00
|
|
|
const res = await fetch("/api/integrations/giphy/search", {
|
2022-05-01 20:42:35 +00:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
keyword,
|
|
|
|
offset,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
const json = await res.json();
|
|
|
|
if (!res.ok) {
|
|
|
|
setErrorMessage(json?.message || "Something went wrong");
|
|
|
|
} else {
|
|
|
|
setGifImage(json.image || "");
|
2022-05-30 16:23:23 +00:00
|
|
|
setNextOffset(json.nextOffset);
|
2022-05-01 20:42:35 +00:00
|
|
|
if (!json.image) {
|
|
|
|
setErrorMessage("No Result found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setIsLoading(false);
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-05-30 16:23:23 +00:00
|
|
|
const getGiphyByUrl = async (url: string) => {
|
|
|
|
if (isLoading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setIsLoading(true);
|
|
|
|
setErrorMessage("");
|
|
|
|
const res = await fetch("/api/integrations/giphy/get", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
url,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
const json = await res.json();
|
|
|
|
if (!res.ok) {
|
|
|
|
setErrorMessage(json?.message || json?.[0]?.message || "Something went wrong");
|
|
|
|
} else {
|
|
|
|
setGifImage(json.image || "");
|
|
|
|
if (!json.image) {
|
|
|
|
setErrorMessage("No Result found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setIsLoading(false);
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderTab = (Icon: any, text: string, mode: Mode) => (
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
"flex cursor-pointer items-center border-b-2 p-2 text-sm",
|
2023-04-05 18:14:46 +00:00
|
|
|
selectedMode === mode ? "border-black" : "text-subtle border-transparent"
|
2022-05-30 16:23:23 +00:00
|
|
|
)}
|
|
|
|
onClick={() => {
|
|
|
|
setKeyword("");
|
|
|
|
setGifImage("");
|
|
|
|
setSelectedMode(mode);
|
|
|
|
}}>
|
2023-01-04 07:38:45 +00:00
|
|
|
<Icon color={selectedMode === mode ? "black" : "grey"} className="h-4 w-4 ltr:mr-2 rtl:ml-2" />
|
2022-05-30 16:23:23 +00:00
|
|
|
{text}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleFormSubmit = async (event: React.SyntheticEvent) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
if (selectedMode === MODE_SEARCH) {
|
|
|
|
searchGiphy(keyword, 0);
|
|
|
|
} else if (selectedMode === MODE_URL) {
|
|
|
|
getGiphyByUrl(keyword);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-01 20:42:35 +00:00
|
|
|
return (
|
|
|
|
<Dialog open={isOpenDialog} onOpenChange={setIsOpenDialog}>
|
|
|
|
<DialogContent>
|
2023-04-05 18:14:46 +00:00
|
|
|
<h3 className="leading-16 font-cal text-emphasis text-xl" id="modal-title">
|
2022-05-30 16:23:23 +00:00
|
|
|
{t("add_gif_to_confirmation")}
|
|
|
|
</h3>
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="text-subtle mb-3 text-sm font-light">{t("find_gif_spice_confirmation")}</p>
|
2022-05-30 16:23:23 +00:00
|
|
|
<div className="flex items-center border-b border-solid">
|
|
|
|
{renderTab(SearchIcon, t("search_giphy"), MODE_SEARCH)}
|
|
|
|
{renderTab(LinkIcon, t("add_link_from_giphy"), MODE_URL)}
|
|
|
|
</div>
|
2023-01-04 07:38:45 +00:00
|
|
|
<form
|
|
|
|
className="flex w-full justify-center space-x-2 space-y-2 rtl:space-x-reverse"
|
|
|
|
onSubmit={handleFormSubmit}>
|
2022-05-30 16:23:23 +00:00
|
|
|
<div className="relative block w-full pt-2">
|
|
|
|
<input
|
|
|
|
type="text"
|
2023-04-05 18:14:46 +00:00
|
|
|
className="border-default block w-full rounded-sm shadow-sm sm:text-sm"
|
2022-05-30 16:23:23 +00:00
|
|
|
placeholder={
|
|
|
|
selectedMode === MODE_SEARCH
|
|
|
|
? t("search_giphy")
|
|
|
|
: "https://media.giphy.com/media/some-id/giphy.gif"
|
|
|
|
}
|
|
|
|
value={keyword}
|
|
|
|
onChange={(event) => {
|
|
|
|
setKeyword(event.target.value);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Button type="submit" tabIndex={-1} color="secondary" loading={isLoading}>
|
2022-05-01 20:42:35 +00:00
|
|
|
{t("search")}
|
|
|
|
</Button>
|
2022-05-30 16:23:23 +00:00
|
|
|
</form>
|
2022-05-01 20:42:35 +00:00
|
|
|
{gifImage && (
|
2023-01-04 07:38:45 +00:00
|
|
|
<div className="flex flex-col items-center space-x-2 space-y-2 pt-3 rtl:space-x-reverse">
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="bg-subtle flex w-full items-center justify-center">
|
2022-05-30 16:23:23 +00:00
|
|
|
{isLoading ? (
|
|
|
|
<div className="flex h-[200px] w-full items-center justify-center bg-gray-400 pt-3 pb-3">
|
|
|
|
<svg
|
2023-04-05 18:14:46 +00:00
|
|
|
className={classNames("mx-4 h-5 w-5 animate-spin", "text-inverted dark:text-emphasis")}
|
2022-05-30 16:23:23 +00:00
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
fill="none"
|
|
|
|
viewBox="0 0 24 24">
|
|
|
|
<circle
|
|
|
|
className="opacity-25"
|
|
|
|
cx="12"
|
|
|
|
cy="12"
|
|
|
|
r="10"
|
|
|
|
stroke="currentColor"
|
2022-07-12 21:35:12 +00:00
|
|
|
strokeWidth="4"
|
|
|
|
/>
|
2022-05-30 16:23:23 +00:00
|
|
|
<path
|
|
|
|
className="opacity-75"
|
|
|
|
fill="currentColor"
|
2022-07-12 21:35:12 +00:00
|
|
|
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
|
|
|
|
/>
|
2022-05-30 16:23:23 +00:00
|
|
|
</svg>
|
2022-05-01 20:42:35 +00:00
|
|
|
</div>
|
2022-05-30 16:23:23 +00:00
|
|
|
) : (
|
|
|
|
<img className="h-[200px] pt-3 pb-3" src={gifImage} alt={`Gif from Giphy for ${keyword}`} />
|
|
|
|
)}
|
|
|
|
</div>
|
2022-05-01 20:42:35 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{errorMessage && <Alert severity="error" title={errorMessage} className="my-4" />}
|
2022-05-30 16:23:23 +00:00
|
|
|
{gifImage && selectedMode === MODE_SEARCH && (
|
2023-01-04 07:38:45 +00:00
|
|
|
<div className="mt-4 flex items-center justify-between space-x-2 rtl:space-x-reverse">
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="text-subtle text-sm font-light">Not the perfect GIF?</div>
|
2022-05-30 16:23:23 +00:00
|
|
|
<Button
|
|
|
|
size="sm"
|
|
|
|
color="secondary"
|
|
|
|
type="button"
|
|
|
|
loading={isLoading}
|
|
|
|
onClick={() => searchGiphy(keyword, nextOffset)}>
|
|
|
|
Shuffle
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-05-01 20:42:35 +00:00
|
|
|
<DialogFooter>
|
|
|
|
<DialogClose
|
2022-11-28 19:14:38 +00:00
|
|
|
color="minimal"
|
|
|
|
tabIndex={-1}
|
2022-05-01 20:42:35 +00:00
|
|
|
onClick={() => {
|
|
|
|
props.setIsOpenDialog(false);
|
2022-11-28 19:14:38 +00:00
|
|
|
}}>
|
|
|
|
{t("cancel")}
|
2022-05-01 20:42:35 +00:00
|
|
|
</DialogClose>
|
|
|
|
|
|
|
|
<Button
|
|
|
|
type="button"
|
2022-05-30 16:23:23 +00:00
|
|
|
disabled={!gifImage}
|
2022-05-01 20:42:35 +00:00
|
|
|
onClick={() => {
|
|
|
|
props.setIsOpenDialog(false);
|
|
|
|
props.onSave(gifImage);
|
2022-05-30 16:23:23 +00:00
|
|
|
setNextOffset(0);
|
2022-05-01 20:42:35 +00:00
|
|
|
setGifImage("");
|
|
|
|
setKeyword("");
|
|
|
|
return false;
|
|
|
|
}}>
|
2022-05-30 16:23:23 +00:00
|
|
|
{t("add_gif")}
|
2022-05-01 20:42:35 +00:00
|
|
|
</Button>
|
|
|
|
</DialogFooter>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|