import { UserButton, Waitlist, useAuth } from "@clerk/react";
import { AuthRelayWriteScope } from "@t3tools/contracts";
import { CloudIcon, RefreshCwIcon, SmartphoneIcon } from "lucide-react";
import { useEffect, useState } from "react";
import { updatePrimaryCloudPreferences } from "../../cloud/linkEnvironment";
import { hasCloudPublicConfig } from "../../cloud/publicConfig";
import { useManagedRelayDevices } from "../../cloud/managedRelayState";
import { usePrimaryCloudLinkState } from "../../cloud/primaryCloudLinkState";
import { isElectron } from "../../env";
import { usePrimarySessionState } from "../../environments/primary";
import { webRuntime } from "../../lib/runtime";
import { cn } from "../../lib/utils";
import { DesktopClerkWaitlist } from "../clerk/DesktopClerkWaitlist";
import { Button } from "../ui/button";
import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from "../ui/empty";
import { Skeleton } from "../ui/skeleton";
import { Switch } from "../ui/switch";
import { toastManager } from "../ui/toast";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import { SettingsPageContainer, SettingsRow, SettingsSection } from "./settingsLayout";
const NOTIFICATION_DEVICE_SKELETON_ROWS = ["primary", "secondary"] as const;
function NotificationDevicesSkeleton() {
return NOTIFICATION_DEVICE_SKELETON_ROWS.map((row) => (
));
}
function EmptyNotificationDevices() {
return (
No notification devices
Sign in on the mobile app to register a device for account-level notifications.
);
}
function cloudErrorMessage(error: unknown, fallback: string): string {
return error instanceof Error ? error.message : fallback;
}
export function CloudSettingsPanel() {
if (!hasCloudPublicConfig()) return null;
return ;
}
function ConfiguredCloudSettingsPanel() {
const { isLoaded, isSignedIn } = useAuth();
if (!isLoaded) {
return null;
}
return isSignedIn ? : ;
}
function CloudWaitlistPanel() {
return (
{isElectron ? : }
);
}
function CloudSettingsPanelInner() {
const primaryLinkState = usePrimaryCloudLinkState();
const primarySessionState = usePrimarySessionState();
const devicesState = useManagedRelayDevices();
const [isUpdatingPreference, setIsUpdatingPreference] = useState(false);
const devices = devicesState.data ?? [];
const canManageRelay =
primarySessionState.data?.authenticated === true &&
Boolean(primarySessionState.data.scopes?.includes(AuthRelayWriteScope));
useEffect(() => {
if (devicesState.error) {
toastManager.add({
type: "error",
title: "Cloud devices unavailable",
description: devicesState.error,
});
}
}, [devicesState.error]);
const updatePublishAgentActivity = async (enabled: boolean) => {
setIsUpdatingPreference(true);
try {
await webRuntime.runPromise(updatePrimaryCloudPreferences({ publishAgentActivity: enabled }));
primaryLinkState.refresh();
toastManager.add({
type: "success",
title: enabled ? "Agent activity enabled" : "Agent activity disabled",
description: enabled
? "This environment can publish agent activity to your notification devices."
: "This environment will stop publishing agent activity.",
});
} catch (error) {
toastManager.add({
type: "error",
title: "Cloud preference update failed",
description: cloudErrorMessage(error, "Could not update cloud preferences."),
});
} finally {
setIsUpdatingPreference(false);
}
};
return (
}>
}
/>
void updatePublishAgentActivity(enabled)}
/>
}
/>
}
/>
Refresh notification devices
}
>
{devicesState.data === null ? (
) : devices.length > 0 ? (
devices.map((device) => (
))
) : (
)}
);
}