import { t } from "../lib/console-text";
import { ArtifactSessionPage } from "../components/session/artifact-session-page";
import type {
WorkspaceArtifactContentResponse,
WorkspaceArtifactDetailResponse,
} from "@opengeni/sdk";
import { SiteView, artifactLoadErrorMessage, artifactLoadErrorView } from "../../artifacts";
import { loadSiteSnapshot } from "../../sites";
import { SiteConversations } from "../components/artifacts/site-conversations";
import { SiteData } from "../components/artifacts/site-data";
import { SitePublishButton, sitePublicationHeader } from "../components/artifacts/site-publishing";
import {
SiteSettings,
hasSiteSettings,
useSiteManagement,
} from "../components/artifacts/site-settings";
import { Link, useNavigate, useSearch } from "../lib/navigation";
import {
ArchiveIcon,
ArchiveRestoreIcon,
LinkIcon,
PanelsTopLeftIcon,
PlusIcon,
RotateCcwIcon,
SparklesIcon,
} from "lucide-react";
import { useCallback, useEffect, useRef, useState } from "react";
import { toast } from "../lib/toast";
import { ArtifactLibrary } from "../components/artifacts/artifact-library";
import { useCopyArtifactLink } from "../components/artifacts/copy-artifact-link";
import {
ARTIFACT_DETAIL_FRAME,
ArtifactKindTile,
useArtifactsBackLink,
} from "../components/artifacts/artifact-page-chrome";
import { artifactKinds, type ArtifactKind } from "../lib/artifact-catalog";
import { invalidateArtifactCatalog, useArtifactCatalog } from "../lib/use-artifact-catalog";
import { useArtifactCatalogMutationInvalidation } from "../lib/use-artifact-catalog-mutation-invalidation";
import { Button } from "../../ui/button";
import { ContentPage } from "../../ui/content-layout";
import { DetailPage, DetailPageBody, DetailPageHeader } from "../../ui/detail-page";
import { DetailSection } from "../../ui/detail-sheet";
import { LineTabs, LineTabsContent, LineTabsList, LineTabsTrigger } from "../../ui/line-tabs";
import { ListRow, RowList } from "../../ui/list-row";
import { MetaChip } from "../../ui/meta-chip";
import { Notice } from "../../ui/notice";
import { RowButton } from "../../ui/page-actions";
import { PageHeader } from "../../ui/page-header";
import { RelativeTime } from "../../ui/relative-time";
import { Skeleton } from "../../ui/skeleton";
import { StatusBadge } from "../../ui/status-badge";
import { useAppContext } from "../app-context";
import { ARTIFACT_ACCESS_REQUIRED, isArtifactReadDenied } from "../lib/artifact-access";
import { useConsoleSiteToolBridge, useConsoleSiteViewer } from "../lib/console-site";
import { readyNewChat } from "../lib/ready-new-chat";
import { userErrorText } from "../../ui/api-error";
import { newArtifactPrompt } from "../lib/site-edit-prompt";
import { hasWorkspacePermission, lacksWorkspacePermission } from "../lib/permissions";
import {
artifactLibraryFilters,
artifactLibraryPositionKey,
artifactLibrarySearch,
readArtifactLibraryPosition,
useArtifactLibraryPosition,
} from "../lib/artifact-library-navigation";
import type { ArtifactCatalogFilters } from "../lib/artifact-catalog";
import type { ArtifactReturnSearch, SiteDetailTab } from "../lib/routes";
// The Site view opens the same publish form through this route's code,
// loaded when needed, so the form stays with the Site's Settings tab.
export { SitePublishingPage } from "../components/artifacts/site-publishing-page";
function formatSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
function SiteLoadError({
error,
accessDenied,
onRetry,
}: {
error: unknown;
accessDenied: boolean;
onRetry: () => void;
}) {
if (accessDenied)
return {ARTIFACT_ACCESS_REQUIRED};
const view = artifactLoadErrorView(t, error, "site");
return (
Retry
) : undefined
}
>
{artifactLoadErrorMessage(t, view)}
);
}
export function ArtifactsRoute({
workspaceId,
artifactId,
fromSession,
}: {
workspaceId: string;
artifactId?: string | undefined;
fromSession?: string | undefined;
}) {
const search = useSearch({ strict: false }) as ArtifactReturnSearch;
// Straight to the Site view, without this page's chrome first.
if (artifactId && opensSiteView(search))
return ;
return artifactId ? (
) : (
);
}
function ArtifactListRoute({
workspaceId,
fromSession,
}: {
workspaceId: string;
fromSession?: string | undefined;
}) {
const context = useAppContext();
const navigate = useNavigate();
const search = useSearch({ strict: false });
const filters = artifactLibraryFilters(search);
const setFilters = (next: ArtifactCatalogFilters) => {
void navigate({
to: "/workspaces/$workspaceId/artifacts",
params: { workspaceId },
search: artifactLibrarySearch(next, fromSession),
replace: true,
resetScroll: false,
});
};
const positionKey = artifactLibraryPositionKey(workspaceId, context.accessKeyVersion, filters);
// Freeze the restore count for this query; loading another page must not
// recreate the catalog's request lifecycle.
const retained = useRef({
key: positionKey,
client: context.client,
pages: readArtifactLibraryPosition(context.client, positionKey).pages,
});
if (retained.current.key !== positionKey || retained.current.client !== context.client)
retained.current = {
key: positionKey,
client: context.client,
pages: readArtifactLibraryPosition(context.client, positionKey).pages,
};
const [empty, setEmpty] = useState(false);
const catalog = useArtifactCatalog(
context.client,
workspaceId,
filters,
context.accessKeyVersion,
retained.current.pages,
);
const position = useArtifactLibraryPosition(
context.client,
positionKey,
// The viewer may have repopulated an invalidated cache with only its first
// page. Do not consume scroll restoration before the retained pages reload.
catalog.loading ||
Boolean(catalog.error) ||
Boolean(catalog.nextCursor && catalog.pages < retained.current.pages),
catalog.items.length,
catalog.pages,
);
const invalidateAfterMutation = useArtifactCatalogMutationInvalidation(
context.client,
workspaceId,
invalidateArtifactCatalog,
);
const canCreate = hasWorkspacePermission(context.accessContext, workspaceId, "sessions:create");
const canPin = hasWorkspacePermission(context.accessContext, workspaceId, "artifacts:publish");
// The catalog quietly omits Sites and editable artifacts without `artifacts:read`.
const artifactKindsHidden = lacksWorkspacePermission(
context.accessContext,
workspaceId,
"artifacts:read",
);
// Readies a new chat in the composer; the person says what to make and sends it.
const [preparingNew, setPreparingNew] = useState(false);
const startSession = async () => {
if (preparingNew) return;
setPreparingNew(true);
try {
await readyNewChat(context.client, workspaceId, newArtifactPrompt(filters.kind));
await navigate({ to: "/workspaces/$workspaceId/sessions", params: { workspaceId } });
} catch (nextError) {
toast.error("Couldn't open a new chat", { description: userErrorText(nextError) });
} finally {
setPreparingNew(false);
}
};
const newArtifact = canCreate ? (
) : null;
return (
setFilters({ ...filters, kind: kind as ArtifactKind | "all" })}
className="min-w-0"
>
}
title="Artifacts"
description="Sites, images, documents and files made with Opengeni"
actions={empty && filters.kind === "all" ? null : newArtifact}
tabs={
{artifactKinds.map(([kind, label]) => (
{label}
))}
}
/>
{
await context.client.updateArtifactPin(workspaceId, item.kind, item.id, pinned);
invalidateAfterMutation();
await catalog.refresh();
}
: undefined
}
/>
);
}
type SiteTab = "site" | SiteDetailTab;
export function ArtifactDetailRoute({
embedded = false,
embeddedView = "site",
...props
}: {
workspaceId: string;
artifactId: string;
fromSession?: string | undefined;
embedded?: boolean | undefined;
/** In the session workbench: the Site itself, or its Settings. */
embeddedView?: "site" | "settings" | undefined;
/** The workbench header's reload and full screen, which replace the frame toolbar. */
embeddedControls?: EmbeddedSiteControls | undefined;
}) {
const { embeddedControls, ...route } = props;
if (!embedded) return ;
return embeddedView === "settings" ? (
) : (
);
}
export type EmbeddedSiteControls = {
reloadToken: number;
fullScreen: boolean;
onFullScreenChange: (fullScreen: boolean) => void;
};
/**
* The session dock's Site Settings: the Site page's Settings tab, so the Site
* can be published, shared or archived without leaving the session.
*/
function EmbeddedSiteSettings({
workspaceId,
artifactId,
}: {
workspaceId: string;
artifactId: string;
}) {
const context = useAppContext();
const [artifact, setArtifact] = useState(
null,
);
const [error, setError] = useState(null);
const readAbort = useRef(null);
const load = useCallback(async () => {
readAbort.current?.abort();
const abort = new AbortController();
readAbort.current = abort;
try {
setError(null);
const detail = await context.client.getWorkspaceArtifact(workspaceId, artifactId, {
signal: abort.signal,
});
if (!abort.signal.aborted) setArtifact(detail.artifact);
} catch (nextError) {
if (abort.signal.aborted) return;
setArtifact(null);
setError(nextError);
}
}, [artifactId, workspaceId, context.client]);
useEffect(() => {
setArtifact(null);
void load();
return () => readAbort.current?.abort();
}, [load]);
const management = useSiteManagement({
workspaceId,
siteId: artifactId,
artifact,
onArtifactChanged: setArtifact,
onStatusChanged: load,
});
return (
{error ? (
void load()}
/>
) : !artifact ? (
Loading Site settings
) : (
)}
{management.audience.dialog}
);
}
/** The session dock's Site view: the shared SiteView with console tool access. */
function EmbeddedSiteDetail({
workspaceId,
artifactId,
controls,
}: {
workspaceId: string;
artifactId: string;
controls?: EmbeddedSiteControls | undefined;
}) {
const { client } = useAppContext();
const viewer = useConsoleSiteViewer();
const toolBridge = useConsoleSiteToolBridge(workspaceId);
return (
);
}
/**
* A Site opens in the Site view, with its chat beside it: a link, a copied
* address or a notification naming `/artifacts/:id` lands there. Its details
* (Versions, Conversations, Data, Settings) stay here, by tab.
*/
function ArtifactDetailOrSite(props: {
workspaceId: string;
artifactId: string;
fromSession?: string | undefined;
}) {
const search = useSearch({ strict: false }) as ArtifactReturnSearch;
return opensSiteView(search) ? : ;
}
/** `/artifacts/:id` without a tab (or the publish view) is the Site itself. */
function opensSiteView(search: ArtifactReturnSearch): boolean {
return search.view !== "publish" && search.tab === undefined;
}
function OpenSiteView({ workspaceId, artifactId }: { workspaceId: string; artifactId: string }) {
const navigate = useNavigate();
useEffect(() => {
void navigate({
to: "/workspaces/$workspaceId/sites/$siteId",
params: { workspaceId, siteId: artifactId },
replace: true,
});
}, [artifactId, navigate, workspaceId]);
return (
Opening Site
);
}
function ArtifactDetailPage({
workspaceId,
artifactId,
fromSession,
}: {
workspaceId: string;
artifactId: string;
fromSession?: string | undefined;
}) {
const context = useAppContext();
const canPublish = hasWorkspacePermission(
context.accessContext,
workspaceId,
"artifacts:publish",
);
const navigate = useNavigate();
const back = useArtifactsBackLink(workspaceId, fromSession);
const copyLink = useCopyArtifactLink(workspaceId);
const search = useSearch({ strict: false }) as ArtifactReturnSearch;
// The tab is in the address, so Settings (from the workbench or the old
// Publish link) and the other tabs can be linked to and survive a reload.
// The Site itself is the Site view, with its chat beside it.
const tab: SiteDetailTab = search.view === "publish" ? "settings" : (search.tab ?? "versions");
const openSiteView = (startNew = false) =>
void navigate({
to: "/workspaces/$workspaceId/sites/$siteId",
params: { workspaceId, siteId: artifactId },
...(startNew ? { search: { new: true } } : {}),
});
const setTab = (next: SiteTab) =>
next === "site"
? openSiteView()
: void navigate({
to: "/workspaces/$workspaceId/artifacts/$artifactId",
params: { workspaceId, artifactId },
search: { ...search, view: undefined, tab: next },
replace: true,
resetScroll: false,
});
const [detail, setDetail] = useState(null);
const [content, setContent] = useState | null>(null);
const [error, setError] = useState(null);
const [busyVersion, setBusyVersion] = useState(null);
const readAbort = useRef(null);
const load = useCallback(async () => {
readAbort.current?.abort();
const abort = new AbortController();
readAbort.current = abort;
try {
setError(null);
const snapshot = await loadSiteSnapshot(context.client, workspaceId, artifactId, {
signal: abort.signal,
includeArchivedContent: true,
});
if (abort.signal.aborted) return;
setDetail(snapshot.detail);
setContent(snapshot.content);
} catch (nextError) {
if (abort.signal.aborted) return;
setDetail(null);
setContent(null);
setError(nextError);
}
}, [artifactId, workspaceId, context.client]);
useEffect(() => {
setDetail(null);
setContent(null);
void load();
return () => readAbort.current?.abort();
}, [load]);
// Edit with Opengeni opens the Site view with a new conversation beside
// the Site, its composer focused: the person says what to change and it
// goes live there.
const startEditSession = () => {
if (!canPublish || !detail || detail.artifact.status === "archived") return;
openSiteView(true);
};
const rollback = async (versionId: string) => {
const current = detail?.artifact.currentVersion;
if (
!canPublish ||
!current ||
current.id === versionId ||
detail?.artifact.status === "archived"
)
return;
setBusyVersion(versionId);
try {
await context.client.rollbackWorkspaceArtifact(workspaceId, artifactId, {
versionId,
expectedCurrentVersionId: current.id,
reason: `Restored from the artifact history by ${context.authSession?.user?.name ?? "a workspace member"}`,
idempotencyKey: crypto.randomUUID(),
});
invalidateArtifactCatalog(context.client, workspaceId);
toast.success("Artifact version restored");
await load();
} catch (nextError) {
toast.error("Couldn't restore version", {
description: userErrorText(nextError),
});
} finally {
setBusyVersion(null);
}
};
const archived = detail?.artifact.status === "archived";
const management = useSiteManagement({
workspaceId,
siteId: artifactId,
artifact: detail?.artifact ?? null,
onArtifactChanged: (artifact) =>
setDetail((current) => (current ? { ...current, artifact } : current)),
onStatusChanged: load,
});
const { manageData, publication, audience, statusBusy, setSiteStatus } = management;
if (error) {
return (
void load()}
/>
);
}
// Content is null once loaded when the Site has no version this person can
// open; that is a final state, not a reason to keep showing the skeleton.
if (!detail) {
return (
Loading Site
);
}
const artifact = detail.artifact;
const toolCount = content?.requestedTools.length ?? 0;
const publicationHeader = sitePublicationHeader(publication.load);
const showSettings = hasSiteSettings(management);
const shownTab: SiteTab =
(tab === "settings" && !showSettings) || (tab === "data" && !manageData) ? "versions" : tab;
return (
setTab(value as SiteTab)}
// Site leaves the page for the Site view: arrowing past it must
// not, so a tab opens on Enter, Space or a click.
activationMode="manual"
>
}
title={artifact.title}
chips={
<>
{audience.chip}
{publicationHeader.chip}
{archived ? (
Archived
) : null}
>
}
meta={[
artifact.currentVersion ? `Version ${artifact.currentVersion.revision}` : null,
publicationHeader.meta,
toolCount > 0 ? `Uses ${toolCount} ${toolCount === 1 ? "tool" : "tools"}` : null,
,
]}
actions={
<>
{canPublish ? audience.shareButton : null}
void copyLink({ kind: "site", id: artifactId })}>
Copy link
{canPublish ? (
<>
{manageData && !archived ? (
setTab("settings")}
/>
) : null}
{archived ? (
) : (
)}
>
) : null}
>
}
tabs={
Site
Versions
Conversations
{manageData ? Data : null}
{showSettings ? Settings : null}
}
/>
{artifact.description || archived ? (
{artifact.description ? (
{artifact.description}
) : null}
{archived ? (
} title="This Site is archived">
It's unpublished. Its source and versions are kept: restore it to edit it or bring
back an earlier version.
) : null}