mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 17:30:56 +08:00
Merge remote-tracking branch 'upstream/main' into codex/s3-archive-ui-docs
This commit is contained in:
@@ -7,6 +7,7 @@ import { Loader2Icon, PlusIcon, SendIcon, XIcon } from "lucide-react";
|
||||
import { HttpCodeBlock, statusTone } from "@/components/http-code-block";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Checkbox } from "@/components/ui/checkbox";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
@@ -23,6 +24,7 @@ type ReplayForm = {
|
||||
url: string;
|
||||
rows: HeaderRow[];
|
||||
body: string;
|
||||
applyRules: boolean;
|
||||
// Headers of the recorded request that the server would not send.
|
||||
dropped: string[];
|
||||
binary: boolean;
|
||||
@@ -40,6 +42,7 @@ function initialForm(exchange: TrafficExchange, raw: string): ReplayForm {
|
||||
url: exchange.url,
|
||||
rows: toRows(sent),
|
||||
body: parsed.binary ? "" : parsed.body,
|
||||
applyRules: false,
|
||||
dropped: notSent.filter((n) => n.toLowerCase() !== "host"),
|
||||
binary: parsed.binary,
|
||||
bodyPreviewOnly: parsed.bodyPreviewOnly,
|
||||
@@ -83,6 +86,7 @@ export function ReplayPanel({
|
||||
// Answers for an older exchange are dropped.
|
||||
const generation = React.useRef(0);
|
||||
const methodListId = React.useId();
|
||||
const applyRulesId = React.useId();
|
||||
|
||||
React.useEffect(() => {
|
||||
generation.current++;
|
||||
@@ -111,6 +115,7 @@ export function ReplayPanel({
|
||||
headers: splitReplayHeaders(form.rows).sent,
|
||||
body: form.body,
|
||||
body_base64: false,
|
||||
apply_rules: form.applyRules,
|
||||
})
|
||||
.then((r) => {
|
||||
if (gen !== generation.current) return;
|
||||
@@ -230,10 +235,20 @@ export function ReplayPanel({
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Button onClick={send} disabled={!canSend}>
|
||||
{sending ? <Loader2Icon className="animate-spin" /> : <SendIcon />}
|
||||
{t("Send")}
|
||||
</Button>
|
||||
<div className="flex flex-wrap items-center gap-4">
|
||||
<Button onClick={send} disabled={!canSend}>
|
||||
{sending ? <Loader2Icon className="animate-spin" /> : <SendIcon />}
|
||||
{t("Send")}
|
||||
</Button>
|
||||
<label htmlFor={applyRulesId} className="flex cursor-pointer items-center gap-2 text-xs select-none">
|
||||
<Checkbox
|
||||
id={applyRulesId}
|
||||
checked={form.applyRules}
|
||||
onCheckedChange={(checked) => update({ applyRules: checked === true })}
|
||||
/>
|
||||
<span>{t("Apply capture rewrite rules")}</span>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="space-y-1 rounded-md border border-red-500/40 bg-red-500/10 p-3 text-xs">
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
ArrowUpNarrowWideIcon,
|
||||
ChevronLeftIcon,
|
||||
ChevronRightIcon,
|
||||
DownloadIcon,
|
||||
EraserIcon,
|
||||
FilterXIcon,
|
||||
ListChecksIcon,
|
||||
@@ -14,11 +15,14 @@ import {
|
||||
RadioTowerIcon,
|
||||
SearchIcon,
|
||||
Trash2Icon,
|
||||
UploadIcon,
|
||||
XIcon,
|
||||
} from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
import { HttpCodeBlock } from "@/components/http-code-block";
|
||||
import { LinkTrafficDialog } from "@/components/link-traffic-dialog";
|
||||
import { Alert, AlertDescription } from "@/components/ui/alert";
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
@@ -44,7 +48,7 @@ import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip
|
||||
import { api } from "@/lib/api";
|
||||
import { setVisibleInterval } from "@/lib/polling";
|
||||
import { useStoredSortPreference } from "@/lib/sort-preference";
|
||||
import type { TrafficDetail, TrafficExchange, TrafficHost, TrafficResp } from "@/lib/types";
|
||||
import type { TrafficDetail, TrafficExchange, TrafficHARImportResult, TrafficHost, TrafficResp } from "@/lib/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { intlLocale, t } from "@/i18n";
|
||||
|
||||
@@ -135,6 +139,7 @@ export default function TrafficPage() {
|
||||
const [path, setPath] = React.useState("");
|
||||
const [pathQ, setPathQ] = React.useState("");
|
||||
const [statusFilter, setStatusFilter] = React.useState("all");
|
||||
const [rewrittenFilter, setRewrittenFilter] = React.useState("all");
|
||||
const [respMin, setRespMin] = React.useState("");
|
||||
const [respMinQ, setRespMinQ] = React.useState("");
|
||||
const [respMax, setRespMax] = React.useState("");
|
||||
@@ -158,6 +163,13 @@ export default function TrafficPage() {
|
||||
const [deleting, setDeleting] = React.useState(false);
|
||||
const [reloadTick, setReloadTick] = React.useState(0); // manual refetch trigger
|
||||
|
||||
// HAR 1.2 import/export state
|
||||
const harInputRef = React.useRef<HTMLInputElement | null>(null);
|
||||
const [harImporting, setHarImporting] = React.useState(false);
|
||||
const [harExporting, setHarExporting] = React.useState(false);
|
||||
const [harSummary, setHarSummary] = React.useState<TrafficHARImportResult | null>(null);
|
||||
const [harError, setHarError] = React.useState<string | null>(null);
|
||||
|
||||
// Debounce both filters so we don't refetch on every keystroke.
|
||||
React.useEffect(() => {
|
||||
const t = setTimeout(() => setHostQ(host.trim()), 300);
|
||||
@@ -184,13 +196,15 @@ export default function TrafficPage() {
|
||||
return () => clearTimeout(t);
|
||||
}, [respMax]);
|
||||
|
||||
const hasAdvancedFilter = Boolean(bodyQ || pathQ || respMinQ || respMaxQ) || statusFilter !== "all";
|
||||
const hasAdvancedFilter =
|
||||
Boolean(bodyQ || pathQ || respMinQ || respMaxQ) || statusFilter !== "all" || rewrittenFilter !== "all";
|
||||
const resetAdvancedFilters = () => {
|
||||
setBody("");
|
||||
setBodyQ("");
|
||||
setPath("");
|
||||
setPathQ("");
|
||||
setStatusFilter("all");
|
||||
setRewrittenFilter("all");
|
||||
setRespMin("");
|
||||
setRespMinQ("");
|
||||
setRespMax("");
|
||||
@@ -201,7 +215,7 @@ export default function TrafficPage() {
|
||||
// biome-ignore lint/correctness/useExhaustiveDependencies: these values intentionally trigger a page reset.
|
||||
React.useEffect(() => {
|
||||
setPage(0);
|
||||
}, [hostQ, queryQ, method, size, bodyQ, pathQ, statusFilter, respMinQ, respMaxQ, sort]);
|
||||
}, [hostQ, queryQ, method, size, bodyQ, pathQ, statusFilter, rewrittenFilter, respMinQ, respMaxQ, sort]);
|
||||
|
||||
// Load the current page. Auto-refresh only on page 0 (newest) so paging back
|
||||
// through history isn't yanked out from under the user.
|
||||
@@ -214,6 +228,7 @@ export default function TrafficPage() {
|
||||
body: bodyQ,
|
||||
path: pathQ,
|
||||
status: statusFilter,
|
||||
rewritten: rewrittenFilter,
|
||||
respMin: respMinQ,
|
||||
respMax: respMaxQ,
|
||||
sort: sort.field,
|
||||
@@ -243,7 +258,21 @@ export default function TrafficPage() {
|
||||
alive = false;
|
||||
stopPolling();
|
||||
};
|
||||
}, [page, size, hostQ, method, queryQ, bodyQ, pathQ, statusFilter, respMinQ, respMaxQ, sort, reloadTick]);
|
||||
}, [
|
||||
page,
|
||||
size,
|
||||
hostQ,
|
||||
method,
|
||||
queryQ,
|
||||
bodyQ,
|
||||
pathQ,
|
||||
statusFilter,
|
||||
rewrittenFilter,
|
||||
respMinQ,
|
||||
respMaxQ,
|
||||
sort,
|
||||
reloadTick,
|
||||
]);
|
||||
|
||||
// Delete traffic for the current host filter (substring) or the checked
|
||||
// hosts (exact batch), then refetch.
|
||||
@@ -349,6 +378,76 @@ export default function TrafficPage() {
|
||||
: { field, direction: "desc" },
|
||||
);
|
||||
|
||||
const handleHarFileChange = async (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = event.target.files?.[0];
|
||||
event.target.value = "";
|
||||
if (!file) return;
|
||||
setHarError(null);
|
||||
setHarSummary(null);
|
||||
if (file.size > 32 * 1024 * 1024) {
|
||||
const msg = t("HAR file exceeds the 32 MiB limit");
|
||||
setHarError(msg);
|
||||
toast.error(msg);
|
||||
return;
|
||||
}
|
||||
setHarImporting(true);
|
||||
try {
|
||||
const raw = await file.text();
|
||||
const res = await api.trafficImportHAR(raw);
|
||||
setHarSummary(res);
|
||||
const summary = t("HAR import complete: {imported} imported, {skipped} skipped, {failed} failed", {
|
||||
imported: res.imported,
|
||||
skipped: res.skipped,
|
||||
failed: res.failed,
|
||||
});
|
||||
if (res.failed > 0 || res.truncated) {
|
||||
toast.warning(summary);
|
||||
} else {
|
||||
toast.success(summary);
|
||||
}
|
||||
setPage(0);
|
||||
setReloadTick((n) => n + 1);
|
||||
} catch (e) {
|
||||
const msg = t("HAR import failed: {error}", { error: (e as Error).message });
|
||||
setHarError(msg);
|
||||
toast.error(msg);
|
||||
} finally {
|
||||
setHarImporting(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleHarExport = async () => {
|
||||
setHarExporting(true);
|
||||
try {
|
||||
const meta = await api.trafficExportHAR({
|
||||
host: hostQ,
|
||||
method,
|
||||
q: queryQ,
|
||||
body: bodyQ,
|
||||
path: pathQ,
|
||||
status: statusFilter,
|
||||
rewritten: rewrittenFilter,
|
||||
respMin: respMinQ,
|
||||
respMax: respMaxQ,
|
||||
});
|
||||
if (meta.truncated) {
|
||||
toast.warning(
|
||||
t("Exported {exported} of {matched} matching exchanges (truncated: {reason})", {
|
||||
exported: meta.exported,
|
||||
matched: meta.matched,
|
||||
reason: meta.truncatedReason ?? "limit",
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
toast.success(t("Exported {count} exchanges to HAR", { count: meta.exported }));
|
||||
}
|
||||
} catch (e) {
|
||||
toast.error((e as Error).message);
|
||||
} finally {
|
||||
setHarExporting(false);
|
||||
}
|
||||
};
|
||||
|
||||
const exchanges = React.useMemo(() => traffic?.exchanges ?? [], [traffic]);
|
||||
const total = traffic?.total ?? exchanges.length;
|
||||
const pageCount = Math.max(1, Math.ceil(total / size));
|
||||
@@ -378,9 +477,76 @@ export default function TrafficPage() {
|
||||
<span className="text-xs text-muted-foreground tabular-nums">
|
||||
{t("{count} records", { count: traffic?.count ?? 0 })}
|
||||
</span>
|
||||
<input
|
||||
ref={harInputRef}
|
||||
type="file"
|
||||
accept=".har,application/json"
|
||||
className="hidden"
|
||||
onChange={(e) => void handleHarFileChange(e)}
|
||||
/>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="h-8"
|
||||
disabled={harImporting}
|
||||
onClick={() => harInputRef.current?.click()}
|
||||
>
|
||||
{harImporting ? <Loader2Icon className="size-3.5 animate-spin" /> : <UploadIcon className="size-3.5" />}
|
||||
{harImporting ? t("Importing HAR…") : t("Import HAR")}
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="h-8"
|
||||
disabled={harExporting || !traffic?.count}
|
||||
onClick={() => void handleHarExport()}
|
||||
>
|
||||
{harExporting ? <Loader2Icon className="size-3.5 animate-spin" /> : <DownloadIcon className="size-3.5" />}
|
||||
{harExporting ? t("Exporting HAR…") : t("Export HAR")}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{harError ? (
|
||||
<Alert variant="destructive" className="flex items-center justify-between gap-2 py-2">
|
||||
<AlertDescription className="text-xs">{harError}</AlertDescription>
|
||||
<Button variant="ghost" size="icon-xs" aria-label={t("Dismiss")} onClick={() => setHarError(null)}>
|
||||
<XIcon className="size-3.5" />
|
||||
</Button>
|
||||
</Alert>
|
||||
) : null}
|
||||
|
||||
{harSummary ? (
|
||||
<Alert className="flex items-start justify-between gap-2 py-2">
|
||||
<AlertDescription className="space-y-1 text-xs">
|
||||
<p>
|
||||
{t("HAR import complete: {imported} imported, {skipped} skipped, {failed} failed", {
|
||||
imported: harSummary.imported,
|
||||
skipped: harSummary.skipped,
|
||||
failed: harSummary.failed,
|
||||
})}
|
||||
</p>
|
||||
{harSummary.truncated ? (
|
||||
<p className="text-amber-600 dark:text-amber-400">
|
||||
{t("HAR import stopped early at the 32 MiB decoded body cap.")}
|
||||
</p>
|
||||
) : null}
|
||||
{harSummary.errors && harSummary.errors.length > 0 ? (
|
||||
<ul className="list-disc pl-4 text-muted-foreground">
|
||||
{harSummary.errors.slice(0, 5).map((item) => (
|
||||
<li key={`${item.index}-${item.reason}`} className="font-mono">
|
||||
#{item.index}: {item.reason}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : null}
|
||||
</AlertDescription>
|
||||
<Button variant="ghost" size="icon-xs" aria-label={t("Dismiss")} onClick={() => setHarSummary(null)}>
|
||||
<XIcon className="size-3.5" />
|
||||
</Button>
|
||||
</Alert>
|
||||
) : null}
|
||||
|
||||
{/* Toolbar */}
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<Popover open={pickerOpen} onOpenChange={setPickerOpen}>
|
||||
@@ -598,6 +764,16 @@ export default function TrafficPage() {
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<Select value={rewrittenFilter} onValueChange={setRewrittenFilter}>
|
||||
<SelectTrigger size="sm" className="w-32">
|
||||
<SelectValue placeholder={t("Rewritten")} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">{t("All traffic")}</SelectItem>
|
||||
<SelectItem value="rewritten">{t("Rewritten")}</SelectItem>
|
||||
<SelectItem value="original">{t("Original")}</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<div className="flex items-center gap-1 text-xs text-muted-foreground">
|
||||
<span>{t("Response size")}</span>
|
||||
<Input
|
||||
@@ -721,7 +897,17 @@ export default function TrafficPage() {
|
||||
<MethodBadge method={e.method} />
|
||||
</TableCell>
|
||||
<TableCell className="max-w-0">
|
||||
<span className="block truncate font-mono text-xs">{e.url}</span>
|
||||
<div className="flex items-center gap-1.5">
|
||||
<span className="min-w-0 truncate font-mono text-xs">{e.url}</span>
|
||||
{e.rewritten ? (
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="shrink-0 border-amber-500/30 bg-amber-500/10 px-1.5 py-0 text-[10px] font-medium text-amber-600 dark:text-amber-400"
|
||||
>
|
||||
{t("Rewritten")}
|
||||
</Badge>
|
||||
) : null}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<span className={cn("font-mono text-xs font-semibold tabular-nums", statusTone(e.status))}>
|
||||
@@ -762,6 +948,14 @@ export default function TrafficPage() {
|
||||
<Badge variant="secondary" className={cn("font-mono tabular-nums", statusTone(selected.status))}>
|
||||
{selected.status}
|
||||
</Badge>
|
||||
{selected.rewritten ? (
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="border-amber-500/30 bg-amber-500/10 text-amber-600 dark:text-amber-400"
|
||||
>
|
||||
{t("Rewritten")}
|
||||
</Badge>
|
||||
) : null}
|
||||
<span className="ml-auto text-xs text-muted-foreground tabular-nums">{fmtTime(selected.ts)}</span>
|
||||
</div>
|
||||
<SheetTitle className="break-all font-mono">{selected.host}</SheetTitle>
|
||||
|
||||
Reference in New Issue
Block a user