Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

feat: integrations intersection observer

Sidwebworkse18d0f84734f
diff --git a/apps/erp/app/hooks/useAnyVisible.ts b/apps/erp/app/hooks/useAnyVisible.ts
new file mode 100644
index 0000000000..91ef4bca41
--- /dev/null
+++ b/apps/erp/app/hooks/useAnyVisible.ts
@@ -0,0 +1,63 @@
+import { type RefObject, useEffect, useState } from "react";
+
+type UseAnyVisibleOptions = {
+ /** Container whose descendants are queried. */
+ containerRef: RefObject<HTMLElement | null>;
+ /** CSS selector for elements to observe. */
+ selector: string;
+ /** Skip observing entirely. Hook returns `false`. */
+ enabled?: boolean;
+ /** IntersectionObserver threshold. */
+ threshold?: number | number[];
+ /** Re-query the DOM when these change (e.g. list contents). */
+ deps?: ReadonlyArray<unknown>;
+};
+
+/**
+ * Returns `true` when at least one element matching `selector` inside
+ * `containerRef` is intersecting the viewport.
+ *
+ * Returns `false` when disabled, when the container is missing, or when no
+ * matched elements exist.
+ */
+export function useAnyVisible({
+ containerRef,
+ selector,
+ enabled = true,
+ threshold = 0.1,
+ deps = []
+}: UseAnyVisibleOptions): boolean {
+ const [anyVisible, setAnyVisible] = useState(false);
+
+ useEffect(() => {
+ if (!enabled) {
+ setAnyVisible(false);
+ return;
+ }
+ const root = containerRef.current;
+ if (!root) return;
+
+ const targets = root.querySelectorAll<HTMLElement>(selector);
+ if (targets.length === 0) {
+ setAnyVisible(false);
+ return;
+ }
+
+ const visible = new Set<Element>();
+ const obs = new IntersectionObserver(
+ (entries) => {
+ for (const e of entries) {
+ if (e.isIntersecting) visible.add(e.target);
+ else visible.delete(e.target);
+ }
+ setAnyVisible(visible.size > 0);
+ },
+ { threshold }
+ );
+ targets.forEach((t) => obs.observe(t));
+ return () => obs.disconnect();
+ // biome-ignore lint/correctness/useExhaustiveDependencies: caller-controlled `deps` re-queries DOM
+ }, [enabled, selector, threshold, containerRef, ...deps]);
+
+ return anyVisible;
+}