All files / components/workspace app.tsx

86.84% Statements 33/38
57.14% Branches 12/21
66.66% Functions 8/12
91.66% Lines 33/36

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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259    1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x   1x 1x 1x 1x 1x 1x 1x             1x                 7x       7x 7x 21x       7x                                                                                       10x   10x 10x     2x       1x       4x 4x                                                                                                                                                                                                                                                                                                            
"use client";
 
import { useSyncExternalStore, useState } 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, RightSidebar } 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 dynamic from "next/dynamic";
import { useWorkspaceApp } from "@/hooks/use-workspace-app";
import { usePanelResize } from "@/hooks/use-panel-resize";
import { getFilePreviewType } from "@/utils/file-type";
import { FilePreviewPanel } from "@/components/chat/file-preview-panel";
 
const PdfSplitPanel = dynamic(
  () => import("@/components/chat/pdf-split-panel").then((m) => m.PdfSplitPanel),
  { ssr: false },
);
 
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,
  );
 
  Iif (!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] light:border-slate-200 lg:max-w-sm" />
          <div className="h-[70vh] flex-1 rounded-[1.75rem] border border-white/9 bg-panel-sm backdrop-blur-[28px] light:border-slate-200" />
        </div>
      </main>
    );
  }
 
  return <WorkspaceAppClient authRole={authRole} authSessions={authSessions} />;
}
 
function WorkspaceAppClient({ authRole, authSessions }: WorkspaceAppProps) {
  const {
    input,
    setInput,
    auth,
    provider,
    messages,
    isLoading,
    canSend,
    requestError,
    suggestions,
    headerTitle,
    headerSubtitle,
    headerHint,
    helperText,
    messagesContainerRef,
    activeThread,
    allThreads,
    isPendingThread,
    messageFiles,
    switchThread,
    createNewThread,
    deleteThread,
    stop,
    resetChat,
    handleSubmit,
    handlePromptSelect,
    handleToolApproval,
    handleRoleChange,
    handleFileAttached,
  } = useWorkspaceApp(authRole ?? "user", authSessions);
 
  const [previewFile, setPreviewFile] = useState<File | null>(null);
  const { width: pdfWidth, handleMouseDown: handleResizerMouseDown } = usePanelResize();
 
  function handleFilePreview(file: File) {
    setPreviewFile(file);
  }
 
  function handleClosePreview() {
    setPreviewFile(null);
  }
 
  function handleFileAttachedWithPreview(file: File | null) {
    handleFileAttached(file);
    Iif (file === null && previewFile !== null) {
      setPreviewFile(null);
    }
  }
 
 
  const accountPanel = (
    <AuthPanel
      role={auth.role}
      session={auth.session}
      disabled={isLoading}
      onRoleChange={handleRoleChange}
    />
  );
 
  const sidebarProviderPanel = (
    <Card
      variant="panel"
      className="p-4 text-white shadow-navy-md light:text-slate-900 light:shadow-glass-card-light"
    >
      <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 light:border-red-400 light:bg-red-50 light:text-red-600">
          {provider.validationError}
        </p>
      ) : null}
    </Card>
  );
 
  return (
    <main className="h-screen h-dvh overflow-hidden px-3 py-3 sm:px-5 sm:py-5 bg-dot-grid">
      {provider.successMessage ? (
        <Toast
          message={provider.successMessage}
          variant="success"
          onDismiss={provider.dismissSuccessMessage}
        />
      ) : null}
      <div className="mx-auto flex h-full w-full max-w-[1600px] flex-col gap-3 sm:gap-4 lg:flex-row">
        <ThreadSidebar
          activeThread={activeThread}
          allThreads={allThreads}
          isPendingThread={isPendingThread}
          disabled={isLoading}
          onSwitchThread={switchThread}
          onCreateThread={createNewThread}
          onDeleteThread={deleteThread}
          onResetChat={resetChat}
        />
 
        <section className="flex h-full flex-1 flex-col overflow-hidden rounded-[1.75rem] border border-white/9 bg-panel backdrop-blur-[28px] shadow-navy-xl light:border-slate-200 light:shadow-glass-card-light min-w-0">
        <div className="flex h-full flex-1 flex-col min-w-0 overflow-hidden">
          <header className="border-b border-white/8 bg-app-header px-4 py-5 sm:px-6 lg:px-8 shadow-app-header light:border-slate-200 light:shadow-none">
            <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)}
            suggestions={suggestions}
            messageFiles={messageFiles}
            onSelectPrompt={handlePromptSelect}
            onToolApproval={handleToolApproval}
            onFilePreview={handleFilePreview}
          />
 
          <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}
            onFileAttached={handleFileAttachedWithPreview}
            onFilePreview={handleFilePreview}
          />
        </div>
        </section>
 
        {previewFile ? (
          <div
            style={{ width: pdfWidth, minWidth: pdfWidth, maxWidth: pdfWidth }}
            className="hidden lg:flex h-full relative group/resizer"
          >
            <div
              onMouseDown={handleResizerMouseDown}
              className="absolute inset-y-[10%] left-0 w-1 cursor-col-resize z-10 rounded-full opacity-0 group-hover/resizer:opacity-100 transition-opacity bg-white/30 light:bg-slate-400/60"
            />
            {getFilePreviewType(previewFile) === "pdf" ? (
              <PdfSplitPanel file={previewFile} onClose={handleClosePreview} />
            ) : (
              <FilePreviewPanel file={previewFile} type={getFilePreviewType(previewFile)} onClose={handleClosePreview} />
            )}
          </div>
        ) : (
          <RightSidebar
            accountPanel={accountPanel}
            providerPanel={sidebarProviderPanel}
          />
        )}
      </div>
    </main>
  );
}