All files / components/workspace app.tsx

0% Statements 0/24
0% Branches 0/15
0% Functions 0/6
0% Lines 0/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201                                                                                                                                                                                                                                                                                                                                                                                                                 
"use client";
 
import { useSyncExternalStore } from "react";
import { ProviderSelector } from "@/components/chat/provider-selector";
import { ChatComposer } from "@/components/chat/composer";
import { ChatTranscript } from "@/components/transcript";
import { Badge } from "@/components/ui/badge";
import { Card } from "@/components/ui/card";
import { Toast } from "@/components/ui/toast";
import { AuthPanel } from "@/components/workspace/auth-panel";
import { ThreadSidebar } from "@/components/workspace/sidebar";
import { APP_NAME, APP_HEADER_REVIEW_BADGE_LABEL } from "@/constants/app";
import { CHAT_COMPOSER_COPY } from "@/constants/chat";
import { DEFAULT_PROVIDER_OPTIONS } from "@/constants/provider";
import type { AppRole, MockAuthSession } from "@/lib/auth/session";
import { isProductionLike } from "@/lib/runtime-env";
import type { AIProviderName } from "@/lib/ai-provider";
import { Text } from "@/components/ui/text";
import { getInitialsFromName } from "@/utils/avatar";
import { useWorkspaceApp } from "@/hooks/use-workspace-app";
 
const ALLOWED_PROVIDERS: AIProviderName[] = isProductionLike()
  ? ["openai"]
  : DEFAULT_PROVIDER_OPTIONS;
 
type WorkspaceAppProps = {
  authRole?: AppRole;
  authSessions: Record<AppRole, MockAuthSession>;
};
 
export function WorkspaceApp({
  authRole = "user",
  authSessions,
}: WorkspaceAppProps) {
  const isHydrated = useSyncExternalStore(
    () => () => {},
    () => true,
    () => false,
  );
 
  if (!isHydrated) {
    return (
      <main className="min-h-screen min-h-dvh px-3 py-3 sm:px-5 sm:py-5">
        <div className="mx-auto flex min-h-[calc(100vh-1.5rem)] min-h-[calc(100dvh-1.5rem)] w-full max-w-[1600px] flex-col gap-3 sm:min-h-[calc(100vh-2.5rem)] sm:min-h-[calc(100dvh-2.5rem)] sm:gap-4 lg:flex-row">
          <div className="h-[70vh] w-full rounded-[1.75rem] border border-white/9 bg-panel-sm backdrop-blur-[28px] lg:max-w-sm" />
          <div className="h-[70vh] flex-1 rounded-[1.75rem] border border-white/9 bg-panel-sm backdrop-blur-[28px]" />
        </div>
      </main>
    );
  }
 
  return <WorkspaceAppClient authRole={authRole} authSessions={authSessions} />;
}
 
function WorkspaceAppClient({ authRole, authSessions }: WorkspaceAppProps) {
  const {
    input,
    setInput,
    auth,
    provider,
    messages,
    isLoading,
    canSend,
    requestError,
    quickActions,
    headerTitle,
    headerSubtitle,
    headerHint,
    helperText,
    messagesContainerRef,
    activeThread,
    allThreads,
    switchThread,
    createNewThread,
    deleteThread,
    stop,
    resetChat,
    handleSubmit,
    handlePromptSelect,
    handleToolApproval,
    handleRoleChange,
  } = useWorkspaceApp(authRole ?? "user", authSessions);
 
  const accountPanel = (
    <AuthPanel
      role={auth.role}
      session={auth.session}
      disabled={isLoading}
      onRoleChange={handleRoleChange}
    />
  );
 
  const sidebarProviderPanel = (
    <Card
      variant="panel"
      className="p-4 text-white shadow-[0_8px_30px_rgba(5,10,30,0.25)]"
    >
      <ProviderSelector
        provider={provider}
        allowedProviders={ALLOWED_PROVIDERS}
        withContainer={false}
      />
      {provider.validationError ? (
        <p className="mt-3 rounded-xl border border-red-500/30 bg-red-500/10 px-3 py-2 text-xs text-red-400">
          {provider.validationError}
        </p>
      ) : null}
    </Card>
  );
 
  return (
    <main className="min-h-screen min-h-dvh px-3 py-3 sm:px-5 sm:py-5">
      {provider.successMessage ? (
        <Toast
          message={provider.successMessage}
          variant="success"
          onDismiss={provider.dismissSuccessMessage}
        />
      ) : null}
      <div className="mx-auto flex min-h-[calc(100vh-1.5rem)] min-h-[calc(100dvh-1.5rem)] w-full max-w-[1600px] flex-col gap-3 sm:min-h-[calc(100vh-2.5rem)] sm:min-h-[calc(100dvh-2.5rem)] sm:gap-4 lg:flex-row">
        <ThreadSidebar
          activeThread={activeThread}
          allThreads={allThreads}
          disabled={isLoading}
          accountPanel={accountPanel}
          providerPanel={sidebarProviderPanel}
          onSwitchThread={switchThread}
          onCreateThread={createNewThread}
          onDeleteThread={deleteThread}
          onResetChat={resetChat}
        />
 
        <section className="flex min-h-[70vh] flex-1 flex-col overflow-hidden rounded-[1.75rem] border border-white/9 bg-panel backdrop-blur-[28px] shadow-[0_16px_60px_rgba(7,12,32,0.36)]">
          <header className="border-b border-white/8 bg-app-header px-4 py-5 sm:px-6 lg:px-8 shadow-[0_1px_0_rgba(255,255,255,0.06),0_8px_32px_rgba(0,0,0,0.2)]">
            <div className="mx-auto w-full max-w-3xl">
              <div className="mb-3 flex flex-wrap items-center gap-2">
                <Text as="p" variant="eyebrow">
                  {APP_NAME}
                </Text>
                <Badge size="md" variant="neutral">
                  {auth.session.roleLabel}
                </Badge>
                <Badge size="md" variant="brand">
                  {APP_HEADER_REVIEW_BADGE_LABEL}
                </Badge>
              </div>
 
              <div className="space-y-2">
                <Text
                  as="h2"
                  variant="title"
                  className="tracking-tight sm:text-[1.9rem]"
                >
                  {headerTitle}
                </Text>
                <Text variant="subtitle" className="max-w-2xl">
                  {headerSubtitle}
                </Text>
                {headerHint ? (
                  <Text variant="captionMuted" className="block">
                    {headerHint}
                  </Text>
                ) : null}
              </div>
            </div>
          </header>
 
          <ChatTranscript
            containerRef={messagesContainerRef}
            messages={messages}
            isLoading={isLoading}
            userAvatarUrl={auth.session.avatar}
            userAvatarLabel={`${auth.session.name} avatar`}
            userInitials={getInitialsFromName(auth.session.name)}
            quickActions={quickActions}
            onSelectPrompt={handlePromptSelect}
            onToolApproval={handleToolApproval}
          />
 
          <ChatComposer
            input={input}
            canSend={canSend}
            isLoading={isLoading}
            isProviderReady={provider.isProviderReady}
            inputTooltip={
              !provider.isProviderReady
                ? CHAT_COMPOSER_COPY.verifyProviderTooltip
                : undefined
            }
            helperText={helperText}
            errorMessage={requestError}
            onInputChange={setInput}
            onSubmitAction={handleSubmit}
            onStopAction={stop}
          />
        </section>
      </div>
    </main>
  );
}