Supra_App/src/routes/(authed)/+layout.svelte

129 lines
3.6 KiB
Svelte
Raw Normal View History

2026-02-17 14:30:02 +07:00
<!-- navbar select menus -->
<script lang="ts">
import favicon from '$lib/assets/favicon.svg';
import AppAccountSelect from '$lib/components/app-account-select.svelte';
import AppSidebar from '$lib/components/app-sidebar.svelte';
import * as Sidebar from '$lib/components/ui/sidebar/index';
import '../layout.css';
import ErrorLayout from '$lib/components/error-layout.svelte';
import { sidebarStore } from '$lib/core/stores/sidebar';
import { auth } from '$lib/core/stores/auth';
import { connectToWebsocket } from '$lib/core/stores/websocketStore';
import * as adb from '$lib/core/adb/adb';
import { addNotification } from '$lib/core/stores/noti';
2026-06-09 10:50:59 +07:00
import { page } from '$app/stores';
import {
AdbDaemonWebUsbDevice,
AdbDaemonWebUsbDeviceManager
} from '@yume-chan/adb-daemon-webusb';
import AdbWebCredentialStore from '@yume-chan/adb-credential-web';
import { deviceCredentialManager } from '$lib/core/adb/deviceCredManager';
2026-02-17 14:30:02 +07:00
let { children } = $props();
2026-06-09 10:50:59 +07:00
let websocketConnectedForUid = $state('');
let adbReconnectTriedForUid = $state('');
function getAutoConnectChannel(pathname: string) {
if (pathname.startsWith('/tools/create-menu')) {
return 'recipe';
}
if (pathname.startsWith('/tools/brew')) {
return 'brew';
}
return 'adb';
}
async function tryAutoConnect() {
try {
2026-06-09 10:50:59 +07:00
if (adb.getAdbInstance()) return true;
if (!('usb' in navigator) || !AdbDaemonWebUsbDeviceManager.BROWSER) {
throw new Error('WebUSB not supported, try using fallback or different browser');
}
const devices = await AdbDaemonWebUsbDeviceManager.BROWSER.getDevices();
if (!devices || devices.length == 0) {
throw new Error('No device found');
}
if (devices.length > 1) {
throw new Error('Too many connected devices');
}
const device = devices[0];
const credStore = new AdbWebCredentialStore();
try {
2026-06-09 10:50:59 +07:00
const channel = getAutoConnectChannel($page.url.pathname);
if (channel === 'recipe') {
await adb.connectRecipeMenuDeviceByCred(device, credStore);
} else {
await adb.connectDeviceByCred(device, credStore, channel === 'brew');
}
return true;
} catch (e: any) {
if (e.message === 'CREDENTIAL_EXPIRED') {
try {
await deviceCredentialManager.clearAllCredentials();
} catch (ignored) {}
}
if (e instanceof AdbDaemonWebUsbDevice.DeviceBusyError) {
addNotification(
'ERR:Device is already in use by another program, please close the program and try again'
);
}
return false;
}
} catch (e) {
console.error('error on auto connect brew page', e);
addNotification('ERROR:Failed to auto connect, please try again');
}
}
2026-06-09 10:50:59 +07:00
$effect(() => {
const currentUser = $auth;
2026-06-09 10:50:59 +07:00
if (!currentUser) {
websocketConnectedForUid = '';
adbReconnectTriedForUid = '';
return;
}
2026-06-09 10:50:59 +07:00
if (websocketConnectedForUid !== currentUser.uid) {
websocketConnectedForUid = currentUser.uid;
console.log('connect ws after auth ready');
2026-06-09 10:50:59 +07:00
void currentUser.getIdToken().then((idToken) => {
connectToWebsocket(idToken);
});
}
if (adbReconnectTriedForUid !== currentUser.uid && !adb.getAdbInstance()) {
adbReconnectTriedForUid = currentUser.uid;
// void tryAutoConnect();
2026-06-09 10:50:59 +07:00
}
});
2026-02-17 14:30:02 +07:00
</script>
<svelte:head>
<link rel="icon" href={favicon} />
<link href="https://fonts.googleapis.com/css2?family=Roboto+Flex" rel="stylesheet" />
<title>Taobin Management Tools</title>
</svelte:head>
<Sidebar.Provider
onOpenChange={(open) => {
sidebarStore.set(open);
}}
>
<AppSidebar />
2026-06-09 10:50:59 +07:00
<main class="h-screen w-screen overflow-auto">
2026-02-17 14:30:02 +07:00
<Sidebar.Trigger />
{@render children()}
</main>
</Sidebar.Provider>