Files
cal.diy/companion/utils/alerts.ts
T
Dhairyashil Shinde 1c3c6292df refactor: handle unavailable features on iOS and consolidate settings UI (#27081)
- Refactor duplicate UI components (SettingsGroup, SettingRow, NavigationRow) into [SettingsUI.tsx]
- Centralize showNotAvailableAlert in utils/alerts.ts
- Replace in-app browser links with Feature not available alerts for iOS on:
  - More tab items (Apps, Routing, Workflows, Insights, Support)
  - Event Type Other tab (Apps, Workflows, Webhooks)
  - Event Type Advanced tab (Private Links)
- Remove View public page from Profile Sheet on iOS
- Remove Preview actions from Event Type (List menu and Detail quick actions) on iOS
- Add Learn More links to Advanced Tab settings
2026-01-21 09:42:06 -03:00

102 lines
2.9 KiB
TypeScript

/**
* Alert Utilities
*
* Helper functions for showing alerts with environment-aware and platform-aware behavior.
* On iOS/Android, uses native Alert.alert().
* On web (browser extension), uses the global toast system for centered notifications.
* Error alerts are only shown in development mode to avoid confusing users in production.
*/
import { Alert, Platform } from "react-native";
type ToastType = "success" | "error" | "info";
/**
* Global toast reference (set by ToastProvider)
* This allows the alert utilities to show toasts on web without requiring React context.
*/
let globalShowToast: ((title: string, message: string, type?: ToastType) => void) | null = null;
/**
* Set the global toast function. Called by ToastProvider on mount.
* @param fn - The showToast function from ToastProvider, or null to clear
*/
export const setGlobalToastFunction = (fn: typeof globalShowToast) => {
globalShowToast = fn;
};
/**
* Show an error alert only in development mode.
* On web, shows a centered toast notification.
* In production, errors are silently logged to console.
*
* @param title - The alert title
* @param message - The error message to display
*/
export const showErrorAlert = (title: string, message: string) => {
if (Platform.OS === "web" && globalShowToast) {
if (__DEV__) {
globalShowToast(title, message, "error");
} else {
console.error(`[${title}] ${message}`);
}
return;
}
if (__DEV__) {
Alert.alert(title, message);
} else {
console.error(`[${title}] ${message}`);
}
};
/**
* Show a confirmation alert (always shown - user-initiated actions)
* This is a direct reference to Alert.alert for consistency.
* Note: On web, this will not work - use a modal or custom confirmation dialog instead.
*/
export const showConfirmAlert = Alert.alert;
/**
* Show a success alert (always shown - user feedback)
* On web, shows a centered toast notification.
*
* @param title - The alert title
* @param message - The success message to display
*/
export const showSuccessAlert = (title: string, message: string) => {
if (Platform.OS === "web" && globalShowToast) {
globalShowToast(title, message, "success");
return;
}
Alert.alert(title, message);
};
/**
* Show an info alert (always shown - informational feedback)
* On web, shows a centered toast notification.
*
* @param title - The alert title
* @param message - The info message to display
*/
export const showInfoAlert = (title: string, message: string) => {
if (Platform.OS === "web" && globalShowToast) {
globalShowToast(title, message, "info");
return;
}
Alert.alert(title, message);
};
/**
* Show a standard "Feature not available" alert for companion app limitations.
*/
export const showNotAvailableAlert = () => {
Alert.alert(
"Feature not available",
"This feature is not available on app yet. To use, please visit app.cal.com.",
[{ text: "OK", style: "cancel" }]
);
};