mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-12-02 04:54:06 +01:00
Detail card, settings window and small fixes for Yaak Proxy (#824)
This commit is contained in:
1 parent
4538123d88
commit
2ca2bc5f00
75 files changed
+4574
-1147
No files matched your search
@@ -116,7 +116,7 @@ export function AutoScroller<T>({
|
||||
className={classNames(
|
||||
"absolute bottom-3 left-1/2 -translate-x-1/2 z-10",
|
||||
"flex items-center gap-1.5 h-6 px-3 rounded-full",
|
||||
"bg-surface border border-border shadow-lg text-text text-xs",
|
||||
"bg-surface border border-border shadow-lg text-text text-xs tabular-nums",
|
||||
"hover:bg-surface-highlight",
|
||||
)}
|
||||
>
|
||||
|
||||
@@ -106,6 +106,18 @@ export interface HttpRequestPaneViewProps {
|
||||
|
||||
/** Tabs only the app has, after the shared ones */
|
||||
extraTabs?: ExtraPaneTab[];
|
||||
|
||||
/** Replaces the URL bar, as the proxy's card shows a captured request's method and URL */
|
||||
urlLine?: ReactNode;
|
||||
/** Tabs with nothing in them read in the subtlest colour */
|
||||
dimEmptyTabs?: boolean;
|
||||
/**
|
||||
* The tab to open on for a request with no tab of its own; with it, a tab chosen for one
|
||||
* request doesn't carry to the next
|
||||
*/
|
||||
defaultTab?: string;
|
||||
/** A tab was chosen */
|
||||
onTabChange?: (tab: string) => void;
|
||||
}
|
||||
|
||||
type HeadersEditorInherited = { name: string; value: string; enabled?: boolean; id?: string }[];
|
||||
@@ -153,6 +165,10 @@ export function HttpRequestPaneView({
|
||||
authTabs,
|
||||
authContent,
|
||||
extraTabs,
|
||||
urlLine,
|
||||
dimEmptyTabs = false,
|
||||
defaultTab,
|
||||
onTabChange,
|
||||
}: HttpRequestPaneViewProps) {
|
||||
const { ErrorBoundary } = useEditorHost();
|
||||
const readOnly = !editable;
|
||||
@@ -168,27 +184,27 @@ export function HttpRequestPaneView({
|
||||
: 0;
|
||||
}
|
||||
|
||||
const headerCount = request.headers.filter((h) => h.name && h.enabled !== false).length;
|
||||
const tabs = useMemo<TabItem[]>(
|
||||
() => [
|
||||
bodyTab ?? {
|
||||
value: REQUEST_TAB_BODY,
|
||||
label: bodyTypeLabel(request.bodyType, request.contentType),
|
||||
empty: dimEmptyTabs && request.bodyType == null,
|
||||
rightSlot: numParams > 0 ? <CountBadge count={numParams} /> : null,
|
||||
},
|
||||
{
|
||||
value: REQUEST_TAB_PARAMS,
|
||||
rightSlot: <CountBadge count={paramPairs.length} />,
|
||||
label: "Params",
|
||||
empty: dimEmptyTabs && paramPairs.length === 0,
|
||||
},
|
||||
...(headersTabs ?? [
|
||||
{
|
||||
value: REQUEST_TAB_HEADERS,
|
||||
label: "Headers",
|
||||
rightSlot: (
|
||||
<CountBadge
|
||||
count={request.headers.filter((h) => h.name && h.enabled !== false).length}
|
||||
/>
|
||||
),
|
||||
empty: dimEmptyTabs && headerCount === 0,
|
||||
rightSlot: <CountBadge count={headerCount} />,
|
||||
},
|
||||
]),
|
||||
...(authTabs ??
|
||||
@@ -205,14 +221,15 @@ export function HttpRequestPaneView({
|
||||
[
|
||||
authTabs,
|
||||
bodyTab,
|
||||
dimEmptyTabs,
|
||||
extraTabs,
|
||||
headerCount,
|
||||
headersTabs,
|
||||
numParams,
|
||||
paramPairs.length,
|
||||
request.auth,
|
||||
request.bodyType,
|
||||
request.contentType,
|
||||
request.headers,
|
||||
],
|
||||
);
|
||||
|
||||
@@ -235,6 +252,7 @@ export function HttpRequestPaneView({
|
||||
style={style}
|
||||
className={classNames(className, "h-full grid grid-rows-[auto_minmax(0,1fr)] grid-cols-1")}
|
||||
>
|
||||
{urlLine ?? (
|
||||
<UrlBar
|
||||
stateKey={`url.${request.id}`}
|
||||
key={urlBarKey}
|
||||
@@ -264,6 +282,7 @@ export function HttpRequestPaneView({
|
||||
forceUpdateKey={urlForceUpdateKey ?? forceUpdateKey}
|
||||
isLoading={isSending}
|
||||
/>
|
||||
)}
|
||||
<Tabs
|
||||
ref={tabsRef}
|
||||
label="Request"
|
||||
@@ -271,6 +290,9 @@ export function HttpRequestPaneView({
|
||||
tabListClassName="mt-1 -mb-1.5"
|
||||
storageKey={tabsStorageKey}
|
||||
activeTabKey={request.id}
|
||||
defaultValue={defaultTab}
|
||||
keyedDefault={defaultTab != null}
|
||||
onChangeValue={onTabChange}
|
||||
>
|
||||
<TabContent value={REQUEST_TAB_AUTH} ErrorBoundary={ErrorBoundary}>
|
||||
{authContent ?? (request.auth != null && <AuthDisplay auth={request.auth} />)}
|
||||
|
||||
@@ -69,6 +69,22 @@ export interface HttpResponsePaneViewProps {
|
||||
infoRows?: ReactElement[];
|
||||
/** The copy button for a body held back for its size */
|
||||
renderCopyButton?: (getText: () => Promise<string | null>) => ReactNode;
|
||||
/**
|
||||
* The request is shown apart, as the proxy's card shows it beside the response: no Request
|
||||
* tab, and Headers and Cookies hold the response's alone
|
||||
*/
|
||||
requestShownApart?: boolean;
|
||||
/** Drawn inside a card the app draws, without a card of its own */
|
||||
bare?: boolean;
|
||||
/** Tabs with nothing in them read in the subtlest colour */
|
||||
dimEmptyTabs?: boolean;
|
||||
/**
|
||||
* The tab to open on for a key with no tab of its own; with it, a tab chosen under one key
|
||||
* doesn't carry to the next
|
||||
*/
|
||||
defaultTab?: string;
|
||||
/** A tab was chosen */
|
||||
onTabChange?: (tab: string) => void;
|
||||
}
|
||||
|
||||
/** Yaak's response pane: a status line over Response, Request, Headers and Cookies tabs */
|
||||
@@ -95,6 +111,11 @@ export function HttpResponsePaneView({
|
||||
renderBodyViewer,
|
||||
infoRows,
|
||||
renderCopyButton,
|
||||
requestShownApart = false,
|
||||
bare = false,
|
||||
dimEmptyTabs = false,
|
||||
defaultTab,
|
||||
onTabChange,
|
||||
}: HttpResponsePaneViewProps) {
|
||||
const { ErrorBoundary } = useEditorHost();
|
||||
const mimeType =
|
||||
@@ -138,9 +159,15 @@ export function HttpResponsePaneView({
|
||||
: []),
|
||||
];
|
||||
|
||||
const noBody = response == null || (response.contentLength ?? 0) === 0;
|
||||
const headerCount = response?.headers.length ?? 0;
|
||||
const cookieCount = requestShownApart
|
||||
? cookieCounts.received
|
||||
: cookieCounts.sent + cookieCounts.received;
|
||||
return [
|
||||
{
|
||||
value: RESPONSE_TAB_BODY,
|
||||
empty: dimEmptyTabs && noBody,
|
||||
options: {
|
||||
value: viewMode,
|
||||
onChange: (v) => onViewModeChange(v === "raw" ? "raw" : "pretty"),
|
||||
@@ -153,40 +180,48 @@ export function HttpResponsePaneView({
|
||||
itemsAfter: itemsAfter.length > 0 ? itemsAfter : undefined,
|
||||
},
|
||||
},
|
||||
{
|
||||
value: RESPONSE_TAB_REQUEST,
|
||||
label: "Request",
|
||||
rightSlot: (response?.requestContentLength ?? 0) > 0 ? <CountBadge count={true} /> : null,
|
||||
},
|
||||
...(requestShownApart
|
||||
? []
|
||||
: [
|
||||
{
|
||||
value: RESPONSE_TAB_REQUEST,
|
||||
label: "Request",
|
||||
rightSlot:
|
||||
(response?.requestContentLength ?? 0) > 0 ? <CountBadge count={true} /> : null,
|
||||
},
|
||||
]),
|
||||
{
|
||||
value: RESPONSE_TAB_HEADERS,
|
||||
label: "Headers",
|
||||
rightSlot: (
|
||||
<CountBadge
|
||||
count={response?.requestHeaders.length ?? 0}
|
||||
count2={response?.headers.length ?? 0}
|
||||
showZero
|
||||
/>
|
||||
empty: dimEmptyTabs && headerCount === 0,
|
||||
rightSlot: requestShownApart ? (
|
||||
<CountBadge count={headerCount} />
|
||||
) : (
|
||||
<CountBadge count={response?.requestHeaders.length ?? 0} count2={headerCount} showZero />
|
||||
),
|
||||
},
|
||||
{
|
||||
value: RESPONSE_TAB_COOKIES,
|
||||
label: "Cookies",
|
||||
rightSlot:
|
||||
cookieCounts.sent > 0 || cookieCounts.received > 0 ? (
|
||||
<CountBadge count={cookieCounts.sent} count2={cookieCounts.received} showZero />
|
||||
) : null,
|
||||
empty: dimEmptyTabs && cookieCount === 0,
|
||||
rightSlot: requestShownApart ? (
|
||||
<CountBadge count={cookieCounts.received} />
|
||||
) : cookieCounts.sent > 0 || cookieCounts.received > 0 ? (
|
||||
<CountBadge count={cookieCounts.sent} count2={cookieCounts.received} showZero />
|
||||
) : null,
|
||||
},
|
||||
...(extraTabs?.map((t) => t.tab) ?? []),
|
||||
];
|
||||
}, [
|
||||
cookieCounts,
|
||||
dimEmptyTabs,
|
||||
extraTabs,
|
||||
mimeType,
|
||||
onCopyAsCurl,
|
||||
onCopyBody,
|
||||
onSaveBody,
|
||||
onViewModeChange,
|
||||
requestShownApart,
|
||||
response,
|
||||
viewMode,
|
||||
]);
|
||||
@@ -197,9 +232,8 @@ export function HttpResponsePaneView({
|
||||
className={classNames(
|
||||
className,
|
||||
"x-theme-responsePane",
|
||||
"max-h-full h-full",
|
||||
"bg-surface rounded-md border border-border-subtle overflow-hidden",
|
||||
"relative",
|
||||
"max-h-full h-full overflow-hidden relative",
|
||||
!bare && "bg-surface rounded-md border border-border-subtle",
|
||||
)}
|
||||
>
|
||||
{response == null ? (
|
||||
@@ -234,6 +268,9 @@ export function HttpResponsePaneView({
|
||||
tabListClassName="mt-0.5 -mb-1.5"
|
||||
storageKey={tabsStorageKey}
|
||||
activeTabKey={activeTabKey}
|
||||
defaultValue={defaultTab}
|
||||
keyedDefault={defaultTab != null}
|
||||
onChangeValue={onTabChange}
|
||||
>
|
||||
<TabContent value={RESPONSE_TAB_BODY} ErrorBoundary={ErrorBoundary}>
|
||||
<ErrorBoundary name="Http Response Viewer">
|
||||
@@ -259,6 +296,7 @@ export function HttpResponsePaneView({
|
||||
</Suspense>
|
||||
</ErrorBoundary>
|
||||
</TabContent>
|
||||
{!requestShownApart && (
|
||||
<TabContent value={RESPONSE_TAB_REQUEST} ErrorBoundary={ErrorBoundary}>
|
||||
<ConfirmLargeResponse
|
||||
contentLength={response.requestContentLength}
|
||||
@@ -277,11 +315,19 @@ export function HttpResponsePaneView({
|
||||
)}
|
||||
</ConfirmLargeResponse>
|
||||
</TabContent>
|
||||
)}
|
||||
<TabContent value={RESPONSE_TAB_HEADERS} ErrorBoundary={ErrorBoundary}>
|
||||
<ResponseHeaders response={response} infoRows={infoRows} />
|
||||
<ResponseHeaders
|
||||
response={response}
|
||||
infoRows={infoRows}
|
||||
responseOnly={requestShownApart}
|
||||
/>
|
||||
</TabContent>
|
||||
<TabContent value={RESPONSE_TAB_COOKIES} ErrorBoundary={ErrorBoundary}>
|
||||
<ResponseCookies response={response} />
|
||||
<ResponseCookies
|
||||
response={response}
|
||||
side={requestShownApart ? "received" : undefined}
|
||||
/>
|
||||
</TabContent>
|
||||
{extraTabs?.map(({ tab, content }) => (
|
||||
<TabContent key={tab.value} value={tab.value} ErrorBoundary={ErrorBoundary}>
|
||||
|
||||
@@ -10,6 +10,8 @@ import type { HttpResponseView } from "./types";
|
||||
|
||||
interface Props {
|
||||
response: HttpResponseView;
|
||||
/** Only the cookies sent or only those received, for a pane that shows one side */
|
||||
side?: "sent" | "received";
|
||||
}
|
||||
|
||||
interface ParsedCookie {
|
||||
@@ -102,7 +104,7 @@ export function parseCookieHeaders({ sent, received }: HttpResponseView["cookieH
|
||||
};
|
||||
}
|
||||
|
||||
export function ResponseCookies({ response }: Props) {
|
||||
export function ResponseCookies({ response, side }: Props) {
|
||||
const { useDetailsBannerStoredValue } = useEditorHost();
|
||||
const { sentCookies, receivedCookies } = useMemo(
|
||||
() => parseCookieHeaders(response.cookieHeaders),
|
||||
@@ -111,6 +113,7 @@ export function ResponseCookies({ response }: Props) {
|
||||
|
||||
return (
|
||||
<div className="overflow-auto h-full pb-4 gap-y-3 flex flex-col pr-0.5">
|
||||
{side !== "received" && (
|
||||
<DetailsBanner
|
||||
defaultOpen
|
||||
useStoredValue={useDetailsBannerStoredValue}
|
||||
@@ -134,7 +137,9 @@ export function ResponseCookies({ response }: Props) {
|
||||
</KeyValueRows>
|
||||
)}
|
||||
</DetailsBanner>
|
||||
)}
|
||||
|
||||
{side !== "sent" && (
|
||||
<DetailsBanner
|
||||
defaultOpen
|
||||
useStoredValue={useDetailsBannerStoredValue}
|
||||
@@ -213,6 +218,7 @@ export function ResponseCookies({ response }: Props) {
|
||||
</div>
|
||||
)}
|
||||
</DetailsBanner>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { format, formatDistanceToNowStrict } from "date-fns";
|
||||
import type { ReactElement } from "react";
|
||||
import { DetailsBanner } from "../DetailsBanner";
|
||||
import { useEditorHost } from "../EditorHost";
|
||||
import { EmptyStateText } from "../EmptyStateText";
|
||||
import { HeadersTable } from "../HeadersTable";
|
||||
import { IconButton } from "../IconButton";
|
||||
import type { KeyValueRowProps } from "../KeyValueRow";
|
||||
@@ -12,10 +13,30 @@ interface Props {
|
||||
response: HttpResponseView;
|
||||
/** More rows for the Info section, after the standard ones, each keyed */
|
||||
infoRows?: ReactElement[];
|
||||
/** Only the response's headers, for a pane whose request and info are shown elsewhere */
|
||||
responseOnly?: boolean;
|
||||
}
|
||||
|
||||
export function ResponseHeaders({ response, infoRows }: Props) {
|
||||
export function ResponseHeaders({ response, infoRows, responseOnly }: Props) {
|
||||
const { openUrl, useDetailsBannerStoredValue } = useEditorHost();
|
||||
if (responseOnly) {
|
||||
return (
|
||||
<div className="overflow-auto h-full pb-4 pr-0.5">
|
||||
{response.headers.length === 0 ? (
|
||||
<EmptyStateText>No Headers</EmptyStateText>
|
||||
) : (
|
||||
<KeyValueRows>
|
||||
{response.headers.map((h, i) => (
|
||||
// oxlint-disable-next-line react/no-array-index-key
|
||||
<KeyValueRow labelColor="info" key={i} label={h.name}>
|
||||
{h.value}
|
||||
</KeyValueRow>
|
||||
))}
|
||||
</KeyValueRows>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className="overflow-auto h-full pb-4 gap-y-3 flex flex-col pr-0.5">
|
||||
<DetailsBanner
|
||||
|
||||
@@ -13,7 +13,12 @@ import {
|
||||
SettingsList,
|
||||
SettingsSection,
|
||||
} from "./SettingRow";
|
||||
import type { SettingsCallbacks, SettingsEditorKeymap, SettingsView } from "./types";
|
||||
import type {
|
||||
SettingsCallbacks,
|
||||
SettingsEditorKeymap,
|
||||
SettingsInterfaceRow,
|
||||
SettingsView,
|
||||
} from "./types";
|
||||
|
||||
const NULL_FONT_VALUE = "__NULL_FONT__";
|
||||
|
||||
@@ -44,6 +49,7 @@ export function SettingsInterface({
|
||||
trailingSections,
|
||||
}: SettingsInterfaceProps) {
|
||||
const { app, values, fonts } = view;
|
||||
const shown = (row: SettingsInterfaceRow) => !view.hiddenInterfaceRows?.includes(row);
|
||||
|
||||
return (
|
||||
<VStack space={1.5} className="mb-4">
|
||||
@@ -125,43 +131,55 @@ export function SettingsInterface({
|
||||
</SettingRow>
|
||||
</SettingsSection>
|
||||
|
||||
<SettingsSection title="Editor">
|
||||
<SettingRowSelect
|
||||
name="editorKeymap"
|
||||
title="Editor keymap"
|
||||
description="Keyboard shortcut preset used by text editors."
|
||||
value={values.editorKeymap}
|
||||
onChange={(editorKeymap) => callbacks.onChange({ editorKeymap })}
|
||||
options={keymaps}
|
||||
/>
|
||||
<SettingRowBoolean
|
||||
title="Wrap editor lines"
|
||||
description="Wrap long lines in request and response editors."
|
||||
checked={values.editorSoftWrap}
|
||||
onChange={(editorSoftWrap) => callbacks.onChange({ editorSoftWrap })}
|
||||
/>
|
||||
<SettingRowBoolean
|
||||
title="Colorize request methods"
|
||||
description="Use method-specific colors for HTTP request methods."
|
||||
checked={values.coloredMethods}
|
||||
onChange={(coloredMethods) => callbacks.onChange({ coloredMethods })}
|
||||
/>
|
||||
</SettingsSection>
|
||||
{(shown("editorKeymap") || shown("editorSoftWrap") || shown("coloredMethods")) && (
|
||||
<SettingsSection title="Editor">
|
||||
{shown("editorKeymap") && (
|
||||
<SettingRowSelect
|
||||
name="editorKeymap"
|
||||
title="Editor keymap"
|
||||
description="Keyboard shortcut preset used by text editors."
|
||||
value={values.editorKeymap}
|
||||
onChange={(editorKeymap) => callbacks.onChange({ editorKeymap })}
|
||||
options={keymaps}
|
||||
/>
|
||||
)}
|
||||
{shown("editorSoftWrap") && (
|
||||
<SettingRowBoolean
|
||||
title="Wrap editor lines"
|
||||
description="Wrap long lines in request and response editors."
|
||||
checked={values.editorSoftWrap}
|
||||
onChange={(editorSoftWrap) => callbacks.onChange({ editorSoftWrap })}
|
||||
/>
|
||||
)}
|
||||
{shown("coloredMethods") && (
|
||||
<SettingRowBoolean
|
||||
title="Colorize request methods"
|
||||
description="Use method-specific colors for HTTP request methods."
|
||||
checked={values.coloredMethods}
|
||||
onChange={(coloredMethods) => callbacks.onChange({ coloredMethods })}
|
||||
/>
|
||||
)}
|
||||
</SettingsSection>
|
||||
)}
|
||||
|
||||
<SettingsSection title="Window">
|
||||
<NativeTitlebarSetting
|
||||
useNativeTitlebar={values.useNativeTitlebar}
|
||||
onApply={callbacks.onApplyNativeTitlebar}
|
||||
/>
|
||||
{view.windowControlsSetting && (
|
||||
<SettingRowBoolean
|
||||
title="Hide window controls"
|
||||
description="Hide the close, maximize, and minimize controls on Windows or Linux."
|
||||
checked={values.hideWindowControls}
|
||||
onChange={(hideWindowControls) => callbacks.onChange({ hideWindowControls })}
|
||||
/>
|
||||
)}
|
||||
</SettingsSection>
|
||||
{(shown("nativeTitlebar") || view.windowControlsSetting) && (
|
||||
<SettingsSection title="Window">
|
||||
{shown("nativeTitlebar") && (
|
||||
<NativeTitlebarSetting
|
||||
useNativeTitlebar={values.useNativeTitlebar}
|
||||
onApply={callbacks.onApplyNativeTitlebar}
|
||||
/>
|
||||
)}
|
||||
{view.windowControlsSetting && (
|
||||
<SettingRowBoolean
|
||||
title="Hide window controls"
|
||||
description="Hide the close, maximize, and minimize controls on Windows or Linux."
|
||||
checked={values.hideWindowControls}
|
||||
onChange={(hideWindowControls) => callbacks.onChange({ hideWindowControls })}
|
||||
/>
|
||||
)}
|
||||
</SettingsSection>
|
||||
)}
|
||||
|
||||
{trailingSections}
|
||||
</SettingsList>
|
||||
|
||||
@@ -106,8 +106,17 @@ export interface SettingsView {
|
||||
feedbackPrompts: boolean;
|
||||
/** Null when this build has no license */
|
||||
license: SettingsLicenseView | null;
|
||||
/** Interface rows this app doesn't act on yet, left out of the tab */
|
||||
hiddenInterfaceRows?: SettingsInterfaceRow[];
|
||||
}
|
||||
|
||||
/** The Interface tab's rows an app may leave out */
|
||||
export type SettingsInterfaceRow =
|
||||
| "editorKeymap"
|
||||
| "editorSoftWrap"
|
||||
| "coloredMethods"
|
||||
| "nativeTitlebar";
|
||||
|
||||
export interface SettingsCallbacks {
|
||||
onChange: (patch: SettingsPatch) => void | Promise<void>;
|
||||
/** Save the native title bar choice and restart, since it only applies at launch */
|
||||
|
||||
@@ -41,6 +41,8 @@ export type TabItem =
|
||||
value: string;
|
||||
label: string;
|
||||
hidden?: boolean;
|
||||
/** Nothing in it: the label reads in the subtlest colour */
|
||||
empty?: boolean;
|
||||
leftSlot?: ReactNode;
|
||||
rightSlot?: ReactNode;
|
||||
}
|
||||
@@ -57,6 +59,8 @@ export type TabItem =
|
||||
export interface TabOptionsItem {
|
||||
value: string;
|
||||
options: Omit<RadioDropdownProps, "children">;
|
||||
/** Nothing in it: the label reads in the subtlest colour */
|
||||
empty?: boolean;
|
||||
leftSlot?: ReactNode;
|
||||
rightSlot?: ReactNode;
|
||||
}
|
||||
@@ -89,6 +93,11 @@ export interface TabsProps {
|
||||
activeTabKey?: string;
|
||||
/** Where the order and active tab under `storageKey` are kept */
|
||||
useStoredValue?: UseStoredValue;
|
||||
/**
|
||||
* A key with no stored tab opens on `defaultValue`, not on the tab last chosen under another
|
||||
* key, and a stored tab no longer offered falls back the same way
|
||||
*/
|
||||
keyedDefault?: boolean;
|
||||
}
|
||||
|
||||
export const Tabs = forwardRef<TabsRef, TabsProps>(function Tabs(
|
||||
@@ -105,6 +114,7 @@ export const Tabs = forwardRef<TabsRef, TabsProps>(function Tabs(
|
||||
storageKey,
|
||||
activeTabKey,
|
||||
useStoredValue: useStoredValueProp,
|
||||
keyedDefault = false,
|
||||
}: TabsProps,
|
||||
forwardedRef: Ref<TabsRef>,
|
||||
) {
|
||||
@@ -131,8 +141,12 @@ export const Tabs = forwardRef<TabsRef, TabsProps>(function Tabs(
|
||||
|
||||
// Get the active tab value - prefer storage (if activeTabKey), then defaultValue, then first tab
|
||||
const storedActiveTab = activeTabKey ? storage?.activeTabs?.[activeTabKey] : undefined;
|
||||
const [internalValue, setInternalValue] = useState<string | undefined>(undefined);
|
||||
const value = storedActiveTab ?? internalValue ?? defaultValue ?? originalTabs[0]?.value;
|
||||
const [internal, setInternal] = useState<{ key?: string; value: string } | undefined>(undefined);
|
||||
const internalValue =
|
||||
keyedDefault && internal?.key !== activeTabKey ? undefined : internal?.value;
|
||||
const offered = (v: string | undefined) =>
|
||||
v != null && (!keyedDefault || originalTabs.some((t) => t.value === v));
|
||||
const value = [storedActiveTab, internalValue, defaultValue].find(offered) ?? originalTabs[0]?.value;
|
||||
|
||||
// Helper to normalize storage (handle migration from old format)
|
||||
const normalizeStorage = useCallback(
|
||||
@@ -144,7 +158,7 @@ export const Tabs = forwardRef<TabsRef, TabsProps>(function Tabs(
|
||||
// Handle tab change - update internal state, storage if we have a key, and call prop callback
|
||||
const onChangeValue = useCallback(
|
||||
async (newValue: string) => {
|
||||
setInternalValue(newValue);
|
||||
setInternal({ key: activeTabKey, value: newValue });
|
||||
if (storageKey && activeTabKey) {
|
||||
await setStorage((s) => {
|
||||
const normalized = normalizeStorage(s);
|
||||
@@ -501,7 +515,15 @@ function TabButton({
|
||||
) : "options" in tab ? (
|
||||
<TabOptionsButton tab={tab} buttonProps={btnProps} isActive={isActive} />
|
||||
) : (
|
||||
<Button leftSlot={tab.leftSlot} rightSlot={tab.rightSlot} {...btnProps}>
|
||||
<Button
|
||||
leftSlot={tab.leftSlot}
|
||||
rightSlot={tab.rightSlot}
|
||||
{...btnProps}
|
||||
className={classNames(
|
||||
btnProps.className,
|
||||
!isActive && "empty" in tab && tab.empty && "text-text-subtlest!",
|
||||
)}
|
||||
>
|
||||
{"label" in tab && tab.label ? tab.label : tab.value}
|
||||
</Button>
|
||||
);
|
||||
@@ -574,6 +596,7 @@ function TabOptionsButton({
|
||||
</div>
|
||||
}
|
||||
{...buttonProps}
|
||||
className={classNames(buttonProps.className, !isActive && tab.empty && "text-text-subtlest!")}
|
||||
>
|
||||
{option && "shortLabel" in option && option.shortLabel
|
||||
? option.shortLabel
|
||||
|
||||
Reference in new issue
Block a user