refactor(sidebar): split state derivation

This commit is contained in:
2026-07-10 10:38:21 +08:00
parent 270e84385b
commit a1a93a51fb
13 changed files with 360 additions and 331 deletions
+54
View File
@@ -0,0 +1,54 @@
"use client";
import { useEffect, useState } from "react";
import { pwaUtil } from "@/utils";
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;
}