"use client";
/**
* Desktop welcome window — the reference-first artwork studio front door.
*
* Composer-first and outcome-first: describe what you want, or gather references
* from the gallery, and the workspace opens with that request as the first turn
* of a new native chat (see {@link welcome_message}). The composer targets the
* DEFAULT workspace — the managed root `~/Documents/Grida Projects`, always
* registered — unless the top-left picker aims it at an existing project.
* "Open folder…" and the recents list stay for opening work that already
* exists. Submitting an otherwise empty composer simply enters the
* selected/default workspace without starting a turn.
*
* The home NEVER mints a per-session folder: the agent roots at the workspace
* and decides on turn one whether it becomes a board, a deck or a tree of
* files (guided by its skills), writing only what it decides.
*
* A reference card opens related Library ideas; its Add action drops that
* reference into the composer's tray. A slides template is selected directly
* (references multi-select; a template is single-select, like choosing one
* Keynote theme). Neither action starts work on its own. References fold into
* the first message as plain URLs. A picked template is copied into the
* workspace as its own deck, and the first message names that copy so the
* agent adapts it. The model picker offers the native connections a new chat
* can choose before its workspace opens; the opened workspace selects it.
*/
import {
Suspense,
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
useSyncExternalStore,
} from "react";
import { useRouter, useSearchParams } from "next/navigation";
import { XIcon } from "lucide-react";
import { toast } from "sonner";
import { cn } from "@app/ui/lib/utils";
import {
getDesktopBridge,
useDesktopBridge,
workspaces as workspacesNs,
type Workspace,
} from "@/lib/desktop/bridge";
import { welcome_message } from "@/lib/desktop/welcome-message";
import { imagesMessageContext } from "@/lib/desktop/images-message-context";
import { workflowMessageContext } from "@/lib/desktop/workflow-message-context";
import { dotcanvas } from "dotcanvas";
import { TitleBar } from "@/scaffolds/desktop/chrome/title-bar";
import { AgentComposer } from "@/scaffolds/desktop/shared/agent-composer";
import { AgentPermissionConfirmation } from "@/scaffolds/desktop/shared/agent-permission-confirmation";
import { HomeComposer } from "@/scaffolds/desktop/home/home-composer";
import { NativeAttachmentCard } from "@/scaffolds/desktop/workbench/native-attachment-card";
import { useWorkspaceFileMentions } from "@/scaffolds/desktop/shared/use-workspace-file-mentions";
import { WorkspaceCanvasCreation } from "@/scaffolds/desktop/workbench/workspace-canvas-creation";
import {
ComposerDropArea,
type ComposerCatalog,
type ComposerTransfer,
} from "@/kits/composer";
import {
LibraryExplorerView,
type LibraryExplorerViewHandle,
} from "@/kits/library-explorer";
import { workspaceWorkbenchHref } from "@/scaffolds/desktop/workbench/workspace-workbench-url";
import { resolveLibraryExplorerPage } from "@/scaffolds/desktop/shared/design-search";
import { SlidesTemplateGallery } from "@/scaffolds/desktop/home/slides-template-gallery";
import {
WorkflowGallery,
WorkflowReferenceTray,
} from "@/scaffolds/desktop/home/workflow-gallery";
import {
workflowShowcaseUrl,
type WorkflowShowcase,
} from "@/scaffolds/desktop/home/workflow-showcases";
// `import type` is erased at build — it does NOT pull the loader (and its
// fflate/dotcanvas deps) into the home chunk; the gallery's dynamic `import()`
// stays the sole code-split boundary. The home only holds the picked template
// object and copies its `files` (the unzipped bundle) into the workspace.
import type { SlidesTemplate } from "@/lib/slides-templates";
import { RecentProjectsCommand } from "@/scaffolds/desktop/home/recent-projects-command";
import { WorkspacePicker } from "@/scaffolds/desktop/home/workspace-picker";
import {
ApplicationPreset,
type ApplicationPresetId,
} from "@/scaffolds/desktop/home/application-preset";
import { PresetRail } from "@/scaffolds/desktop/home/preset-rail";
import type { chat_vocabulary } from "@/lib/agent-chat/chat-vocabulary";
import { last_workspace } from "@/lib/desktop/last-workspace";
import { NativeChatController } from "@/lib/agent-chat/native-chat-controller";
const NOOP = () => {};
const WELCOME_LIBRARY_SOURCE = { kind: "browse" } as const;
/**
* The native host adds `?startup=restore-last-workspace` only to the cold-start
* welcome window. Explicit Home navigation, File → New Window, auth callback,
* and macOS re-activation all use plain `/desktop/welcome` and stay here.
*/
export default function DesktopWelcomePage() {
return (
}>
);
}
function DesktopWelcomeRoute() {
const router = useRouter();
const params = useSearchParams();
const shouldRestore = params.get("startup") === "restore-last-workspace";
const onboardingWorkspaceId = params.get("onboardingWorkspace");
const [checking, setChecking] = useState(shouldRestore);
useEffect(() => {
if (!shouldRestore) return;
let cancelled = false;
void last_workspace
.resolve(window.localStorage, {
list: workspacesNs.list,
openFolder: workspacesNs.openFolder,
readdir: workspacesNs.readdir,
})
.catch((err) => {
if (!cancelled) {
console.warn("[welcome] couldn't restore last workspace:", err);
}
return null;
})
.then((target) => {
if (cancelled) return;
if (target) {
router.replace(last_workspace.href(target));
return;
}
// The startup directive is spent. Back and reload should stay on Home.
const url = new URL(window.location.href);
url.searchParams.delete("startup");
window.history.replaceState(null, "", url);
setChecking(false);
});
return () => {
cancelled = true;
};
}, [router, shouldRestore]);
function selectPreset(next: ApplicationPresetId) {
const url = new URL(window.location.href);
if (ApplicationPreset.fromParam(url.searchParams.get("preset")) === next) {
return;
}
if (next === ApplicationPreset.DEFAULT) url.searchParams.delete("preset");
else url.searchParams.set("preset", next);
window.history.pushState(null, "", url);
}
return checking ? (
) : (
);
}
function StartupRestoreScreen() {
return (
Opening last workspace…
);
}
function WelcomeSurface({
initialWorkspaceId,
preset,
onPresetChange,
}: {
initialWorkspaceId: string | null;
preset: ApplicationPresetId;
onPresetChange: (preset: ApplicationPresetId) => void;
}) {
const router = useRouter();
const bridge = useDesktopBridge();
const [composer] = useState(() => new HomeComposer());
const staged = useSyncExternalStore(
composer.subscribe,
composer.read,
composer.read
);
useEffect(() => {
void composer.restore();
return composer.resetPermissionMode;
}, [composer]);
useEffect(() => {
if (staged.permissionError) toast.error(staged.permissionError);
}, [staged.permissionError]);
const filePicker = useRef(null);
const imagePicker = useRef(null);
const galleryRef = useRef(null);
const galleryMotionRef = useRef(null);
// The home's single page-scroll container — everything (composer, gallery)
// scrolls together inside it; the gallery virtualizes against it.
const scrollRef = useRef(null);
const libraryRef = useRef(null);
const [openingFolder, setBusy] = useState(false);
const busy = openingFolder || staged.busy || staged.permissionBusy;
const {
prepareWelcomeFullAccess,
answerWelcomeFullAccess,
startWelcomeFullAccess,
} = bridge?.nativeChat ?? {};
const fullAccess =
prepareWelcomeFullAccess &&
answerWelcomeFullAccess &&
startWelcomeFullAccess
? {
prepare: prepareWelcomeFullAccess,
answer: answerWelcomeFullAccess,
start: startWelcomeFullAccess,
}
: undefined;
const showWorkflows = preset === "workflows";
const showImages = preset === "image";
const presetSpec = ApplicationPreset.byId(preset);
const PresetIcon = presetSpec.icon;
// Recent-projects quick switcher (⌃R, VS Code's "Open Recent"). Keeps the
// home minimal — no persistent recents list.
const [commandOpen, setCommandOpen] = useState(false);
// Null targets the managed default workspace; an explicit choice is honored
// for text, references and templates alike.
const [target, setTarget] = useState(null);
useEffect(() => composer.resetPermissionMode(), [composer, target?.id]);
const composerDraft = useMemo(
() => composer.draftFor(target?.id ?? "default"),
[composer, target?.id]
);
const pendingInitialWorkspaceId = useRef(initialWorkspaceId);
const [defaultWorkspace, setDefaultWorkspace] = useState(
null
);
useEffect(() => {
let active = true;
void workspacesNs
.getDefault()
.then((workspace) => {
if (active) setDefaultWorkspace(workspace);
})
.catch(() => {});
return () => {
active = false;
};
}, []);
const mentions = useWorkspaceFileMentions((target ?? defaultWorkspace)?.id);
const catalog = useMemo(
() => ({ commands: [], mentions }),
[mentions]
);
// References the user has gathered from the gallery (multi-select). They seed
// ONE fresh board on submit; a click in the gallery toggles membership here.
const [pickedRefs, setPickedRefs] = useState<
chat_vocabulary.DesignReference[]
>([]);
const selectedRefIds = useMemo(
() => new Set(pickedRefs.map((p) => p.id)),
[pickedRefs]
);
const [workflowSelection, setWorkflowSelection] = useState<
readonly WorkflowShowcase[]
>([]);
const workflowPicks = useMemo(
() =>
workflowSelection.map((showcase, index) => ({
showcase,
number: index + 1,
})),
[workflowSelection]
);
const selectedWorkflows = showWorkflows ? workflowPicks : undefined;
// The slides template the user picked from the gallery — single-select (you
// start from ONE template, like choosing a Keynote theme). Null = none. It's
// an attachment-style reference, NOT an instant start: the composer send seeds
// its bundle into the session scratch and hands it to the agent (see `start`).
const [pickedTemplate, setPickedTemplate] = useState(
null
);
// A picked template only makes sense in slides mode (the template gallery is
// slides-only). If the user switches the preset away, drop it so a stale
// template chip can't linger over the reference gallery.
useEffect(() => {
if (preset !== "slides") setPickedTemplate(null);
}, [preset]);
// Composer docking: the composer starts as the in-flow hero, then floats as a
// fixed bottom bar once it scrolls out of view — so you can keep writing while
// browsing the gallery. It's the SAME element (its position toggles), so the
// Tiptap draft, focus, and caret survive the transition; `heroHeight` is a
// spacer that holds its old spot so the gallery doesn't jump when it detaches.
const composerRef = useRef(null);
const composerSlotRef = useRef(null);
const [docked, setDocked] = useState(false);
const [heroHeight, setHeroHeight] = useState();
const [workspaces, setWorkspaces] = useState([]);
const previousPreset = useRef(preset);
useLayoutEffect(() => {
if (previousPreset.current === preset) return;
previousPreset.current = preset;
scrollRef.current?.scrollTo({ top: 0, behavior: "instant" });
setDocked(false);
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
const animation = galleryMotionRef.current?.animate(
[
{ opacity: 0.35, transform: "translateX(12px)" },
{ opacity: 1, transform: "translateX(0)" },
],
{ duration: 220, easing: "ease-out", fill: "backwards" }
);
return () => animation?.cancel();
}, [preset]);
const refreshWorkspaces = useCallback(async () => {
const bridge = getDesktopBridge();
if (!bridge) return;
try {
const nextWorkspaces = await workspacesNs.list();
setWorkspaces(nextWorkspaces);
const pendingId = pendingInitialWorkspaceId.current;
if (pendingId) {
const opened = nextWorkspaces.find(
(workspace) => workspace.id === pendingId
);
if (opened) setTarget((current) => current ?? opened);
pendingInitialWorkspaceId.current = null;
}
} catch (err) {
// Non-fatal — the welcome page can still create or open a folder.
console.warn("[welcome] workspaces.list failed:", err);
}
}, []);
useEffect(() => {
void refreshWorkspaces();
// Re-fetch on focus so the File menu's "Open Folder…" surfaces here
// even when the welcome window is already visible.
const onFocus = () => void refreshWorkspaces();
window.addEventListener("focus", onFocus);
return () => window.removeEventListener("focus", onFocus);
}, [refreshWorkspaces]);
// ⌃R toggles the recent-projects palette (Ctrl, NOT Cmd — Cmd+R reloads the
// window in Electron). preventDefault so no reload accelerator fires.
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
if (e.ctrlKey && !e.metaKey && (e.key === "r" || e.key === "R")) {
e.preventDefault();
if (e.repeat) return; // holding the key must not flicker the palette
setCommandOpen((v) => !v);
}
};
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, []);
// Track the composer's natural (in-flow) height so the spacer can reserve it
// when the composer detaches to float — otherwise the flow collapses and the
// gallery jumps up by the composer's height at the dock moment. Freeze the
// in-flow spacer while docked, and keep measuring the floating composer so
// the Workflows gallery can scroll clear of it as the draft grows.
const dockedRef = useRef(docked);
dockedRef.current = docked;
useEffect(() => {
const el = composerRef.current;
if (!el) return;
const scroller = scrollRef.current;
const measure = () => {
const height = el.offsetHeight;
if (!dockedRef.current) setHeroHeight(height);
scroller?.style.setProperty("--welcome-composer-height", `${height}px`);
};
const ro = new ResizeObserver(measure);
ro.observe(el);
measure();
return () => ro.disconnect();
}, []);
// Dock the composer once its hero slot scrolls past the top of the page, and
// restore it in place when scrolled back up. We watch the SLOT (which stays in
// flow as a spacer even while the composer floats) rather than the composer
// itself — the floated bar re-enters the viewport at the bottom, so watching it
// would immediately undock and thrash. Gone above the top edge → float.
useEffect(() => {
const root = scrollRef.current;
const slot = composerSlotRef.current;
if (!root || !slot) return;
const io = new IntersectionObserver(
([entry]) => {
const rootTop = entry.rootBounds?.top ?? 0;
setDocked(
!entry.isIntersecting && entry.boundingClientRect.top <= rootTop
);
},
{ root, threshold: 0 }
);
io.observe(slot);
return () => io.disconnect();
}, []);
// Auto-detect `.canvas` decks/boards among the recents: a folder containing
// `.canvas.json` opens the board file window (`/desktop/file?id=`) instead of
// the file workbench. Probed via the bridge once per list.
const [canvasIds, setCanvasIds] = useState>(new Set());
useEffect(() => {
let cancelled = false;
void (async () => {
const ids = new Set();
// The native reader has finite workers. Leave room for open editors
// while checking every remembered workspace.
for (let index = 0; index < workspaces.length; index += 2) {
if (cancelled) return;
await Promise.all(
workspaces.slice(index, index + 2).map(async (w) => {
try {
const entries = await workspacesNs.readdir(w.id);
if (
entries.some((e) => e.rel_path === dotcanvas.MANIFEST_FILENAME)
)
ids.add(w.id);
} catch {
// Unavailable folders remain available as ordinary workspace rows.
}
})
);
}
if (!cancelled) setCanvasIds(ids);
})();
return () => {
cancelled = true;
};
}, [workspaces]);
// Native connection choices for the first chat. Only choices that do not
// depend on a workspace are offered here; the opened workspace selects the
// chosen one as a new connection. `null` keeps the workspace default.
const modelSelection = useMemo(
() => new NativeChatController.ModelSelection(),
[]
);
const {
profiles,
profileId,
error: profileReadError,
} = useSyncExternalStore(
modelSelection.subscribe,
modelSelection.read,
modelSelection.read
);
useEffect(() => {
composer.initializeEffort(profiles?.newChatDefault?.effort);
}, [composer, profiles?.newChatDefault?.effort]);
const readProfiles = useCallback(() => {
const list = getDesktopBridge()?.nativeChat?.welcomeProfiles;
if (!list) return;
void modelSelection.refresh(list);
}, [modelSelection]);
useEffect(() => {
readProfiles();
// Settings can add a provider key or endpoint in another window.
window.addEventListener("focus", readProfiles);
return () => {
window.removeEventListener("focus", readProfiles);
modelSelection.release();
};
}, [readProfiles, modelSelection]);
// Open a workspace and, when there is one, leave the first message for its
// chat. An empty message enters the workspace without starting a turn.
const start = useCallback(
async (text: string) => {
const template = pickedTemplate;
const references =
template || showWorkflows || showImages ? [] : pickedRefs;
const workflowReferences = selectedWorkflows?.map(
({ number, showcase }) => ({
number,
title: showcase.title,
showcaseUrl: workflowShowcaseUrl(showcase.slug),
sourceUrl: `https://github.com/gridaco/fx/tree/main/examples/${showcase.sourceFolder}`,
templateId: showcase.templateId,
posterUrl: showcase.preview.image,
})
);
const messageContext = showWorkflows
? workflowMessageContext(workflowReferences ?? [])
: showImages
? imagesMessageContext(pickedRefs)
: undefined;
const hasRequest =
text.trim() ||
template ||
references.length ||
messageContext?.displayParts.length ||
composer.read().attachment;
if (hasRequest) {
const chat = getDesktopBridge()?.nativeChat;
if (!chat)
throw new Error(
"Chat isn't available in this version of Grida Desktop."
);
if ((template || messageContext) && !chat.submitMessage)
throw new Error(
"Update Grida Desktop to send this request. Your draft is still here."
);
}
await composer.submit(
{
text,
prompt: ApplicationPreset.prompt(preset, text),
profileId,
target,
references,
messageContext,
...(template && {
template: {
name: template.name,
copy: async (workspaceId: string) => ({
title: template.title,
slides: template.pages.length,
path: await WorkspaceCanvasCreation.copy(workspaceId, template),
}),
},
}),
},
{
defaultWorkspace: workspacesNs.getDefault,
stash: welcome_message.stash,
open: (workspace) => router.push(workspaceWorkbenchHref(workspace)),
}
);
},
[
pickedTemplate,
pickedRefs,
selectedWorkflows,
showWorkflows,
showImages,
target,
profileId,
router,
composer,
preset,
]
);
// A Library card's Add action toggles it in the composer's tray (multi-select)
// instead of starting a board on its own — the user gathers several, then
// starts one board from all of them via the composer / the tray's Start button.
const togglePick = useCallback((pin: chat_vocabulary.DesignReference) => {
setPickedRefs((cur) =>
cur.some((p) => p.id === pin.id)
? cur.filter((p) => p.id !== pin.id)
: [...cur, pin]
);
}, []);
const toggleWorkflowReference = useCallback((showcase: WorkflowShowcase) => {
setWorkflowSelection((current) =>
current.some((pick) => pick.slug === showcase.slug)
? current.filter((pick) => pick.slug !== showcase.slug)
: [...current, showcase]
);
}, []);
// Picking a slides template SELECTS it into the composer (single-select), like
// attaching a reference — it does NOT start a session on its own. Clicking the
// already-picked one clears it. The start (composer send / Enter) copies the
// deck into the workspace and names the copy in the first message; see
// `start`.
const togglePickTemplate = useCallback((template: SlidesTemplate) => {
setPickedTemplate((cur) => (cur?.name === template.name ? null : template));
}, []);
const onOpen = useCallback(async () => {
const bridge = getDesktopBridge();
if (!bridge) {
toast.error("Desktop bridge not available.");
return;
}
try {
setBusy(true);
const ws = await workspacesNs.chooseFolder();
if (!ws) return;
await refreshWorkspaces();
setTarget(ws); // aim the composer at the just-opened project
} catch (err) {
toast.error(err instanceof Error ? err.message : "Couldn't open folder.");
} finally {
setBusy(false);
}
}, [refreshWorkspaces]);
// The whole Home window accepts external files/folders through the same
// intake as the editor. The shared drop zone consumes each gesture once.
const transferAttachment = (event: ComposerTransfer.Event) => {
if (!busy) composer.transfer(event);
};
return (
to h-svh/overflow-hidden, so the home
// owns its layout: one scroll pane with the composer hero on top and the
// reference gallery below. Once the hero scrolls out of view the composer
// detaches into a floating bottom bar (see `docked`) so you can keep
// writing while browsing.
className="flex h-svh w-full flex-col bg-background"
>
{/* Bare drag region — Settings moved to the rail footer below. */}
{/* One page-scroll container — composer hero then gallery scroll
together (no nested scroll); the gallery virtualizes against this. */}
{/* Top-left workspace picker — where a start lands (the managed
~/Documents/Grida Projects root by default, shown as "Default workspace")
or which existing project to aim at. Borderless + friendly;
replaces the old picker-above-composer + "start blank" row. */}
{/* Composer hero — a generous top offset drops the input to roughly
mid-screen, but the space below stays tight so the gallery sits
close beneath it (not an even top/bottom split) and peeks in to
invite the scroll that docks the composer. */}
{/* Composer slot — narrow and centered; the gallery below spans the
wider container. This slot stays in flow and is what the dock
observer watches; once the composer floats it reserves `heroHeight`
so detaching doesn't jump the gallery up. */}
{/* The composer itself. In the hero it's a plain in-flow block; once
docked it becomes a floating card fixed to the bottom, sliding up
on entry. It's the SAME element in both states, so the Tiptap
draft, focus, and caret carry across the transition. */}
{/* Picked-template chip — the slides-mode counterpart of the
references tray: a single attachment-style chip for the deck the
user chose to start from (single-select). Neutral styling (no
accent) — it reads as an attachment, and starting is the
composer's own send. Its X removes; the gallery card also
toggles it. */}
{pickedTemplate && (
)}
{/* Picked-references tray — a bare, compact row of thumbnails:
no panel, no label, no Clear/Start. Membership IS the state, so
each pin just removes on its hover-X, and starting is the
composer's own send. When docked the row floats above the input
in the same stack — same design language, lifted by shadow, not
boxed in a container. */}
{!showWorkflows && pickedRefs.length > 0 && (