aboutsummaryrefslogtreecommitdiff
path: root/src/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.ts')
-rw-r--r--src/utils.ts24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/utils.ts b/src/utils.ts
new file mode 100644
index 0000000..171bf71
--- /dev/null
+++ b/src/utils.ts
@@ -0,0 +1,24 @@
+import { LSPluginUserEvents } from "@logseq/libs/dist/LSPlugin.user";
+import React from "react";
+
+let _visible = logseq.isMainUIVisible;
+
+function subscribeLogseqEvent<T extends LSPluginUserEvents>(
+ eventName: T,
+ handler: (...args: any) => void
+) {
+ logseq.on(eventName, handler);
+ return () => {
+ logseq.off(eventName, handler);
+ };
+}
+
+const subscribeToUIVisible = (onChange: () => void) =>
+ subscribeLogseqEvent("ui:visible:changed", ({ visible }) => {
+ _visible = visible;
+ onChange();
+ });
+
+export const useAppVisible = () => {
+ return React.useSyncExternalStore(subscribeToUIVisible, () => _visible);
+};