feat: Troubleshooter atom (#27497)

This commit is contained in:
Rajiv Sahal authored and GitHub committed 2026-02-06 17:34:58 +02:00
1 parent a71d62dc33
commit 8a17ebc2ef
31 files changed
+821 -305

No files matched your search

@@ -0,0 +1,174 @@
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { Badge } from "@calcom/ui/components/badge";
import { Button } from "@calcom/ui/components/button";
import { Switch } from "@calcom/ui/components/form";
import { TroubleshooterListItemContainer } from "./TroubleshooterListItemContainer";
interface CalendarToggleItemProps {
title: string;
subtitle: string;
colorDot?: string;
status: "connected" | "not_found";
calendars?: {
active?: boolean;
name?: string;
}[];
}
function CalendarToggleItem(props: CalendarToggleItemProps) {
const badgeStatus = props.status === "connected" ? "green" : "orange";
const badgeText = props.status === "connected" ? "Connected" : "Not found";
return (
<TroubleshooterListItemContainer
title={props.title}
subtitle={props.subtitle}
prefixSlot={
<>
<div
className="h-4 w-4 self-center rounded-[4px]"
style={{
backgroundColor: props.colorDot,
}}
/>
</>
}
suffixSlot={
<div>
<Badge variant={badgeStatus} withDot size="sm">
{badgeText}
</Badge>
</div>
}
>
<div className="[&>*]:text-emphasis flex flex-col gap-3">
{props.calendars?.map((calendar) => {
return (
<Switch
key={calendar.name}
checked={calendar.active}
label={calendar.name}
disabled
/>
);
})}
</div>
</TroubleshooterListItemContainer>
);
}
function EmptyCalendarToggleItem({
installCalendarAction,
}: {
installCalendarAction?: { href: string } | { onClick: () => void };
}) {
const { t } = useLocale();
return (
<TroubleshooterListItemContainer
title={t("installed", { count: 0 })}
subtitle={t("please_install_a_calendar")}
prefixSlot={
<>
<div className="h-4 w-4 self-center rounded-[4px] bg-blue-500" />
</>
}
suffixSlot={
<div>
<Badge variant="orange" withDot size="sm">
{t("unavailable")}
</Badge>
</div>
}
>
{installCalendarAction && (
<div className="flex flex-col gap-3">
<Button
color="secondary"
className="justify-center gap-2"
{...installCalendarAction}
>
{t("install_calendar")}
</Button>
</div>
)}
</TroubleshooterListItemContainer>
);
}
interface ConnectedCalendarItem {
credentialId: number;
integration: {
name: string;
};
error?: {
message: string;
};
calendars?: {
primary: boolean | null;
isSelected: boolean;
name?: string;
}[];
}
interface CalendarToggleContainerComponentProps {
connectedCalendars: ConnectedCalendarItem[];
isLoading: boolean;
manageCalendarsAction?: { href: string } | { onClick: () => void };
installCalendarAction?: { href: string } | { onClick: () => void };
}
export function CalendarToggleContainerComponent({
connectedCalendars,
isLoading,
manageCalendarsAction,
installCalendarAction,
}: CalendarToggleContainerComponentProps): JSX.Element {
const { t } = useLocale();
const hasConnectedCalendars = connectedCalendars.length > 0;
return (
<div className="flex flex-col stack-y-3">
<p className="text-sm font-medium leading-none">
{t("calendars_were_checking_for_conflicts")}
</p>
{hasConnectedCalendars && !isLoading ? (
<>
{connectedCalendars.map((calendar) => {
const foundPrimary = calendar.calendars?.find(
(item) => item.primary
);
// Will be used when getAvailbility is modified to use externalId instead of appId for source.
// const color = SELECTION_COLORS[idx] || "#000000";
// // Add calendar to color map using externalId (what we use on the backend to determine source)
// addToColorMap(foundPrimary?.externalId, color);
return (
<CalendarToggleItem
key={calendar.credentialId}
title={calendar.integration.name}
colorDot="#000000"
subtitle={foundPrimary?.name ?? t("nameless_calendar")}
status={calendar.error ? "not_found" : "connected"}
calendars={calendar.calendars?.map((item) => {
return {
active: item.isSelected,
name: item.name,
};
})}
/>
);
})}
{manageCalendarsAction && (
<Button
color="secondary"
className="justify-center gap-2"
{...manageCalendarsAction}
>
{t("manage_calendars")}
</Button>
)}
</>
) : (
<EmptyCalendarToggleItem installCalendarAction={installCalendarAction} />
)}
</div>
);
}
@@ -0,0 +1,33 @@
import { Label } from "@calcom/ui/components/form";
import type React from "react";
import { TroubleshooterListItemHeader } from "./TroubleshooterListItemContainer";
import { useLocale } from "@calcom/lib/hooks/useLocale";
interface ScheduleItem {
id: number;
name: string;
}
interface EventScheduleItemComponentProps {
schedule: ScheduleItem | null;
suffixSlot?: React.ReactNode;
}
export function EventScheduleItemComponent({
schedule,
suffixSlot,
}: EventScheduleItemComponentProps): JSX.Element {
const { t } = useLocale();
return (
<div>
<Label> {t("availability_schedule")}</Label>
<TroubleshooterListItemHeader
className="group rounded-md border-b"
prefixSlot={<div className="w-4 rounded-[4px] bg-black" />}
title={schedule?.name ?? t("loading")}
suffixSlot={suffixSlot}
/>
</div>
);
}
@@ -0,0 +1,115 @@
import { getQueryParam } from "@calcom/features/bookings/Booker/utils/query-param";
import { useTroubleshooterStore } from "@calcom/features/troubleshooter/store";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { SelectField } from "@calcom/ui/components/form";
import { startTransition, useEffect, useMemo } from "react";
import { shallow } from "zustand/shallow";
interface EventTypeItem {
id: number;
title: string;
slug: string;
length: number;
username?: string | null;
team?: {
id: number;
name: string;
} | null;
}
interface EventTypeSelectComponentProps {
eventTypes: EventTypeItem[];
isPending?: boolean;
}
export function EventTypeSelectComponent({
eventTypes,
isPending,
}: EventTypeSelectComponentProps): JSX.Element {
const { t } = useLocale();
const { event: selectedEventType, setEvent: setSelectedEventType } =
useTroubleshooterStore(
(state) => ({
event: state.event,
setEvent: state.setEvent,
}),
shallow
);
const options = useMemo(() => {
if (!eventTypes) return [];
return eventTypes.map((e) => ({
label: e.title,
value: e.id.toString(),
id: e.id,
duration: e.length,
}));
}, [eventTypes]);
// Initialize event type from query param or default to first event
useEffect(() => {
if (!eventTypes || eventTypes.length === 0) return;
const selectedEventIdParam = getQueryParam("eventTypeId");
const eventTypeId = selectedEventIdParam
? parseInt(selectedEventIdParam, 10)
: null;
// If we already have a selected event that matches the query param, don't do anything
if (selectedEventType?.id === eventTypeId) return;
// If there's a query param, try to find and set that event
if (eventTypeId && !isNaN(eventTypeId)) {
startTransition(() => {
const foundEventType = eventTypes.find((et) => et.id === eventTypeId);
if (foundEventType) {
setSelectedEventType({
id: foundEventType.id,
slug: foundEventType.slug,
duration: foundEventType.length,
teamId: foundEventType.team?.id ?? null,
username: foundEventType.username ?? null,
});
return;
}
});
}
// If no event is selected and no valid query param, default to first event
if (!selectedEventType && !eventTypeId) {
const firstEvent = eventTypes[0];
setSelectedEventType({
id: firstEvent.id,
slug: firstEvent.slug,
duration: firstEvent.length,
teamId: firstEvent.team?.id ?? null,
username: firstEvent.username ?? null,
});
}
}, [eventTypes, selectedEventType, setSelectedEventType]);
return (
<SelectField
label={t("event_type")}
options={options}
isDisabled={isPending || options.length === 0}
value={
options.find((option) => option.id === selectedEventType?.id) ||
options[0]
}
onChange={(option) => {
if (!option) return;
const foundEventType = eventTypes?.find((et) => et.id === option.id);
if (foundEventType) {
setSelectedEventType({
id: foundEventType.id,
slug: foundEventType.slug,
duration: foundEventType.length,
teamId: foundEventType.team?.id ?? null,
username: foundEventType.username ?? null,
});
}
}}
/>
);
}
@@ -0,0 +1,42 @@
import type { PropsWithChildren } from "react";
import classNames from "@calcom/ui/classNames";
interface TroubleshooterListItemContainerProps {
title: string;
subtitle?: string;
suffixSlot?: React.ReactNode;
prefixSlot?: React.ReactNode;
className?: string;
}
export function TroubleshooterListItemHeader({
prefixSlot,
title,
subtitle,
suffixSlot,
className,
}: TroubleshooterListItemContainerProps) {
return (
<div className={classNames("border-subtle flex max-w-full gap-3 border border-b-0 px-4 py-2", className)}>
{prefixSlot}
<div className="flex h-full max-w-full flex-1 flex-col flex-nowrap truncate text-sm leading-4">
<p className="font-medium">{title}</p>
{subtitle && <p className="font-normal">{subtitle}</p>}
</div>
{suffixSlot}
</div>
);
}
export function TroubleshooterListItemContainer({
children,
...rest
}: PropsWithChildren<TroubleshooterListItemContainerProps>) {
return (
<div className="[&>*:first-child]:rounded-t-md ">
<TroubleshooterListItemHeader {...rest} />
<div className="border-subtle flex flex-col stack-y-3 rounded-b-md border p-4">{children}</div>
</div>
);
}
+14 -4
View File
@@ -11,6 +11,7 @@ import { updateQueryParam, getQueryParam, removeQueryParam } from "../bookings/B
*/
type StoreInitializeType = {
month: string | null;
isPlatform?: boolean;
};
type EventType = {
@@ -18,9 +19,11 @@ type EventType = {
slug: string;
duration: number;
teamId?: number | null;
username?: string | null;
};
export type TroubleshooterStore = {
isPlatform: boolean;
event: EventType | null;
setEvent: (eventSlug: EventType) => void;
month: string | null;
@@ -41,6 +44,7 @@ export type TroubleshooterStore = {
* See comments in interface above for more information on it's specific values.
*/
export const useTroubleshooterStore = create<TroubleshooterStore>((set, get) => ({
isPlatform: false,
selectedDate: getQueryParam("date") || null,
setSelectedDate: (selectedDate: string | null) => {
// unset selected date
@@ -77,7 +81,9 @@ export const useTroubleshooterStore = create<TroubleshooterStore>((set, get) =>
event: null,
setEvent: (event: EventType) => {
set({ event });
updateQueryParam("eventTypeId", event.id.toString());
if (!get().isPlatform) {
updateQueryParam("eventTypeId", event.id.toString());
}
},
month: getQueryParam("month") || getQueryParam("date") || dayjs().format("YYYY-MM"),
setMonth: (month: string | null) => {
@@ -85,7 +91,10 @@ export const useTroubleshooterStore = create<TroubleshooterStore>((set, get) =>
updateQueryParam("month", month ?? "");
get().setSelectedDate(null);
},
initialize: ({ month }: StoreInitializeType) => {
initialize: ({ month, isPlatform }: StoreInitializeType) => {
if (isPlatform) {
set({ isPlatform: true });
}
if (month) {
set({ month });
updateQueryParam("month", month);
@@ -101,11 +110,12 @@ export const useTroubleshooterStore = create<TroubleshooterStore>((set, get) =>
},
}));
export const useInitalizeTroubleshooterStore = ({ month }: StoreInitializeType) => {
export const useInitalizeTroubleshooterStore = ({ month, isPlatform }: StoreInitializeType) => {
const initializeStore = useTroubleshooterStore((state) => state.initialize);
useEffect(() => {
initializeStore({
month,
isPlatform,
});
}, [initializeStore, month]);
}, [initializeStore, month, isPlatform]);
};