2026-03-23 18:09:59 +02:00
|
|
|
"use client";
|
|
|
|
|
|
2026-03-24 16:06:20 +02:00
|
|
|
import {
|
|
|
|
|
useConnectionState,
|
|
|
|
|
useZero,
|
|
|
|
|
ZeroProvider as ZeroReactProvider,
|
|
|
|
|
} from "@rocicorp/zero/react";
|
|
|
|
|
import { useAtomValue } from "jotai";
|
2026-06-23 12:59:16 +05:30
|
|
|
import { usePathname } from "next/navigation";
|
2026-06-24 03:55:40 +05:30
|
|
|
import { useEffect, useMemo, useState } from "react";
|
2026-03-23 18:09:59 +02:00
|
|
|
import { currentUserAtom } from "@/atoms/user/user-query.atoms";
|
2026-06-23 12:59:16 +05:30
|
|
|
import { useSession } from "@/hooks/use-session";
|
2026-06-24 03:55:40 +05:30
|
|
|
import { getDesktopAccessToken } from "@/lib/auth-fetch";
|
|
|
|
|
import { handleUnauthorized, isPublicRoute, refreshSession } from "@/lib/auth-utils";
|
2026-03-23 18:09:59 +02:00
|
|
|
import { queries } from "@/zero/queries";
|
|
|
|
|
import { schema } from "@/zero/schema";
|
|
|
|
|
|
2026-06-15 11:03:45 +05:30
|
|
|
const configuredCacheURL = process.env.NEXT_PUBLIC_ZERO_CACHE_URL;
|
|
|
|
|
|
|
|
|
|
function getCacheURL() {
|
|
|
|
|
if (configuredCacheURL) return configuredCacheURL;
|
|
|
|
|
if (typeof window !== "undefined") {
|
|
|
|
|
return `${window.location.origin}/zero`;
|
|
|
|
|
}
|
|
|
|
|
return "http://localhost:4848";
|
|
|
|
|
}
|
2026-03-23 18:09:59 +02:00
|
|
|
|
2026-06-23 12:59:16 +05:30
|
|
|
function ZeroAuthSync({ isDesktop }: { isDesktop: boolean }) {
|
2026-03-24 16:06:20 +02:00
|
|
|
const zero = useZero();
|
|
|
|
|
const connectionState = useConnectionState();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-06-23 12:59:16 +05:30
|
|
|
if (connectionState.name !== "needs-auth") return;
|
|
|
|
|
|
2026-06-24 03:55:40 +05:30
|
|
|
refreshSession().then(async (refreshed) => {
|
|
|
|
|
if (!refreshed) {
|
2026-06-23 12:59:16 +05:30
|
|
|
handleUnauthorized();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isDesktop) {
|
2026-06-24 03:55:40 +05:30
|
|
|
const newToken = await getDesktopAccessToken();
|
|
|
|
|
if (!newToken) {
|
|
|
|
|
handleUnauthorized();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-06-23 12:59:16 +05:30
|
|
|
zero.connection.connect({ auth: newToken });
|
|
|
|
|
} else {
|
|
|
|
|
zero.connection.connect();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, [connectionState.name, isDesktop, zero]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (typeof window === "undefined" || !window.electronAPI?.onAuthChanged) return;
|
|
|
|
|
return window.electronAPI.onAuthChanged(({ accessToken }) => {
|
|
|
|
|
if (accessToken) {
|
|
|
|
|
zero.connection.connect({ auth: accessToken });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, [zero]);
|
2026-03-24 16:06:20 +02:00
|
|
|
|
2026-03-31 18:39:45 -07:00
|
|
|
return null;
|
2026-03-24 16:06:20 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-23 12:59:16 +05:30
|
|
|
function AuthenticatedZeroProvider({
|
|
|
|
|
children,
|
|
|
|
|
isDesktop,
|
|
|
|
|
}: {
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
isDesktop: boolean;
|
|
|
|
|
}) {
|
|
|
|
|
const { data: user, isLoading } = useAtomValue(currentUserAtom);
|
2026-03-23 19:49:28 +02:00
|
|
|
|
2026-04-15 20:07:12 +08:00
|
|
|
const userId = user?.id;
|
2026-06-23 12:59:16 +05:30
|
|
|
const userID = userId ? String(userId) : undefined;
|
|
|
|
|
|
|
|
|
|
if (isLoading || !userID) {
|
|
|
|
|
return <>{children}</>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ZeroClientProvider userID={userID} isDesktop={isDesktop}>
|
|
|
|
|
{children}
|
|
|
|
|
</ZeroClientProvider>
|
2026-04-15 20:07:12 +08:00
|
|
|
);
|
2026-06-23 12:59:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ZeroClientProvider({
|
|
|
|
|
children,
|
|
|
|
|
userID,
|
|
|
|
|
isDesktop,
|
|
|
|
|
}: {
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
userID: string;
|
|
|
|
|
isDesktop: boolean;
|
|
|
|
|
}) {
|
|
|
|
|
const cacheURL = useMemo(() => getCacheURL(), []);
|
2026-06-24 03:55:40 +05:30
|
|
|
const [desktopAuth, setDesktopAuth] = useState<string | undefined>(undefined);
|
2026-06-23 12:59:16 +05:30
|
|
|
const context = useMemo(() => ({ userId: userID }), [userID]);
|
2026-04-15 20:07:12 +08:00
|
|
|
|
2026-06-24 03:55:40 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isDesktop) return;
|
|
|
|
|
let isMounted = true;
|
|
|
|
|
getDesktopAccessToken().then((token) => {
|
|
|
|
|
if (isMounted) setDesktopAuth(token || undefined);
|
|
|
|
|
});
|
|
|
|
|
return () => {
|
|
|
|
|
isMounted = false;
|
|
|
|
|
};
|
|
|
|
|
}, [isDesktop]);
|
|
|
|
|
|
2026-04-15 20:07:12 +08:00
|
|
|
const opts = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
userID,
|
|
|
|
|
schema,
|
|
|
|
|
queries,
|
|
|
|
|
context,
|
|
|
|
|
cacheURL,
|
2026-06-24 03:55:40 +05:30
|
|
|
auth: isDesktop ? desktopAuth : undefined,
|
2026-04-15 20:07:12 +08:00
|
|
|
}),
|
2026-06-24 03:55:40 +05:30
|
|
|
[userID, context, cacheURL, isDesktop, desktopAuth]
|
2026-04-15 20:07:12 +08:00
|
|
|
);
|
2026-03-23 18:09:59 +02:00
|
|
|
|
|
|
|
|
return (
|
2026-03-24 16:59:42 +02:00
|
|
|
<ZeroReactProvider {...opts}>
|
2026-06-23 12:59:16 +05:30
|
|
|
<ZeroAuthSync isDesktop={isDesktop} />
|
2026-03-31 18:39:45 -07:00
|
|
|
{children}
|
2026-03-23 18:09:59 +02:00
|
|
|
</ZeroReactProvider>
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-06-23 12:59:16 +05:30
|
|
|
|
|
|
|
|
function WebZeroProvider({ children }: { children: React.ReactNode }) {
|
|
|
|
|
const session = useSession();
|
|
|
|
|
|
|
|
|
|
if (session.status !== "authenticated") {
|
|
|
|
|
return <>{children}</>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <AuthenticatedZeroProvider isDesktop={false}>{children}</AuthenticatedZeroProvider>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function DesktopZeroProvider({ children }: { children: React.ReactNode }) {
|
|
|
|
|
return <AuthenticatedZeroProvider isDesktop>{children}</AuthenticatedZeroProvider>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ZeroProvider({ children }: { children: React.ReactNode }) {
|
|
|
|
|
const pathname = usePathname();
|
|
|
|
|
const isDesktop = typeof window !== "undefined" && !!window.electronAPI;
|
|
|
|
|
|
|
|
|
|
if (!isDesktop && isPublicRoute(pathname)) {
|
|
|
|
|
return <>{children}</>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isDesktop) {
|
|
|
|
|
return <DesktopZeroProvider>{children}</DesktopZeroProvider>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <WebZeroProvider>{children}</WebZeroProvider>;
|
|
|
|
|
}
|