Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Merge the workspace shell: navigation and phone shell, g1t as orchestrator, agents in roles with audience-checked reads, reactions and custom emoji, live notifications and browser push, the homepage tour (agents 0002, chat 0002) | 1 | import { useEffect } from "react"; |
| 2 | import { redirect, useNavigate } from "react-router"; | |
| Chat and workspace agents: channels, DMs and named agents you talk to | 3 | |
| 4 | import type { Route } from "./+types/index"; | |
| 5 | import { ChatUnavailable, NoChannels } from "../../../components/chat/empty"; | |
| Merge the workspace shell: navigation and phone shell, g1t as orchestrator, agents in roles with audience-checked reads, reactions and custom emoji, live notifications and browser push, the homepage tour (agents 0002, chat 0002) | 6 | import { ChatSidebar } from "../../../components/chat/sidebar"; |
| 7 | import { NewMessageButton } from "../../../components/chat/actions"; | |
| 8 | import { isPhone } from "../../../lib/phone"; | |
| Chat and workspace agents: channels, DMs and named agents you talk to | 9 | import { channelPath } from "../../../lib/chat"; |
| 10 | import { LAST_CHAT_COOKIE, sidebarOrNull } from "../../../lib/chat.server"; | |
| 11 | import { readCookie } from "../../../lib/mission"; | |
| 12 | import { page } from "../../../lib/meta"; | |
| Merge the workspace shell: navigation and phone shell, g1t as orchestrator, agents in roles with audience-checked reads, reactions and custom emoji, live notifications and browser push, the homepage tour (agents 0002, chat 0002) | 13 | import { chat, workspaceAgents } from "../../../lib/services.server"; |
| Chat and workspace agents: channels, DMs and named agents you talk to | 14 | import { requireUser } from "../../../lib/session.server"; |
| 15 | ||
| 16 | export function meta({ params, ...args }: Route.MetaArgs) { | |
| 17 | return page(args, { title: `Chat · ${params.owner} · g1t` }); | |
| 18 | } | |
| 19 | ||
| 20 | /** | |
| 21 | * Chat opens where it was left: the last conversation open in this | |
| 22 | * workspace, else #general, else the first there is. | |
| 23 | */ | |
| 24 | export async function loader({ params, context, request }: Route.LoaderArgs) { | |
| 25 | const viewer = requireUser(context, request); | |
| 26 | const slug = params.owner.toLowerCase(); | |
| Merge the workspace shell: navigation and phone shell, g1t as orchestrator, agents in roles with audience-checked reads, reactions and custom emoji, live notifications and browser push, the homepage tour (agents 0002, chat 0002) | 27 | // `?agent=<handle>`: the direct message with that agent, opened or made |
| 28 | // (the top bar's Ask g1t, and links to an agent from anywhere). | |
| 29 | const asked = new URL(request.url).searchParams.get("agent"); | |
| 30 | if (asked) { | |
| 31 | const listed = await workspaceAgents.list(slug, viewer).catch(() => null); | |
| 32 | const agent = listed?.ok ? listed.value.find((a) => a.handle === asked.toLowerCase()) : null; | |
| 33 | const dm = agent ? await chat.openDm(slug, viewer, [{ kind: "agent", id: agent.id }]).catch(() => null) : null; | |
| 34 | if (dm?.ok) throw redirect(channelPath(slug, dm.value)); | |
| 35 | } | |
| 36 | // A phone opens on the list of conversations; a wider screen on one. | |
| 37 | const phone = isPhone(request.headers); | |
| Chat and workspace agents: channels, DMs and named agents you talk to | 38 | const last = readCookie(request.headers.get("cookie"), LAST_CHAT_COOKIE); |
| Merge the workspace shell: navigation and phone shell, g1t as orchestrator, agents in roles with audience-checked reads, reactions and custom emoji, live notifications and browser push, the homepage tour (agents 0002, chat 0002) | 39 | const remembered = last && last.startsWith(`/${slug}/-/chat/`) && !last.includes("..") ? last : null; |
| 40 | if (remembered && !phone) throw redirect(remembered); | |
| Chat and workspace agents: channels, DMs and named agents you talk to | 41 | const sidebar = await sidebarOrNull(slug, viewer); |
| Merge the workspace shell: navigation and phone shell, g1t as orchestrator, agents in roles with audience-checked reads, reactions and custom emoji, live notifications and browser push, the homepage tour (agents 0002, chat 0002) | 42 | if (!sidebar) return { unavailable: true, phone, to: null }; |
| Chat and workspace agents: channels, DMs and named agents you talk to | 43 | const entries = sidebar.entries; |
| 44 | const first = | |
| 45 | entries.find((entry) => entry.channel.kind === "channel" && entry.channel.name === "general") ?? | |
| 46 | entries.find((entry) => entry.channel.kind === "channel") ?? | |
| 47 | entries[0]; | |
| Merge the workspace shell: navigation and phone shell, g1t as orchestrator, agents in roles with audience-checked reads, reactions and custom emoji, live notifications and browser push, the homepage tour (agents 0002, chat 0002) | 48 | const to = remembered ?? (first ? channelPath(slug, first.channel) : null); |
| 49 | if (to && !phone) throw redirect(to); | |
| 50 | return { unavailable: false, phone, to }; | |
| Chat and workspace agents: channels, DMs and named agents you talk to | 51 | } |
| 52 | ||
| Merge the workspace shell: navigation and phone shell, g1t as orchestrator, agents in roles with audience-checked reads, reactions and custom emoji, live notifications and browser push, the homepage tour (agents 0002, chat 0002) | 53 | export default function ChatIndex({ loaderData, params }: Route.ComponentProps) { |
| 54 | const navigate = useNavigate(); | |
| 55 | const { to } = loaderData; | |
| 56 | // Told it was a phone, but wider: on to the conversation after all. | |
| 57 | useEffect(() => { | |
| 58 | if (to && window.matchMedia("(min-width: 768px)").matches) navigate(to, { replace: true }); | |
| 59 | }, [to, navigate]); | |
| 60 | return ( | |
| 61 | <> | |
| 62 | {/* A phone: the list of conversations, full screen. */} | |
| 63 | <div className="h-[calc(100dvh-3.5rem-3.5rem-env(safe-area-inset-bottom)-env(safe-area-inset-top))] md:hidden"> | |
| 64 | <ChatSidebar slug={params.owner.toLowerCase()} heading={false} /> | |
| 65 | <div className="fixed right-4 bottom-[calc(3.5rem+env(safe-area-inset-bottom)+1rem)] z-30"> | |
| 66 | <NewMessageButton slug={params.owner.toLowerCase()} variant="fab" /> | |
| 67 | </div> | |
| 68 | </div> | |
| 69 | <div className="max-md:hidden">{loaderData.unavailable ? <ChatUnavailable /> : to ? null : <NoChannels />}</div> | |
| 70 | </> | |
| 71 | ); | |
| Chat and workspace agents: channels, DMs and named agents you talk to | 72 | } |
This file's history is long; its oldest lines are credited to the oldest commit read.