|
| 1 | +import { useEffect, useState, useCallback, useRef } from "react"; |
| 2 | +import { useTranslation } from "react-i18next"; |
| 3 | +import { platform } from "@tauri-apps/plugin-os"; |
| 4 | +import { |
| 5 | + checkAccessibilityPermission, |
| 6 | + requestAccessibilityPermission, |
| 7 | +} from "tauri-plugin-macos-permissions-api"; |
| 8 | +import { commands } from "@/bindings"; |
| 9 | +import HandyTextLogo from "../icons/HandyTextLogo"; |
| 10 | +import { Keyboard, Check, Loader2 } from "lucide-react"; |
| 11 | + |
| 12 | +interface AccessibilityOnboardingProps { |
| 13 | + onComplete: () => void; |
| 14 | +} |
| 15 | + |
| 16 | +type PermissionState = "checking" | "prompt" | "waiting" | "granted"; |
| 17 | + |
| 18 | +const AccessibilityOnboarding: React.FC<AccessibilityOnboardingProps> = ({ |
| 19 | + onComplete, |
| 20 | +}) => { |
| 21 | + const { t } = useTranslation(); |
| 22 | + const [isMacOS, setIsMacOS] = useState<boolean | null>(null); |
| 23 | + const [permissionState, setPermissionState] = |
| 24 | + useState<PermissionState>("checking"); |
| 25 | + const pollingRef = useRef<ReturnType<typeof setInterval> | null>(null); |
| 26 | + |
| 27 | + // Check platform and permission status on mount |
| 28 | + useEffect(() => { |
| 29 | + const currentPlatform = platform(); |
| 30 | + const isMac = currentPlatform === "macos"; |
| 31 | + setIsMacOS(isMac); |
| 32 | + |
| 33 | + // Skip immediately on non-macOS - no permission needed |
| 34 | + if (!isMac) { |
| 35 | + onComplete(); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // On macOS, check if permission is already granted |
| 40 | + const checkInitial = async () => { |
| 41 | + try { |
| 42 | + const granted = await checkAccessibilityPermission(); |
| 43 | + if (granted) { |
| 44 | + // Already have permission, initialize Enigo and skip ahead |
| 45 | + try { |
| 46 | + await commands.initializeEnigo(); |
| 47 | + } catch (e) { |
| 48 | + console.warn("Failed to initialize Enigo:", e); |
| 49 | + } |
| 50 | + setPermissionState("granted"); |
| 51 | + setTimeout(() => onComplete(), 300); |
| 52 | + } else { |
| 53 | + // Need to ask for permission - show the prompt |
| 54 | + setPermissionState("prompt"); |
| 55 | + } |
| 56 | + } catch (error) { |
| 57 | + console.error("Failed to check accessibility permission:", error); |
| 58 | + // On error, show the prompt anyway |
| 59 | + setPermissionState("prompt"); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + checkInitial(); |
| 64 | + }, [onComplete]); |
| 65 | + |
| 66 | + // Polling for permission after user clicks the button |
| 67 | + const startPolling = useCallback(() => { |
| 68 | + if (pollingRef.current) return; // Already polling |
| 69 | + |
| 70 | + pollingRef.current = setInterval(async () => { |
| 71 | + try { |
| 72 | + const granted = await checkAccessibilityPermission(); |
| 73 | + if (granted) { |
| 74 | + // Permission granted! |
| 75 | + if (pollingRef.current) { |
| 76 | + clearInterval(pollingRef.current); |
| 77 | + pollingRef.current = null; |
| 78 | + } |
| 79 | + |
| 80 | + // Try to initialize Enigo now that we have permission |
| 81 | + try { |
| 82 | + await commands.initializeEnigo(); |
| 83 | + } catch (e) { |
| 84 | + console.warn("Failed to initialize Enigo:", e); |
| 85 | + } |
| 86 | + |
| 87 | + setPermissionState("granted"); |
| 88 | + setTimeout(() => onComplete(), 500); |
| 89 | + } |
| 90 | + } catch (error) { |
| 91 | + console.error("Error checking permission:", error); |
| 92 | + } |
| 93 | + }, 1000); |
| 94 | + }, [onComplete]); |
| 95 | + |
| 96 | + // Cleanup polling on unmount |
| 97 | + useEffect(() => { |
| 98 | + return () => { |
| 99 | + if (pollingRef.current) { |
| 100 | + clearInterval(pollingRef.current); |
| 101 | + } |
| 102 | + }; |
| 103 | + }, []); |
| 104 | + |
| 105 | + const handleGivePermission = async () => { |
| 106 | + try { |
| 107 | + // This opens System Settings to the Accessibility pane |
| 108 | + await requestAccessibilityPermission(); |
| 109 | + } catch (error) { |
| 110 | + console.error("Failed to request accessibility permission:", error); |
| 111 | + } |
| 112 | + // Start polling for permission regardless of whether request succeeded |
| 113 | + setPermissionState("waiting"); |
| 114 | + startPolling(); |
| 115 | + }; |
| 116 | + |
| 117 | + // Still checking platform/initial permission |
| 118 | + if (isMacOS === null || permissionState === "checking") { |
| 119 | + return ( |
| 120 | + <div className="h-screen w-screen flex items-center justify-center"> |
| 121 | + <Loader2 className="w-8 h-8 animate-spin text-text/50" /> |
| 122 | + </div> |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + // Permission granted - show success briefly |
| 127 | + if (permissionState === "granted") { |
| 128 | + return ( |
| 129 | + <div className="h-screen w-screen flex flex-col items-center justify-center gap-4"> |
| 130 | + <div className="p-4 rounded-full bg-emerald-500/20"> |
| 131 | + <Check className="w-12 h-12 text-emerald-400" /> |
| 132 | + </div> |
| 133 | + <p className="text-lg font-medium text-text"> |
| 134 | + {t("onboarding.accessibility.granted")} |
| 135 | + </p> |
| 136 | + </div> |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + // Show permission request screen (prompt or waiting state) |
| 141 | + return ( |
| 142 | + <div className="h-screen w-screen flex flex-col p-6 gap-6 items-center justify-center"> |
| 143 | + <div className="flex flex-col items-center gap-2"> |
| 144 | + <HandyTextLogo width={200} /> |
| 145 | + </div> |
| 146 | + |
| 147 | + <div className="max-w-md w-full flex flex-col items-center gap-6"> |
| 148 | + <div className="p-4 rounded-full bg-logo-primary/20"> |
| 149 | + <Keyboard className="w-12 h-12 text-logo-primary" /> |
| 150 | + </div> |
| 151 | + |
| 152 | + <div className="text-center"> |
| 153 | + <h2 className="text-xl font-semibold text-text mb-2"> |
| 154 | + {t("onboarding.accessibility.title")} |
| 155 | + </h2> |
| 156 | + <p className="text-text/70"> |
| 157 | + {t("onboarding.accessibility.description")} |
| 158 | + </p> |
| 159 | + </div> |
| 160 | + |
| 161 | + <div className="w-full p-4 rounded-lg bg-white/5 border border-mid-gray/20"> |
| 162 | + <p className="text-sm text-text/60 text-center"> |
| 163 | + {t("onboarding.accessibility.explanation")} |
| 164 | + </p> |
| 165 | + </div> |
| 166 | + |
| 167 | + {permissionState === "prompt" && ( |
| 168 | + <button |
| 169 | + onClick={handleGivePermission} |
| 170 | + className="w-full py-3 px-6 rounded-lg bg-logo-primary hover:bg-logo-primary/90 text-white font-medium transition-colors" |
| 171 | + > |
| 172 | + {t("onboarding.accessibility.givePermission")} |
| 173 | + </button> |
| 174 | + )} |
| 175 | + |
| 176 | + {permissionState === "waiting" && ( |
| 177 | + <> |
| 178 | + <button |
| 179 | + disabled |
| 180 | + className="w-full py-3 px-6 rounded-lg bg-mid-gray/30 text-text/50 font-medium cursor-not-allowed flex items-center justify-center gap-2" |
| 181 | + > |
| 182 | + <Loader2 className="w-4 h-4 animate-spin" /> |
| 183 | + {t("onboarding.accessibility.waiting")} |
| 184 | + </button> |
| 185 | + <p className="text-xs text-text/40 text-center"> |
| 186 | + {t("onboarding.accessibility.instructions")} |
| 187 | + </p> |
| 188 | + </> |
| 189 | + )} |
| 190 | + </div> |
| 191 | + </div> |
| 192 | + ); |
| 193 | +}; |
| 194 | + |
| 195 | +export default AccessibilityOnboarding; |
0 commit comments