246 lines · 10.7 KBRaw
| 1 | import { AgentAvatar, Ago, Badge, cn, Empty, LayerCard, LineBadge, LinkButton } from "@refs/design-system"; |
| 2 | import { ChatCircleIcon, CheckSquareIcon, GitBranchIcon, GitPullRequestIcon, KeyIcon, RobotIcon } from "@refs/design-system/icons"; |
| 3 | import { Link } from "react-router"; |
| 4 | import type { Route } from "./+types/dashboard"; |
| 5 | import { AppShell, PageHeader } from "../components/shell"; |
| 6 | |
| 7 | import { pullLink, taskLink, type OnlineAgent, type PullSummary, type RepoSummary, type TaskSummary } from "@refs/backend/contract"; |
| 8 | import { data } from "react-router"; |
| 9 | import { formText, found, refs as handle, signedIn, nowSeconds } from "../lib/session"; |
| 10 | import { decidePermission } from "../lib/permissions"; |
| 11 | import { Opener, openerOf } from "../components/timeline"; |
| 12 | import { PermissionRequests } from "../components/permission-requests"; |
| 13 | import { Landing } from "../components/landing"; |
| 14 | |
| 15 | export const meta: Route.MetaFunction = ({ loaderData }) => loaderData && "landing" in loaderData |
| 16 | ? [{ title: "refs: the Git host for coding agents" }, { name: "description", content: "Stop reading diffs. Start reading decisions. refs shows you the few choices your agents made that matter, so you can steer, branch or merge in minutes." }] |
| 17 | : [{ title: "Overview · refs" }]; |
| 18 | |
| 19 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 20 | if (!params.ns && !handle(context).viewer.person) return { landing: true as const }; |
| 21 | const refs = signedIn(context); |
| 22 | if (!refs.viewer.namespace) throw data(null, { status: 404 }); |
| 23 | const useTasks = refs.viewer.can.useTasks; |
| 24 | const [repos, assigned] = await Promise.all([refs.repos(), useTasks ? refs.tasks({ status: "open", assignee: "me", page: { limit: 8 } }) : null]); |
| 25 | // ponytail: three queries per repository; add a namespace-wide query when namespaces hold many repositories |
| 26 | const activity = await Promise.all( |
| 27 | repos.map(async (summary) => { |
| 28 | const repo = await refs.repo(summary.name); |
| 29 | const [permissions, agents, pulls] = await Promise.all([repo.permissions(), repo.agents(), useTasks ? repo.pulls({ status: "open" }) : null]); |
| 30 | return { repo: summary.name, permissions, agents, pulls: pulls?.items ?? [] }; |
| 31 | }), |
| 32 | ); |
| 33 | return { |
| 34 | repos, |
| 35 | tasks: assigned?.page.items ?? [], |
| 36 | waiting: activity.flatMap(({ repo, permissions }) => permissions.filter((request) => request.canDecide).map((request) => ({ ...request, repo }))), |
| 37 | agents: activity.flatMap(({ repo, agents }) => agents.map((agent) => ({ ...agent, repo }))), |
| 38 | pulls: activity.flatMap(({ pulls }) => pulls).sort((a, b) => b.createdAt - a.createdAt).slice(0, 10), |
| 39 | now: nowSeconds(), |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | export async function action({ request, context }: Route.ActionArgs) { |
| 44 | const form = await request.formData(); |
| 45 | const repo = await found(signedIn(context).repo(formText(form, "repo"))); |
| 46 | const outcome = await decidePermission(repo, form); |
| 47 | return outcome.ok ? { error: null } : { error: outcome.problem.message }; |
| 48 | } |
| 49 | |
| 50 | const ROW = "flex items-start gap-3 px-4 py-2.5 shadow-[inset_0_-1px_0_var(--hairline)] last:shadow-none"; |
| 51 | |
| 52 | function Section({ icon, title, count, children }: { icon: React.ReactNode; title: string; count?: number; children: React.ReactNode }) { |
| 53 | return ( |
| 54 | <LayerCard> |
| 55 | <LayerCard.Secondary> |
| 56 | {icon} |
| 57 | {title} |
| 58 | {count !== undefined && <span className="font-normal text-faint tabular-nums">{count}</span>} |
| 59 | </LayerCard.Secondary> |
| 60 | {children} |
| 61 | </LayerCard> |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | function Quiet({ children }: { children: React.ReactNode }) { |
| 66 | return <p className="m-0 px-4 py-4 text-compact text-muted">{children}</p>; |
| 67 | } |
| 68 | |
| 69 | function OpenPulls({ pulls, now }: { pulls: PullSummary[]; now: number }) { |
| 70 | return ( |
| 71 | <Section icon={<GitPullRequestIcon />} title="Open pull requests" count={pulls.length}> |
| 72 | {pulls.length === 0 ? ( |
| 73 | <Quiet>No open pull requests. When an agent opens one, it shows here for review.</Quiet> |
| 74 | ) : ( |
| 75 | <ul className="m-0 list-none p-0"> |
| 76 | {pulls.map((pull) => ( |
| 77 | <li key={`${pull.repo}#${pull.number}`} className={ROW}> |
| 78 | <GitPullRequestIcon size={16} className="mt-0.5 shrink-0 text-success" aria-label="Open" /> |
| 79 | <div className="flex min-w-0 flex-1 flex-col gap-0.5"> |
| 80 | <Link to={`${pullLink(pull.repo, pull.number)}/reasoning`} className="truncate font-medium text-ink hover:text-accent-text"> |
| 81 | {pull.title} |
| 82 | </Link> |
| 83 | <span className="flex flex-wrap items-center gap-x-2 gap-y-1 text-compact text-muted"> |
| 84 | <span className="tabular-nums"> |
| 85 | {pull.repo}#{pull.number} |
| 86 | </span> |
| 87 | <LineBadge name={pull.head} className="max-w-48" /> |
| 88 | <Opener by={openerOf(pull)} /> |
| 89 | <Ago at={pull.createdAt} now={now} /> |
| 90 | </span> |
| 91 | </div> |
| 92 | {pull.comments > 0 && ( |
| 93 | <span className="flex shrink-0 items-center gap-1 text-compact text-muted tabular-nums" aria-label={`${pull.comments} comments`}> |
| 94 | <ChatCircleIcon size={14} /> {pull.comments} |
| 95 | </span> |
| 96 | )} |
| 97 | </li> |
| 98 | ))} |
| 99 | </ul> |
| 100 | )} |
| 101 | </Section> |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | function Agents({ agents }: { agents: (OnlineAgent & { repo: string })[] }) { |
| 106 | return ( |
| 107 | <Section icon={<RobotIcon />} title="Agents online" count={agents.length}> |
| 108 | {agents.length === 0 ? ( |
| 109 | <Quiet>No agents are connected. They appear here when a Claude Code, OpenCode or Codex session connects to one of these repositories.</Quiet> |
| 110 | ) : ( |
| 111 | <ul className="m-0 list-none p-0 text-compact"> |
| 112 | {agents.map((agent) => ( |
| 113 | <li key={agent.session} className={cn(ROW, "items-center")}> |
| 114 | <AgentAvatar name={agent.runtime} /> |
| 115 | <div className="flex min-w-0 flex-col"> |
| 116 | <span className="truncate"> |
| 117 | <span className="font-medium">{agent.runtime}</span> <span className="text-muted">for {agent.onBehalfOf.name}</span> |
| 118 | </span> |
| 119 | <span className="truncate text-muted"> |
| 120 | {agent.branch ? <>In {agent.repo}</> : <>Idle in {agent.repo}</>} |
| 121 | {agent.branch && <LineBadge name={agent.branch} className="ml-2" />} |
| 122 | </span> |
| 123 | </div> |
| 124 | </li> |
| 125 | ))} |
| 126 | </ul> |
| 127 | )} |
| 128 | </Section> |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | function RepositoryList({ repos, now }: { repos: RepoSummary[]; now: number }) { |
| 133 | if (repos.length === 0) { |
| 134 | return ( |
| 135 | <Empty |
| 136 | icon={<GitBranchIcon size={40} />} |
| 137 | title="No repositories yet" |
| 138 | description="Create a repository, then push to it with any Git client." |
| 139 | contents={ |
| 140 | <LinkButton variant="primary" href="/new"> |
| 141 | New repository |
| 142 | </LinkButton> |
| 143 | } |
| 144 | /> |
| 145 | ); |
| 146 | } |
| 147 | return ( |
| 148 | <Section icon={<GitBranchIcon />} title="Repositories" count={repos.length}> |
| 149 | <ul className="m-0 list-none p-0"> |
| 150 | {repos.map((repo) => ( |
| 151 | <li key={repo.name} className={cn(ROW, "items-center justify-between gap-4")}> |
| 152 | <div className="flex min-w-0 flex-col"> |
| 153 | <div className="flex items-center gap-2"> |
| 154 | <Link to={`/${repo.name}`} className="truncate font-semibold text-ink hover:text-accent-text"> |
| 155 | {repo.name} |
| 156 | </Link> |
| 157 | <Badge variant="outline">{repo.visibility === "public" ? "Public" : "Private"}</Badge> |
| 158 | </div> |
| 159 | {repo.description && <p className="m-0 truncate text-compact text-muted">{repo.description}</p>} |
| 160 | </div> |
| 161 | <div className="flex shrink-0 items-center gap-4 text-compact text-muted tabular-nums"> |
| 162 | {repo.openPulls > 0 && ( |
| 163 | <Link to={`/${repo.name}/pulls`} className="hidden items-center gap-1 hover:text-accent-text sm:flex" aria-label={`${repo.openPulls} open pull requests`}> |
| 164 | <GitPullRequestIcon size={14} /> {repo.openPulls} |
| 165 | </Link> |
| 166 | )} |
| 167 | {repo.openTasks > 0 && ( |
| 168 | <Link to={`/${repo.name}/tasks`} className="flex items-center gap-1 hover:text-accent-text" aria-label={`${repo.openTasks} open tasks`}> |
| 169 | <CheckSquareIcon size={14} /> {repo.openTasks} |
| 170 | </Link> |
| 171 | )} |
| 172 | <span className="hidden w-24 text-right md:block"> |
| 173 | <Ago at={repo.createdAt} now={now} /> |
| 174 | </span> |
| 175 | </div> |
| 176 | </li> |
| 177 | ))} |
| 178 | </ul> |
| 179 | </Section> |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | function AssignedTasks({ tasks }: { tasks: TaskSummary[] }) { |
| 184 | return ( |
| 185 | <Section icon={<CheckSquareIcon />} title="Assigned to you" count={tasks.length}> |
| 186 | {tasks.length === 0 ? ( |
| 187 | <Quiet>No open tasks are assigned to you.</Quiet> |
| 188 | ) : ( |
| 189 | <ul className="m-0 list-none p-0"> |
| 190 | {tasks.map((task) => ( |
| 191 | <li key={`${task.repo}#${task.number}`} className={cn(ROW, "flex-col gap-0 py-2")}> |
| 192 | <Link to={taskLink(task.repo, task.number)} className="block w-full truncate font-medium text-ink hover:text-accent-text"> |
| 193 | {task.title} |
| 194 | </Link> |
| 195 | <span className="text-compact text-muted tabular-nums"> |
| 196 | {task.repo}#{task.number} |
| 197 | </span> |
| 198 | </li> |
| 199 | ))} |
| 200 | </ul> |
| 201 | )} |
| 202 | </Section> |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | function GitAccess() { |
| 207 | return ( |
| 208 | <Section icon={<KeyIcon />} title="Clone and push"> |
| 209 | <div className="flex flex-col items-start gap-3 p-4"> |
| 210 | <p className="m-0 text-compact text-muted">Git asks for a password the first time you clone or push. Use an access token, with any username.</p> |
| 211 | <LinkButton variant="secondary" href="/settings/tokens"> |
| 212 | Manage access tokens |
| 213 | </LinkButton> |
| 214 | </div> |
| 215 | </Section> |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | export default function Dashboard({ loaderData }: Route.ComponentProps) { |
| 220 | if ("landing" in loaderData) return <Landing />; |
| 221 | const { repos, tasks, waiting, agents, pulls, now } = loaderData; |
| 222 | return ( |
| 223 | <AppShell> |
| 224 | <PageHeader title="Overview" /> |
| 225 | <div className="grid items-start gap-6 lg:grid-cols-[minmax(0,1fr)_340px]"> |
| 226 | <div className="flex min-w-0 flex-col gap-6"> |
| 227 | {waiting.length > 0 && ( |
| 228 | <section className="flex flex-col gap-3" aria-labelledby="waiting"> |
| 229 | <h2 id="waiting" className="m-0 text-base font-semibold"> |
| 230 | Waiting for you |
| 231 | </h2> |
| 232 | <PermissionRequests requests={waiting} now={now} /> |
| 233 | </section> |
| 234 | )} |
| 235 | {repos.length > 0 && <OpenPulls pulls={pulls} now={now} />} |
| 236 | <RepositoryList repos={repos} now={now} /> |
| 237 | </div> |
| 238 | <aside className="flex flex-col gap-6"> |
| 239 | {repos.length > 0 && <Agents agents={agents} />} |
| 240 | <AssignedTasks tasks={tasks} /> |
| 241 | <GitAccess /> |
| 242 | </aside> |
| 243 | </div> |
| 244 | </AppShell> |
| 245 | ); |
| 246 | } |