Files
cal.diy/companion/components/LogoutButton.tsx
T
9c680838b6 fix(companion): improve code clarity and type safety in components (#26062)
- Remove 'as any' type assertion in LoginScreen.tsx, use Platform.OS check
- Fix malformed className string in LogoutButton.tsx (extra quotes)
- Remove @ts-ignore comments in Tooltip.tsx with proper web-specific types
- Replace 'any' type with Schedule type in AvailabilityTab.tsx
- Replace 'any' type with 'string' for SF Symbol icons in EventTypeListItem.ios.tsx
- Remove console.log error handling in SvgImage.tsx
- Extract duplicated formatDuration function to use shared utility from formatters.ts
- Extract repeated inline styles to sectionDividerStyle constant in AvailabilityTab.tsx

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Dhairyashil <dhairyashil10101010@gmail.com>
2025-12-23 00:26:16 +00:00

82 lines
2.2 KiB
TypeScript

import { useAuth } from "../contexts/AuthContext";
import { LogoutConfirmModal } from "./LogoutConfirmModal";
import React, { useState } from "react";
import { TouchableOpacity, Text, Alert, Platform } from "react-native";
interface LogoutButtonProps {
className?: string;
variant?: "default" | "destructive" | "ghost";
}
export function LogoutButton({ className = "" }: LogoutButtonProps) {
const { logout } = useAuth();
const [showConfirmModal, setShowConfirmModal] = useState(false);
const handleLogoutPress = () => {
if (Platform.OS === "web") {
setShowConfirmModal(true);
} else {
// Mobile: use Alert.alert
Alert.alert("Sign Out", "Are you sure you want to sign out?", [
{
text: "Cancel",
style: "cancel",
},
{
text: "Sign Out",
style: "destructive",
onPress: performLogout,
},
]);
}
};
const performLogout = async () => {
try {
await logout();
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error("Logout error", message);
if (__DEV__) {
const stack = error instanceof Error ? error.stack : undefined;
console.debug("[LogoutButton] logout failed", { message, stack });
}
if (Platform.OS === "web") {
window.alert("Failed to sign out. Please try again.");
} else {
Alert.alert("Error", "Failed to sign out. Please try again.");
}
}
};
const handleConfirmLogout = () => {
setShowConfirmModal(false);
performLogout();
};
const handleCancelLogout = () => {
setShowConfirmModal(false);
};
return (
<>
<TouchableOpacity
onPress={handleLogoutPress}
className={`rounded-lg bg-gray-600 px-4 py-2 ${className}`}
style={Platform.OS === "web" ? { cursor: "pointer" } : undefined}
activeOpacity={0.7}
>
<Text className="font-medium text-gray-900">Sign Out</Text>
</TouchableOpacity>
<LogoutConfirmModal
visible={showConfirmModal}
onConfirm={handleConfirmLogout}
onCancel={handleCancelLogout}
/>
</>
);
}
export default LogoutButton;