MentraOS / mobile /src /hooks /useAppsExtras.ts
Anirudh1950's picture
Add files using upload-large-folder tool
68465df verified
Raw History Blame Contribute Delete
3.15 kB
import {useMemo, useSyncExternalStore} from "react"
import {SETTINGS, useActiveApps, useApps, useSetting} from "@mentra/engine"
import {isHiddenFromLaunchers, shouldHideMiniapp} from "@/services/miniapps/miniappVisibility"
import {deploymentStore} from "@/services/deployment/store"
const subscribeDeployment = (onChange: () => void) => deploymentStore.subscribe(onChange)
const getDeployment = () => deploymentStore.getActive()
/** Platform restrictions also apply to All Apps, which ignores home hiding. */
export const useAvailableApps = () => {
const apps = useApps()
const [showIosNotify] = useSetting<boolean>(SETTINGS.show_notify_ios.key)
const [superMode] = useSetting<boolean>(SETTINGS.super_mode.key)
const [miniappDevMode] = useSetting<boolean>(SETTINGS.miniapp_dev_mode.key)
const deployment = useSyncExternalStore(subscribeDeployment, getDeployment)
return useMemo(
() =>
apps.filter(
(app) =>
!isHiddenFromLaunchers(app.packageName) &&
!shouldHideMiniapp(app.packageName, app.version, {dev: app.isMiniappDev}),
),
[apps, showIosNotify, superMode, miniappDevMode, deployment],
)
}
/**
* Foreground tray: standard + background apps. Filtered to offline-only when
* `offline_mode` is on so cloud apps don't show up while disconnected.
*/
export const useForegroundApps = () => {
const apps = useAvailableApps()
const [isOffline] = useSetting(SETTINGS.offline_mode.key)
return useMemo(() => {
if (isOffline) {
return apps.filter((app) => (app.type === "standard" || app.type === "background" || !app.type) && app.offline)
}
return apps.filter((app) => app.type === "standard" || app.type === "background" || !app.type)
}, [apps, isOffline])
}
/**
* Like {@link useForegroundApps} but only the not-running entries — the apps
* actually rendered in the home grid (running ones get the active card).
*/
export const useInactiveForegroundApps = () => {
const apps = useAvailableApps()
const [isOffline] = useSetting(SETTINGS.offline_mode.key)
return useMemo(() => {
if (isOffline) {
return apps.filter((app) => (app.type === "standard" || app.type === "background") && !app.running && app.offline)
}
return apps.filter((app) => (app.type === "standard" || app.type === "background" || !app.type) && !app.running)
}, [apps, isOffline])
}
/**
* Apps incompatible with the currently configured wearable. Returns all apps
* if no wearable is selected (so the UI surface "all apps need glasses").
*/
export const useIncompatibleApps = () => {
const apps = useAvailableApps()
const [defaultWearable] = useSetting(SETTINGS.default_wearable.key)
return useMemo(() => {
if (!defaultWearable) return apps
return apps.filter((app) => !app.compatibility?.isCompatible)
}, [apps, defaultWearable])
}
/** Stable list of running app package names (shallow-equal to avoid re-renders). */
export const useActiveAppPackageNames = () => {
const active = useActiveApps()
return useMemo(() => active.map((a) => a.packageName), [active])
}