9deb320cf6
Rename the global Sidebar route, UI, assets, analytics, and payment return context to Profile. Add accessible message-avatar navigation and preserve the source character across auth and logout flows. BREAKING CHANGE: /sidebar has been removed; use /profile instead.
55 lines
1.2 KiB
TypeScript
55 lines
1.2 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import { pwaUtil } from "@/utils/pwa";
|
|
|
|
type PwaInstallResult = Awaited<ReturnType<typeof pwaUtil.install>>;
|
|
|
|
export function usePwaInstallEntry() {
|
|
const [canInstall, setCanInstall] = useState(false);
|
|
|
|
useEffect(() => {
|
|
const updateInstallEntryVisibility = () => {
|
|
setCanInstall(pwaUtil.canShowInstallEntry());
|
|
};
|
|
|
|
pwaUtil.prepareInstallPrompt();
|
|
updateInstallEntryVisibility();
|
|
const unsubscribe = pwaUtil.subscribe(updateInstallEntryVisibility);
|
|
|
|
return () => {
|
|
unsubscribe();
|
|
};
|
|
}, []);
|
|
|
|
const installApp = async () => {
|
|
pwaUtil.prepareInstallPrompt();
|
|
const result = await pwaUtil.install();
|
|
setCanInstall(
|
|
shouldKeepPwaInstallEntryVisible({
|
|
result,
|
|
isInstalled: pwaUtil.isInstalled(),
|
|
canShowInstallEntry: pwaUtil.canShowInstallEntry(),
|
|
}),
|
|
);
|
|
};
|
|
|
|
return {
|
|
canInstall,
|
|
installApp,
|
|
};
|
|
}
|
|
|
|
export function shouldKeepPwaInstallEntryVisible({
|
|
result,
|
|
isInstalled,
|
|
canShowInstallEntry,
|
|
}: {
|
|
result: PwaInstallResult;
|
|
isInstalled: boolean;
|
|
canShowInstallEntry: boolean;
|
|
}): boolean {
|
|
return result !== "accepted" && !isInstalled && canShowInstallEntry;
|
|
}
|