Shared Table component

This commit is contained in:
Gregory Schier
2026-03-11 15:51:57 -07:00
parent 3e7d04b2f3
commit 0c52fd03e2
11 changed files with 67 additions and 32 deletions

View File

@@ -11,7 +11,7 @@ import {
TableHeaderCell,
TableRow,
TruncatedWideTableCell,
} from '../components/core/Table';
} from '@yaakapp-internal/ui';
import { activeWorkspaceIdAtom } from '../hooks/useActiveWorkspace';
import { createFastMutation } from '../hooks/useFastMutation';
import { showDialog } from '../lib/dialog';

View File

@@ -6,7 +6,7 @@ import { Checkbox } from './core/Checkbox';
import { IconButton } from './core/IconButton';
import { PlainInput } from './core/PlainInput';
import { HStack, VStack } from './core/Stacks';
import { Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow } from './core/Table';
import { Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow } from '@yaakapp-internal/ui';
interface Props {
workspace: Workspace;

View File

@@ -22,7 +22,7 @@ import { Icon } from '@yaakapp-internal/ui';
import { IconButton } from '../core/IconButton';
import { PlainInput } from '../core/PlainInput';
import { HStack, VStack } from '../core/Stacks';
import { Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow } from '../core/Table';
import { Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow } from '@yaakapp-internal/ui';
const HOLD_KEYS = ['Shift', 'Control', 'Alt', 'Meta'];
const LAYOUT_INSENSITIVE_KEYS = [

View File

@@ -26,7 +26,7 @@ import { InlineCode } from '../core/InlineCode';
import { Link } from '../core/Link';
import { PlainInput } from '../core/PlainInput';
import { HStack } from '../core/Stacks';
import { Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow } from '../core/Table';
import { Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow } from '@yaakapp-internal/ui';
import { TabContent, Tabs } from '../core/Tabs/Tabs';
import { EmptyStateText } from '../EmptyStateText';
import { SelectFile } from '../SelectFile';

View File

@@ -1,105 +0,0 @@
import classNames from 'classnames';
import type { ReactNode } from 'react';
export function Table({
children,
className,
scrollable,
}: {
children: ReactNode;
className?: string;
scrollable?: boolean;
}) {
return (
<div className={classNames('w-full', scrollable && 'h-full overflow-y-auto')}>
<table
className={classNames(
className,
'w-full text-sm mb-auto min-w-full max-w-full',
'border-separate border-spacing-0',
scrollable && '[&_thead]:sticky [&_thead]:top-0 [&_thead]:z-10',
)}
>
{children}
</table>
</div>
);
}
export function TableBody({ children }: { children: ReactNode }) {
return (
<tbody className="[&>tr:not(:last-child)>td]:border-b [&>tr:not(:last-child)>td]:border-b-surface-highlight">
{children}
</tbody>
);
}
export function TableHead({ children, className }: { children: ReactNode; className?: string }) {
return (
<thead
className={classNames(
className,
'bg-surface [&_th]:border-b [&_th]:border-b-surface-highlight',
)}
>
{children}
</thead>
);
}
export function TableRow({ children }: { children: ReactNode }) {
return <tr>{children}</tr>;
}
export function TableCell({
children,
className,
align = 'left',
}: {
children: ReactNode;
className?: string;
align?: 'left' | 'center' | 'right';
}) {
return (
<td
className={classNames(
className,
'py-2 [&:not(:first-child)]:pl-4 whitespace-nowrap',
align === 'left' ? 'text-left' : align === 'center' ? 'text-center' : 'text-right',
)}
>
{children}
</td>
);
}
export function TruncatedWideTableCell({
children,
className,
}: {
children: ReactNode;
className?: string;
}) {
return (
<TableCell className={classNames(className, 'truncate max-w-0 w-full')}>{children}</TableCell>
);
}
export function TableHeaderCell({
children,
className,
}: {
children?: ReactNode;
className?: string;
}) {
return (
<th
className={classNames(
className,
'py-2 [&:not(:first-child)]:pl-4 text-left text-text-subtle',
)}
>
{children}
</th>
);
}

View File

@@ -2,7 +2,7 @@ import { useGit } from '@yaakapp-internal/git';
import { showDialog } from '../../lib/dialog';
import { Button } from '../core/Button';
import { IconButton } from '../core/IconButton';
import { Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow } from '../core/Table';
import { Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow } from '@yaakapp-internal/ui';
import { gitCallbacks } from './callbacks';
import { addGitRemote } from './showAddRemoteDialog';

View File

@@ -8,7 +8,7 @@ import {
TableHeaderCell,
TableRow,
TruncatedWideTableCell,
} from '../core/Table';
} from '@yaakapp-internal/ui';
interface Props {
log: GitCommit[];

View File

@@ -1,7 +1,7 @@
import classNames from 'classnames';
import Papa from 'papaparse';
import { useMemo } from 'react';
import { Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow } from '../core/Table';
import { Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow } from '@yaakapp-internal/ui';
interface Props {
text: string | null;

View File

@@ -11,6 +11,7 @@
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",

View File

@@ -1,6 +1,15 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { type } from '@tauri-apps/plugin-os';
import { HeaderSize } from '@yaakapp-internal/ui';
import {
HeaderSize,
Table,
TableBody,
TableCell,
TableHead,
TableHeaderCell,
TableRow,
TruncatedWideTableCell,
} from '@yaakapp-internal/ui';
import classNames from 'classnames';
import { createStore, Provider, useAtomValue } from 'jotai';
import { StrictMode } from 'react';
@@ -11,6 +20,7 @@ import './main.css';
import { initHotkeys } from './hotkeys';
import { listen, rpc } from './rpc';
import { useRpcQueryWithEvent } from './rpc-hooks';
import type { ProxyHeader } from '@yaakapp-internal/proxy-lib';
import { applyChange, dataAtom, httpExchangesAtom, replaceAll } from './store';
const queryClient = new QueryClient();
@@ -85,36 +95,59 @@ function App() {
</span>
</div>
<div className="text-xs font-mono">
{exchanges.length === 0 ? (
<p className="text-text-subtlest">No traffic yet</p>
) : (
<table className="w-full text-left">
<thead>
<tr className="text-text-subtlest border-b border-border-subtle">
<th className="py-1 pr-3 font-medium">Method</th>
<th className="py-1 pr-3 font-medium">URL</th>
<th className="py-1 pr-3 font-medium">Status</th>
</tr>
</thead>
<tbody>
{exchanges.map((ex) => (
<tr key={ex.id} className="border-b border-border-subtle">
<td className="py-1 pr-3">{ex.method}</td>
<td className="py-1 pr-3 truncate max-w-md">{ex.url}</td>
<td className="py-1 pr-3">{ex.resStatus ?? '—'}</td>
</tr>
))}
</tbody>
</table>
)}
</div>
{exchanges.length === 0 ? (
<p className="text-text-subtlest text-sm">No traffic yet</p>
) : (
<Table scrollable>
<TableHead>
<TableRow>
<TableHeaderCell>Method</TableHeaderCell>
<TableHeaderCell>URL</TableHeaderCell>
<TableHeaderCell>Status</TableHeaderCell>
<TableHeaderCell>Content-Type</TableHeaderCell>
</TableRow>
</TableHead>
<TableBody>
{exchanges.map((ex) => (
<TableRow key={ex.id}>
<TableCell className="font-mono text-xs">{ex.method}</TableCell>
<TruncatedWideTableCell className="font-mono text-xs">
{ex.url}
</TruncatedWideTableCell>
<TableCell>
<StatusBadge status={ex.resStatus} error={ex.error} />
</TableCell>
<TableCell className="text-text-subtle text-xs">
{getContentType(ex.resHeaders)}
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)}
</main>
</div>
</div>
);
}
function StatusBadge({ status, error }: { status: number | null; error: string | null }) {
if (error) return <span className="text-xs text-danger">Error</span>;
if (status == null) return <span className="text-xs text-text-subtlest"></span>;
const color =
status >= 500 ? 'text-danger' : status >= 400 ? 'text-warning' : status >= 300 ? 'text-notice' : 'text-success';
return <span className={classNames('text-xs font-mono', color)}>{status}</span>;
}
function getContentType(headers: ProxyHeader[]): string {
const ct = headers.find((h) => h.name.toLowerCase() === 'content-type')?.value;
if (ct == null) return '—';
// Strip parameters (e.g. "; charset=utf-8")
return ct.split(';')[0]?.trim() ?? ct;
}
createRoot(document.getElementById('root') as HTMLElement).render(
<StrictMode>
<QueryClientProvider client={queryClient}>