import React, { Fragment } from "react"; import { useMutation } from "react-query"; import { QueryCell } from "@lib/QueryCell"; import showToast from "@lib/notification"; import { trpc } from "@lib/trpc"; import { List } from "@components/List"; import { ShellSubHeading } from "@components/Shell"; import { Alert } from "@components/ui/Alert"; import Button from "@components/ui/Button"; import Switch from "@components/ui/Switch"; import ConnectIntegration from "./ConnectIntegrations"; import DisconnectIntegration from "./DisconnectIntegration"; import IntegrationListItem from "./IntegrationListItem"; import SubHeadingTitleWithConnections from "./SubHeadingTitleWithConnections"; type Props = { onChanged: () => unknown | Promise; }; function CalendarSwitch(props: { type: string; externalId: string; title: string; defaultSelected: boolean; }) { const utils = trpc.useContext(); const mutation = useMutation< unknown, unknown, { isOn: boolean; } >( async ({ isOn }) => { const body = { integration: props.type, externalId: props.externalId, }; if (isOn) { const res = await fetch("/api/availability/calendar", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(body), }); if (!res.ok) { throw new Error("Something went wrong"); } } else { const res = await fetch("/api/availability/calendar", { method: "DELETE", headers: { "Content-Type": "application/json", }, body: JSON.stringify(body), }); if (!res.ok) { throw new Error("Something went wrong"); } } }, { async onSettled() { await utils.invalidateQueries(["viewer.integrations"]); }, onError() { showToast(`Something went wrong when toggling "${props.title}""`, "error"); }, } ); return (
{ mutation.mutate({ isOn }); }} />
); } function ConnectedCalendarsList(props: Props) { const query = trpc.useQuery(["viewer.connectedCalendars"], { suspense: true }); return ( null} success={({ data }) => ( {data.map((item) => ( {item.calendars ? ( ( )} onOpenChange={props.onChanged} /> }>
    {item.calendars.map((cal) => ( ))}
) : ( ( )} onOpenChange={() => props.onChanged()} /> } /> )}
))}
)} /> ); } function CalendarList(props: Props) { const query = trpc.useQuery(["viewer.integrations"]); return ( ( {data.calendar.items.map((item) => ( ( )} onOpenChange={() => props.onChanged()} /> } /> ))} )} /> ); } export function CalendarListContainer(props: { heading?: false }) { const { heading = true } = props; const utils = trpc.useContext(); const onChanged = () => Promise.allSettled([ utils.invalidateQueries(["viewer.integrations"]), utils.invalidateQueries(["viewer.connectedCalendars"]), ]); const query = trpc.useQuery(["viewer.connectedCalendars"]); return ( <> {heading && ( } subtitle={ <> Configure how your links integrate with your calendars.
You can override these settings on a per event basis. } /> )} {!!query.data?.length && ( } /> )} ); }