OneLauncher-Cracked/apps/oneclient/frontend/src/hooks/useUpdater.ts
Jacob adf9f4bd63
feat: debug page (#493)
* fix: discordRPC route sorting

It wasn't alphabetical and it was annoying me

* feat: debug page

* move stuff into hooks where they should be

* fix: building
2026-03-06 01:25:03 -05:00

56 lines
1.3 KiB
TypeScript

import type { UnlistenFn } from '@tauri-apps/api/event';
import { bindings } from '@/main';
import { listen } from '@tauri-apps/api/event';
import { useEffect } from 'react';
export interface Update {
version: string;
}
export type UpdateEvent =
| { status: 'loading' }
| { status: 'error'; error: string }
| { status: 'updateAvailable'; update: Update }
| { status: 'noUpdateAvailable' }
| { status: 'installing' };
export async function checkForUpdate(): Promise<Update | null> {
return await bindings.oneclient.checkForUpdate();
}
export async function installUpdate(): Promise<void> {
await bindings.oneclient.installUpdate();
}
export async function listenForUpdateEvents(
callback: (event: UpdateEvent) => void,
): Promise<UnlistenFn> {
return await listen<UpdateEvent>('updater', (event) => {
callback(event.payload);
});
}
export function useAutoUpdater() {
useEffect(() => {
void (async () => {
try {
const update = await checkForUpdate();
if (!update)
return;
// eslint-disable-next-line no-console -- Used for debugging - aka important
console.log('Update found on initial check:', update.version);
try {
await installUpdate();
}
catch (e) {
console.error('Failed to install update:', e);
}
}
catch (e) {
console.error('Failed to check for update:', e);
}
})();
}, []);
}