Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions apps/web/src/components/loader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const pixels = [
[0, 0],
[6, 0],
[12, 0],
[12, 6],
[12, 12],
[6, 12],
[0, 12],
[0, 6],
] as const;

export function PixelLoader() {
return (
<svg viewBox="0 0 16 16" className="size-3" fill="currentColor" shapeRendering="crispEdges" aria-hidden>
{pixels.map(([x, y], index) => (
<rect
key={`${x},${y}`}
x={x}
y={y}
width="4"
height="4"
className="pixel-loader-cell"
style={{ animationDelay: `${index * 120 - 960}ms`, opacity: 1 - index / 10 }}
/>
))}
</svg>
);
}
15 changes: 15 additions & 0 deletions apps/web/src/components/logo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** The pixel C and square dot from the site's brand/logo.svg. */
export function Logo({ className = "" }: { readonly className?: string }) {
return (
<svg
viewBox="0 0 10 10"
fill="currentColor"
shapeRendering="crispEdges"
className={`size-3.5 shrink-0 text-ink ${className}`}
aria-hidden
>
<path d="M0 0h10v2H2v6h8v2H0Z" />
<rect x="8" y="4" width="2" height="2" />
</svg>
);
}
6 changes: 5 additions & 1 deletion apps/web/src/components/ui/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PanelLeftIcon } from "lucide-react";
import { Slot } from "radix-ui";

import { useIsMobile } from "@/hooks/use-mobile";
import { useScrollFade } from "@/hooks/use-scroll-fade";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Separator } from "@/components/ui/separator";
Expand Down Expand Up @@ -437,12 +438,15 @@ function SidebarSeparator({ className, ...props }: React.ComponentProps<typeof S
}

function SidebarContent({ className, ...props }: React.ComponentProps<"div">) {
const viewport = React.useRef<HTMLDivElement>(null);
useScrollFade(viewport);
return (
<div
ref={viewport}
data-slot="sidebar-content"
data-sidebar="content"
className={cn(
"flex min-h-0 flex-1 flex-col gap-2 overflow-auto group-data-[collapsible=icon]:overflow-hidden",
"scroll-fade flex min-h-0 flex-1 flex-col gap-2 overflow-auto [scrollbar-width:none] group-data-[collapsible=icon]:overflow-hidden [&::-webkit-scrollbar]:hidden",
className,
)}
{...props}
Expand Down
31 changes: 31 additions & 0 deletions apps/web/src/hooks/use-scroll-fade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useLayoutEffect, type RefObject } from "react";

export function useScrollFade(viewport: RefObject<HTMLElement | null>) {
useLayoutEffect(() => {
const element = viewport.current;
if (element === null) return;
const update = () => {
element.style.setProperty("--scroll-top", element.scrollTop > 1 ? "transparent" : "black");
element.style.setProperty(
"--scroll-bottom",
element.scrollTop + element.clientHeight < element.scrollHeight - 1 ? "transparent" : "black",
);
};
const resize = new ResizeObserver(update);
const observe = () => {
resize.disconnect();
resize.observe(element);
for (const child of element.children) resize.observe(child);
update();
};
const mutations = new MutationObserver(observe);
mutations.observe(element, { childList: true, subtree: true, characterData: true });
element.addEventListener("scroll", update, { passive: true });
observe();
return () => {
resize.disconnect();
mutations.disconnect();
element.removeEventListener("scroll", update);
};
}, [viewport]);
}
14 changes: 13 additions & 1 deletion apps/web/src/landing/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "@/components/ui/sidebar";
import { isMac, openMode } from "@codeworksh/widget/view";
import { sessionAddress } from "../widgets/session/address";
import { projectsAtom } from "../widgets/session/projects";
import { projectsAtom, runningAtom } from "../widgets/session/projects";
import { focusedAtom } from "../wm/chrome";
import { deskAtom } from "../wm/instances";
import { useOpenElsewhere } from "../wm/open";
Expand All @@ -27,6 +27,7 @@ import { problemAtom } from "../kernel/servers";
import { Empty, EmptyAction } from "./empty";
import { useAddProject } from "../shell/palette";
import { placeAtom } from "./place";
import { PixelLoader } from "@/components/loader";

/**
* Recent sessions across every project, newest first, beside every page. With none yet it invites
Expand All @@ -35,6 +36,8 @@ import { placeAtom } from "./place";
export function SessionsSidebar() {
const result = useAtomValue(projectsAtom);
const problem = useAtomValue(problemAtom);
const running = useAtomValue(runningAtom);
const working = new Set(running.values());
const [query, setQuery] = useState("");
const openElsewhere = useOpenElsewhere();
const [place, setPlace] = useAtom(placeAtom);
Expand Down Expand Up @@ -151,6 +154,15 @@ export function SessionsSidebar() {
onAuxClick={(event) => open(session, event)}
>
<span className="flex min-w-0 items-center gap-1.5">
{working.has(session.id) && (
<span
role="status"
aria-label="Agent working"
className="flex shrink-0 text-ink-muted"
>
<PixelLoader />
</span>
)}
<span className="min-w-0 flex-1 truncate text-body font-semibold leading-snug text-ink">
{session.title}
</span>
Expand Down
30 changes: 5 additions & 25 deletions apps/web/src/settings/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Link, Outlet, useMatchRoute, useNavigate } from "@tanstack/react-router";
import { Search } from "lucide-react";
import { useEffect, useRef, useState } from "react";
import { useRef, useState } from "react";
import { useScrollFade } from "@/hooks/use-scroll-fade";

import {
Sidebar,
Expand Down Expand Up @@ -90,31 +91,10 @@ export function SettingsSidebar() {

export function SettingsLayout() {
const viewport = useRef<HTMLElement>(null);
const content = useRef<HTMLDivElement>(null);
useEffect(() => {
const element = viewport.current;
const body = content.current;
if (element === null || body === null) return;
const update = () => {
element.style.setProperty("--scroll-top", element.scrollTop > 1 ? "transparent" : "black");
element.style.setProperty(
"--scroll-bottom",
element.scrollTop + element.clientHeight < element.scrollHeight - 1 ? "transparent" : "black",
);
};
const observer = new ResizeObserver(update);
observer.observe(element);
observer.observe(body);
element.addEventListener("scroll", update, { passive: true });
update();
return () => {
observer.disconnect();
element.removeEventListener("scroll", update);
};
}, []);
useScrollFade(viewport);
return (
<main ref={viewport} className="settings-scroll h-full min-h-0 min-w-0 overflow-auto">
<div ref={content}>
<main ref={viewport} className="scroll-fade h-full min-h-0 min-w-0 overflow-auto">
<div>
<Outlet />
</div>
</main>
Expand Down
10 changes: 9 additions & 1 deletion apps/web/src/shell/switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { AppWindow, X } from "lucide-react";
import { useEffect, useRef } from "react";

import { isMac } from "@codeworksh/widget/view";
import { PixelLoader } from "@/components/loader";
import { registry } from "../widgets";
import { runningAtom } from "../widgets/session/projects";
import { chromeAtom, chromeOf, focusedAtom, tabOwnerAtom } from "../wm/chrome";
import { deskAtom, deskKey } from "../wm/instances";
import {
Expand Down Expand Up @@ -32,6 +34,7 @@ export function Switcher({ workspaces, active }: SwitcherProps) {
const chrome = useAtomValue(chromeAtom);
const focusedOn = useAtomValue(focusedAtom);
const owners = useAtomValue(tabOwnerAtom);
const running = useAtomValue(runningAtom);
const navigate = useNavigate();
const remove = useAtomSet(deleteWorkspace, { mode: "promise" });
// No confirm, like a browser tab; closing the one on screen moves to a neighbour first.
Expand Down Expand Up @@ -103,6 +106,9 @@ export function Switcher({ workspaces, active }: SwitcherProps) {
{workspaces.map((workspace, index) => {
const current = workspace.id === active?.id;
const tab = showing(workspace);
const working = instances.some(
(instance) => (instance.tags & workspaceTag(workspace)) !== 0 && running.has(instance.id),
);
return (
<div key={workspace.id} className="group/tab relative flex h-tab w-56 min-w-16 shrink basis-56">
<Link
Expand All @@ -116,9 +122,11 @@ export function Switcher({ workspaces, active }: SwitcherProps) {
}`}
>
<span
role={working ? "status" : undefined}
aria-label={working ? "Agent working" : undefined}
className={`grid size-4 shrink-0 place-items-center ${current ? "text-ink" : "text-ink-muted"}`}
>
<AppWindow className="size-3.5" strokeWidth={1.75} aria-hidden />
{working ? <PixelLoader /> : <AppWindow className="size-3.5" strokeWidth={1.75} aria-hidden />}
</span>
<span className="truncate text-xs font-medium">{tab}</span>
</Link>
Expand Down
26 changes: 24 additions & 2 deletions apps/web/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,28 @@
}
}

@media (prefers-reduced-motion: no-preference) {
.pixel-loader-cell {
animation: pixel-loader 960ms steps(1, end) infinite;
}
}

@keyframes pixel-loader {
0% {
opacity: 1;
}
12.5% {
opacity: 0.65;
}
25% {
opacity: 0.4;
}
37.5%,
100% {
opacity: 0.2;
}
}

@keyframes zen-spin {
to {
transform: rotate(360deg);
Expand Down Expand Up @@ -552,8 +574,8 @@
color: color-mix(in srgb, var(--cw-ink) 80%, transparent);
}

/* Only soften edges that have more settings content beyond them. */
.settings-scroll {
/* Only soften edges that have more scrollable content beyond them. */
.scroll-fade {
mask-image: linear-gradient(
to bottom,
var(--scroll-top, black),
Expand Down
20 changes: 19 additions & 1 deletion apps/web/src/widgets/session/projects.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import { useAtomValue } from "@effect/atom-react";
import { useAtomSet, useAtomValue } from "@effect/atom-react";
import { AsyncResult, Atom } from "effect/reactivity";
import { useEffect } from "react";

import { Server } from "../../kernel/rpc";
import { codeworkKey } from "../../kernel/servers";

/** Every project with its sessions, as the sidebar lists them; refresh it once a new session exists. */
export const projectsAtom = Server.query("projects.list", undefined, { reactivityKeys: codeworkKey });

/** Running session frames, keyed by instance so closing one never clears another's activity. */
export const runningAtom = Atom.make<ReadonlyMap<string, string>>(new Map()).pipe(Atom.keepAlive);

export function useSessionRunning(instanceId: string, sessionId: string, running: boolean) {
const setRunning = useAtomSet(runningAtom);
useEffect(() => {
if (!running) return;
setRunning((current) => new Map(current).set(instanceId, sessionId));
return () =>
setRunning((current) => {
const next = new Map(current);
next.delete(instanceId);
return next;
});
}, [instanceId, sessionId, running, setRunning]);
}

const listedAtom = Atom.family((id: string) =>
Atom.make((get) => {
const result = get(projectsAtom);
Expand Down
23 changes: 15 additions & 8 deletions apps/web/src/widgets/session/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Atom } from "effect/reactivity";
import { ChevronRight, CircleDashed, X } from "lucide-react";
import { createContext, memo, use, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";

import brand from "../../assets/codework.png";
import { Logo } from "@/components/logo";
import { ComposerNotice, type Notice } from "@/components/banner";
import { Monogram } from "@/components/monogram";
import { useModelName, useModelsNotice } from "@/components/model";
Expand All @@ -15,7 +15,7 @@ import { SessionComposer } from "./composer";
import { Markdown } from "./markdown";
import { Message } from "./message";
import { Queue } from "./queue";
import { projectsAtom, useListedSession } from "./projects";
import { projectsAtom, useListedSession, useSessionRunning } from "./projects";
import { type Info, Json, type ThreadRef, threadKey, topics, type Update } from "./schema";

const decodeReply = Schema.decodeUnknownOption(Json.Reply);
Expand Down Expand Up @@ -61,6 +61,11 @@ type State =

type Ready = Extract<State, { readonly status: "ready" }>;

const isRunning = (state: Ready) => {
const last = state.messages.at(-1);
return state.running ?? (last?.role === "assistant" && last.state === "draft");
};

/*
* The threads opened lately, as last shown: switching back to one renders it on the first frame
* while it catches up, instead of an empty frame until its first page loads.
Expand Down Expand Up @@ -378,6 +383,7 @@ function Conversation({
: loaded.queued,
};
}, [loaded, sent]);
useSessionRunning(instanceId, thread.sessionId, state.status === "ready" && isRunning(state));
const sending = useCallback(
(prompt: { readonly id: string; readonly text: string; readonly createdAt: number }) =>
setSent((current) => [
Expand Down Expand Up @@ -463,8 +469,7 @@ function Conversation({
const view = stand ?? state;
if (view.status === "loading") return <Loading slow={slow} />;
if (view.status === "failed") return <p className="p-3 text-ink-muted">{view.message}</p>;
const last = view.messages.at(-1);
const running = view.running ?? (last?.role === "assistant" && last.state === "draft");
const running = isRunning(view);
return (
<div ref={surface} inert={stand !== undefined} className="relative flex h-full flex-col">
<StreamedContext value={message}>
Expand Down Expand Up @@ -1107,10 +1112,12 @@ function WorkLog({
<ChevronRight className="size-3.5 rotate-90 text-ink-muted" strokeWidth={1.75} aria-hidden />
) : (
<>
<img
src={brand}
alt=""
className={`size-3.5 shrink-0 ${expandable ? "transition-opacity duration-150 ease-out group-hover:opacity-0 group-focus-visible:opacity-0" : ""}`}
<Logo
className={
expandable
? "transition-opacity duration-150 ease-out group-hover:opacity-0 group-focus-visible:opacity-0"
: ""
}
/>
{expandable && (
<ChevronRight
Expand Down
Loading