| 42 | const STORAGE_KEY_HELPER = 'linuxinit_selected_helper'; |
| 43 | |
| 44 | export function useLinuxInit(): UseLinuxInitReturn { |
| 45 | const [selectedDistro, setSelectedDistroState] = useState<DistroId>('ubuntu'); |
| 46 | const [selectedApps, setSelectedApps] = useState<Set<string>>(new Set()); |
| 47 | const [hasYayInstalled, setHasYayInstalled] = useState(false); |
| 48 | const [selectedHelper, setSelectedHelper] = useState<'yay' | 'paru'>('yay'); |
| 49 | const [hydrated, setHydrated] = useState(false); |
| 50 | |
| 51 | // Load saved preferences from localStorage or URL params |
| 52 | useEffect(() => { |
| 53 | try { |
| 54 | logger.debug('Loading user preferences', { source: 'localStorage/URL' }); |
| 55 | // First check for URL config parameter |
| 56 | const urlParams = new URLSearchParams(window.location.search); |
| 57 | const configParam = urlParams.get('config'); |
| 58 | |
| 59 | let loadedDistro: DistroId | null = null; |
| 60 | let loadedApps: string[] = []; |
| 61 | |
| 62 | if (configParam) { |
| 63 | // Load from URL parameter |
| 64 | const urlConfig = decodeConfigFromUrl(configParam); |
| 65 | if (urlConfig && urlConfig.distro && urlConfig.apps) { |
| 66 | loadedDistro = urlConfig.distro; |
| 67 | loadedApps = urlConfig.apps; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Fallback to localStorage if no URL config |
| 72 | if (!loadedDistro) { |
| 73 | const savedDistro = localStorage.getItem(STORAGE_KEY_DISTRO) as DistroId | null; |
| 74 | const savedApps = localStorage.getItem(STORAGE_KEY_APPS); |
| 75 | |
| 76 | if (savedDistro && distros.some(d => d.id === savedDistro)) { |
| 77 | loadedDistro = savedDistro; |
| 78 | } |
| 79 | |
| 80 | if (savedApps) { |
| 81 | loadedApps = JSON.parse(savedApps) as string[]; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Apply loaded distro |
| 86 | if (loadedDistro) { |
| 87 | setSelectedDistroState(loadedDistro); |
| 88 | } |
| 89 | |
| 90 | // Apply loaded apps (filter to valid ones) |
| 91 | if (loadedApps.length > 0) { |
| 92 | const validApps = loadedApps.filter(id => { |
| 93 | const app = apps.find(a => a.id === id); |
| 94 | if (!app) return false; |
| 95 | const pkg = app.targets[loadedDistro || 'ubuntu']; |
| 96 | return pkg !== undefined && pkg !== null; |
| 97 | }); |
| 98 | setSelectedApps(new Set(validApps)); |
| 99 | } |
| 100 | |
| 101 | // Load other preferences |