Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
"use client";
import { Suspense, useEffect, useSyncExternalStore } from "react";
import { useRouter, usePathname, useSearchParams } from "next/navigation";
import { PrefetchKind } from "next/dist/client/components/router-reducer/router-reducer-types";
import {
NavigationProvider,
type NavigationAdapter,
} from "@multica/views/navigation";
import { canGoBackInApp } from "./in-app-history";
/**
* Web half of the `multica:navigate` bridge — the event shared content
* (comments, chat, issue descriptions) fires when a link resolves to an in-app
* destination. A plain click ("push") is a router push in place. A modifier
* click normally never reaches here on web — real anchors leave it to the
* browser — but the editor must intercept every click (contenteditable
* anchors don't navigate natively), and for those `window.open` is the
* closest the web can get: JS cannot open a background tab, so both tab
* dispositions land as a foreground browser tab.
*/
function useInternalLinkHandler(router: ReturnType<typeof useRouter>) {
useEffect(() => {
const handler = (e: Event) => {
const detail = (
e as CustomEvent<{ path?: string; disposition?: string }>
).detail;
const path = detail?.path;
if (!path) return;
if (
detail?.disposition === "background-tab" ||
detail?.disposition === "foreground-tab"
) {
window.open(
window.location.origin + path,
"_blank",
"noopener,noreferrer",
);
return;
}
router.push(path);
};
window.addEventListener("multica:navigate", handler);
return () => window.removeEventListener("multica:navigate", handler);
}, [router]);
}
/**
* The fragment is client-only state Next.js never surfaces: `usePathname()`
* drops it, and a `router.replace("/x#y")` mutates `window.location` without
* a render of its own. Reading it through an external store re-reads the URL
* on every render and re-renders on the events that change it behind React's
* back, so `adapter.hash` is never a stale copy.
*/
function subscribeToHash(onStoreChange: () => void): () => void {
window.addEventListener("hashchange", onStoreChange);
window.addEventListener("popstate", onStoreChange);
return () => {
window.removeEventListener("hashchange", onStoreChange);
window.removeEventListener("popstate", onStoreChange);
};
}
function NavigationProviderInner({
children,
}: {
children: React.ReactNode;
}) {
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();
const hash = useSyncExternalStore(
subscribeToHash,
() => window.location.hash,
() => "",
);
useInternalLinkHandler(router);
const adapter: NavigationAdapter = {
push: router.push,
replace: router.replace,
back: router.back,
forward: router.forward,
canGoBack: canGoBackInApp,
pathname,
searchParams: new URLSearchParams(searchParams.toString()),
hash,
getShareableUrl: (path: string) =>
typeof window === "undefined" ? path : window.location.origin + path,
openInDesktop: (path: string) => {
const url = window.location.origin + path;
window.location.href = `multica://open/issue?url=${encodeURIComponent(url)}`;
},
// router.prefetch is a no-op in dev mode by Next.js design; in production
// it warms the RSC payload + route chunk so the next push() commits with
// no network round-trip. Safe to call repeatedly — Next dedupes internally.
// FULL: workspace routes are dynamic, and the default prefetch stops at
// their loading boundary, leaving the click to fetch the rest. Their
// payload is a small client-component shell, so take all of it (MUL-9538).
prefetch: (path: string) => {
router.prefetch(path, { kind: PrefetchKind.FULL });
},
};
return <NavigationProvider value={adapter}>{children}</NavigationProvider>;
}
export function WebNavigationProvider({
children,
}: {
children: React.ReactNode;
}) {
return (
<Suspense>
<NavigationProviderInner>{children}</NavigationProviderInner>
</Suspense>
);
}