- derive pending approvals from thread activities and label status as "Pending Approval" - remove `isolate` class from chat `SidebarInset` containers
208 lines
6.5 KiB
TypeScript
208 lines
6.5 KiB
TypeScript
import { ThreadId } from "@t3tools/contracts";
|
|
import { createFileRoute, useNavigate } from "@tanstack/react-router";
|
|
import { Suspense, lazy, type ReactNode, useCallback, useEffect } from "react";
|
|
|
|
import ChatView from "../components/ChatView";
|
|
import { parseDiffRouteSearch, stripDiffSearchParams } from "../diffRouteSearch";
|
|
import { useMediaQuery } from "../hooks/useMediaQuery";
|
|
import { useStore } from "../store";
|
|
import { Sheet, SheetPopup } from "../components/ui/sheet";
|
|
import { Sidebar, SidebarInset, SidebarProvider, SidebarRail } from "~/components/ui/sidebar";
|
|
|
|
const DiffPanel = lazy(() => import("../components/DiffPanel"));
|
|
const DIFF_INLINE_LAYOUT_MEDIA_QUERY = "(max-width: 1180px)";
|
|
const DIFF_INLINE_SIDEBAR_WIDTH_STORAGE_KEY = "chat_diff_sidebar_width";
|
|
const DIFF_INLINE_DEFAULT_WIDTH = "clamp(28rem,48vw,44rem)";
|
|
const DIFF_INLINE_SIDEBAR_MIN_WIDTH = 26 * 16;
|
|
|
|
const DiffPanelSheet = (props: {
|
|
children: ReactNode;
|
|
diffOpen: boolean;
|
|
onCloseDiff: () => void;
|
|
}) => {
|
|
return (
|
|
<Sheet
|
|
open={props.diffOpen}
|
|
onOpenChange={(open) => {
|
|
if (!open) {
|
|
props.onCloseDiff();
|
|
}
|
|
}}
|
|
>
|
|
<SheetPopup
|
|
side="right"
|
|
showCloseButton={false}
|
|
keepMounted
|
|
className="w-[min(88vw,820px)] max-w-[820px] p-0"
|
|
>
|
|
{props.children}
|
|
</SheetPopup>
|
|
</Sheet>
|
|
);
|
|
};
|
|
|
|
const DiffLoadingFallback = (props: { inline: boolean }) => {
|
|
if (props.inline) {
|
|
return (
|
|
<div className="flex h-full min-h-0 items-center justify-center px-4 text-center text-xs text-muted-foreground/70">
|
|
Loading diff viewer...
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<aside className="flex h-full w-[560px] shrink-0 items-center justify-center border-l border-border bg-card px-4 text-center text-xs text-muted-foreground/70">
|
|
Loading diff viewer...
|
|
</aside>
|
|
);
|
|
};
|
|
|
|
const DiffPanelInlineSidebar = (props: {
|
|
diffOpen: boolean;
|
|
onCloseDiff: () => void;
|
|
onOpenDiff: () => void;
|
|
}) => {
|
|
const { diffOpen, onCloseDiff, onOpenDiff } = props;
|
|
const onOpenChange = useCallback(
|
|
(open: boolean) => {
|
|
if (open) {
|
|
onOpenDiff();
|
|
return;
|
|
}
|
|
onCloseDiff();
|
|
},
|
|
[onCloseDiff, onOpenDiff],
|
|
);
|
|
const shouldAcceptInlineSidebarWidth = useCallback(
|
|
({ nextWidth, wrapper }: { nextWidth: number; wrapper: HTMLElement }) => {
|
|
const composerForm = document.querySelector<HTMLElement>("[data-chat-composer-form='true']");
|
|
if (!composerForm) return true;
|
|
const composerViewport = composerForm.parentElement;
|
|
if (!composerViewport) return true;
|
|
const previousSidebarWidth = wrapper.style.getPropertyValue("--sidebar-width");
|
|
wrapper.style.setProperty("--sidebar-width", `${nextWidth}px`);
|
|
|
|
const viewportStyle = window.getComputedStyle(composerViewport);
|
|
const viewportPaddingLeft = Number.parseFloat(viewportStyle.paddingLeft) || 0;
|
|
const viewportPaddingRight = Number.parseFloat(viewportStyle.paddingRight) || 0;
|
|
const viewportContentWidth = Math.max(
|
|
0,
|
|
composerViewport.clientWidth - viewportPaddingLeft - viewportPaddingRight,
|
|
);
|
|
const formRect = composerForm.getBoundingClientRect();
|
|
const hasComposerOverflow = composerForm.scrollWidth > composerForm.clientWidth + 0.5;
|
|
const overflowsViewport = formRect.width > viewportContentWidth + 0.5;
|
|
|
|
if (previousSidebarWidth.length > 0) {
|
|
wrapper.style.setProperty("--sidebar-width", previousSidebarWidth);
|
|
} else {
|
|
wrapper.style.removeProperty("--sidebar-width");
|
|
}
|
|
|
|
return !hasComposerOverflow && !overflowsViewport;
|
|
},
|
|
[],
|
|
);
|
|
|
|
return (
|
|
<SidebarProvider
|
|
defaultOpen={false}
|
|
open={diffOpen}
|
|
onOpenChange={onOpenChange}
|
|
className="w-auto min-h-0 flex-none bg-transparent"
|
|
style={{ "--sidebar-width": DIFF_INLINE_DEFAULT_WIDTH } as React.CSSProperties}
|
|
>
|
|
<Sidebar
|
|
side="right"
|
|
collapsible="offcanvas"
|
|
className="border-l border-border bg-card text-foreground"
|
|
resizable={{
|
|
minWidth: DIFF_INLINE_SIDEBAR_MIN_WIDTH,
|
|
shouldAcceptWidth: shouldAcceptInlineSidebarWidth,
|
|
storageKey: DIFF_INLINE_SIDEBAR_WIDTH_STORAGE_KEY,
|
|
}}
|
|
>
|
|
<Suspense fallback={<DiffLoadingFallback inline />}>
|
|
<DiffPanel mode="sidebar" />
|
|
</Suspense>
|
|
<SidebarRail />
|
|
</Sidebar>
|
|
</SidebarProvider>
|
|
);
|
|
};
|
|
|
|
function ChatThreadRouteView() {
|
|
const { state } = useStore();
|
|
const navigate = useNavigate();
|
|
const threadId = Route.useParams({
|
|
select: (params) => ThreadId.makeUnsafe(params.threadId),
|
|
});
|
|
const search = Route.useSearch();
|
|
const threadExists = state.threads.some((thread) => thread.id === threadId);
|
|
const diffOpen = search.diff === "1";
|
|
const shouldUseDiffSheet = useMediaQuery(DIFF_INLINE_LAYOUT_MEDIA_QUERY);
|
|
const closeDiff = useCallback(() => {
|
|
void navigate({
|
|
to: "/$threadId",
|
|
params: { threadId },
|
|
search: (previous) => {
|
|
return stripDiffSearchParams(previous);
|
|
},
|
|
});
|
|
}, [navigate, threadId]);
|
|
const openDiff = useCallback(() => {
|
|
void navigate({
|
|
to: "/$threadId",
|
|
params: { threadId },
|
|
search: (previous) => {
|
|
const rest = stripDiffSearchParams(previous);
|
|
return { ...rest, diff: "1" };
|
|
},
|
|
});
|
|
}, [navigate, threadId]);
|
|
|
|
useEffect(() => {
|
|
if (!state.threadsHydrated) {
|
|
return;
|
|
}
|
|
|
|
if (!threadExists) {
|
|
void navigate({ to: "/", replace: true });
|
|
return;
|
|
}
|
|
}, [navigate, state.threadsHydrated, threadExists, threadId]);
|
|
|
|
if (!state.threadsHydrated || !threadExists) {
|
|
return null;
|
|
}
|
|
|
|
if (!shouldUseDiffSheet) {
|
|
return (
|
|
<>
|
|
<SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground">
|
|
<ChatView threadId={threadId} />
|
|
</SidebarInset>
|
|
<DiffPanelInlineSidebar diffOpen={diffOpen} onCloseDiff={closeDiff} onOpenDiff={openDiff} />
|
|
</>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground">
|
|
<ChatView threadId={threadId} />
|
|
</SidebarInset>
|
|
<DiffPanelSheet diffOpen={diffOpen} onCloseDiff={closeDiff}>
|
|
<Suspense fallback={<DiffLoadingFallback inline={false} />}>
|
|
<DiffPanel mode="sheet" />
|
|
</Suspense>
|
|
</DiffPanelSheet>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export const Route = createFileRoute("/_chat/$threadId")({
|
|
validateSearch: (search) => parseDiffRouteSearch(search),
|
|
component: ChatThreadRouteView,
|
|
});
|