Files
t3code/apps/web/src/components/settings/IntegrationsSettings.tsx
T
Julius MarmingeandClaude Opus 5 de4c1ee1e1 feat(web): configurable browser defaults in Settings → Integrations
The preview browser always opened in fill mode, at 100% zoom, following the
system appearance. That is rarely what you want if you work in responsive mode
against a handful of known viewports, and there was no way to say otherwise.

Adds a Settings → Integrations page whose first section is Browser, holding the
defaults a preview tab opens at: viewport (fill, responsive, or a device
preset), zoom, and appearance. They apply to every entry point that opens a tab
without stating its own size — a hand-opened browser panel and an agent calling
`preview_open` alike.

The viewport travels with `preview.open` and zoom/appearance with `createTab`,
so a tab is born configured rather than reflowing a frame later.

Agents keep their 1280x800 fallback, but only when no default is configured:
a fill-mode tab would otherwise be sized by whatever the panel happens to be,
and agents assert against what they open.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-15 16:26:16 +02:00

326 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Integrations settings - preferences for surfaces T3 Code embeds rather than
* owns. Browser is the first section: the defaults a preview tab opens at,
* applied to both hand-opened tabs and agent `preview_open` calls that don't
* state their own size.
*
* @module IntegrationsSettings
*/
import {
DEFAULT_BROWSER_VIEWPORT,
DEFAULT_PREVIEW_APPEARANCE,
DEFAULT_PREVIEW_ZOOM_FACTOR,
FILL_PREVIEW_VIEWPORT,
PREVIEW_VIEWPORT_MAX_AREA,
PREVIEW_VIEWPORT_MAX_DIMENSION,
PREVIEW_VIEWPORT_MIN_DIMENSION,
PREVIEW_ZOOM_LEVELS,
type PreviewAppearancePreference,
type PreviewViewportSetting,
} from "@t3tools/contracts";
import { PREVIEW_VIEWPORT_PRESETS } from "@t3tools/shared/previewViewport";
import { useState } from "react";
import { Input } from "../ui/input";
import {
Select,
SelectGroup,
SelectGroupLabel,
SelectItem,
SelectPopup,
SelectTrigger,
SelectValue,
} from "../ui/select";
import { useClientSettings, useUpdatePrimarySettings } from "~/hooks/useSettings";
import {
SettingResetButton,
SettingsPageContainer,
SettingsRow,
SettingsSection,
} from "./settingsLayout";
import { searchableSetting } from "./settingsSearch";
const FILL_VALUE = "fill";
const RESPONSIVE_VALUE = "responsive";
/**
* The size a "Responsive" default falls back to when the user switches away
* from Fill and hasn't typed dimensions yet. Fill has no dimensions to carry
* over, so the picker needs something concrete to seed the inputs with.
*/
const RESPONSIVE_SEED_SIZE = { width: 1280, height: 800 } as const;
const APPEARANCE_LABELS: Readonly<Record<PreviewAppearancePreference, string>> = {
system: "System",
light: "Light",
dark: "Dark",
};
const zoomLabel = (zoomFactor: number) => `${Math.round(zoomFactor * 100)}%`;
const viewportSelectValue = (viewport: PreviewViewportSetting): string => {
if (viewport._tag === "fill") return FILL_VALUE;
if (
viewport._tag === "preset" &&
PREVIEW_VIEWPORT_PRESETS.some((preset) => preset.id === viewport.presetId)
) {
return viewport.presetId;
}
return RESPONSIVE_VALUE;
};
/**
* The trigger renders this rather than a bare `SelectValue`, which would fall
* back to printing the raw stored value ("fill") because the options are built
* inline instead of from an `items` map.
*/
const viewportSelectLabel = (viewport: PreviewViewportSetting): string => {
const value = viewportSelectValue(viewport);
if (value === FILL_VALUE) return "Fill panel";
if (value === RESPONSIVE_VALUE) return "Responsive";
return PREVIEW_VIEWPORT_PRESETS.find((preset) => preset.id === value)?.label ?? "Responsive";
};
const isValidDimension = (value: number) =>
Number.isInteger(value) &&
value >= PREVIEW_VIEWPORT_MIN_DIMENSION &&
value <= PREVIEW_VIEWPORT_MAX_DIMENSION;
function BrowserViewportSetting() {
const viewport = useClientSettings((settings) => settings.browserDefaultViewport);
const updateSettings = useUpdatePrimarySettings();
// Held while typing so a half-entered width (e.g. "12") isn't persisted as a
// rejected value; committed on blur/Enter once both dimensions are valid.
const [draftSize, setDraftSize] = useState<{ width: string; height: string } | null>(null);
const sized = viewport._tag === "fill" ? null : viewport;
const presentedSize = draftSize ?? {
width: String(sized?.width ?? RESPONSIVE_SEED_SIZE.width),
height: String(sized?.height ?? RESPONSIVE_SEED_SIZE.height),
};
const selectViewport = (value: string | null) => {
setDraftSize(null);
if (value === FILL_VALUE) {
updateSettings({ browserDefaultViewport: FILL_PREVIEW_VIEWPORT });
return;
}
if (value === RESPONSIVE_VALUE) {
updateSettings({
browserDefaultViewport: {
_tag: "freeform",
width: sized?.width ?? RESPONSIVE_SEED_SIZE.width,
height: sized?.height ?? RESPONSIVE_SEED_SIZE.height,
},
});
return;
}
const preset = PREVIEW_VIEWPORT_PRESETS.find((candidate) => candidate.id === value);
if (!preset) return;
updateSettings({
browserDefaultViewport: {
_tag: "preset",
width: preset.width,
height: preset.height,
presetId: preset.id,
},
});
};
const commitDraftSize = () => {
if (!draftSize) return;
const width = Number(draftSize.width);
const height = Number(draftSize.height);
setDraftSize(null);
if (
!isValidDimension(width) ||
!isValidDimension(height) ||
width * height > PREVIEW_VIEWPORT_MAX_AREA
) {
return;
}
if (sized && width === sized.width && height === sized.height) return;
// Typing a size means the preset no longer describes it.
updateSettings({ browserDefaultViewport: { _tag: "freeform", width, height } });
};
return (
<SettingsRow
{...searchableSetting("browser-default-viewport")}
description="The viewport a browser tab opens at, for both you and agents. Fill sizes the page to the panel; any other choice opens the device toolbar at that size."
resetAction={
viewport._tag !== DEFAULT_BROWSER_VIEWPORT._tag ? (
<SettingResetButton
label="default browser viewport"
onClick={() => {
setDraftSize(null);
updateSettings({ browserDefaultViewport: DEFAULT_BROWSER_VIEWPORT });
}}
/>
) : null
}
control={
<div className="flex w-full items-center gap-2 sm:w-auto">
<Select value={viewportSelectValue(viewport)} onValueChange={selectViewport}>
<SelectTrigger className="w-full sm:w-44" aria-label="Default browser viewport">
<SelectValue>{viewportSelectLabel(viewport)}</SelectValue>
</SelectTrigger>
<SelectPopup align="end" alignItemWithTrigger={false} className="min-w-64">
<SelectItem value={FILL_VALUE}>Fill panel</SelectItem>
<SelectItem value={RESPONSIVE_VALUE}>Responsive</SelectItem>
<SelectGroup>
<SelectGroupLabel>Standard</SelectGroupLabel>
{PREVIEW_VIEWPORT_PRESETS.map((preset) => (
<SelectItem key={preset.id} value={preset.id}>
<span className="flex w-full items-center justify-between gap-5">
<span>{preset.label}</span>
<span className="text-xs tabular-nums text-muted-foreground">
{preset.detail}
</span>
</span>
</SelectItem>
))}
</SelectGroup>
</SelectPopup>
</Select>
{sized ? (
<form
className="m-0 flex shrink-0 items-center gap-1 border-0 p-0"
aria-label="Default viewport dimensions"
onSubmit={(event) => {
event.preventDefault();
commitDraftSize();
}}
>
<Input
nativeInput
type="number"
inputMode="numeric"
size="sm"
className="w-16"
aria-label="Default viewport width"
min={PREVIEW_VIEWPORT_MIN_DIMENSION}
max={PREVIEW_VIEWPORT_MAX_DIMENSION}
value={presentedSize.width}
onChange={(event) => setDraftSize({ ...presentedSize, width: event.target.value })}
onBlur={commitDraftSize}
/>
<span className="text-xs text-muted-foreground">×</span>
<Input
nativeInput
type="number"
inputMode="numeric"
size="sm"
className="w-16"
aria-label="Default viewport height"
min={PREVIEW_VIEWPORT_MIN_DIMENSION}
max={PREVIEW_VIEWPORT_MAX_DIMENSION}
value={presentedSize.height}
onChange={(event) => setDraftSize({ ...presentedSize, height: event.target.value })}
onBlur={commitDraftSize}
/>
</form>
) : null}
</div>
}
/>
);
}
function BrowserZoomSetting() {
const zoomFactor = useClientSettings((settings) => settings.browserDefaultZoomFactor);
const updateSettings = useUpdatePrimarySettings();
return (
<SettingsRow
{...searchableSetting("browser-default-zoom")}
description="Page zoom applied to new browser tabs."
resetAction={
zoomFactor !== DEFAULT_PREVIEW_ZOOM_FACTOR ? (
<SettingResetButton
label="default browser zoom"
onClick={() =>
updateSettings({ browserDefaultZoomFactor: DEFAULT_PREVIEW_ZOOM_FACTOR })
}
/>
) : null
}
control={
<Select
value={String(zoomFactor)}
onValueChange={(value) => {
const next = PREVIEW_ZOOM_LEVELS.find((level) => String(level) === value);
if (next !== undefined) updateSettings({ browserDefaultZoomFactor: next });
}}
>
<SelectTrigger className="w-full sm:w-40" aria-label="Default browser zoom">
<SelectValue>{zoomLabel(zoomFactor)}</SelectValue>
</SelectTrigger>
<SelectPopup align="end" alignItemWithTrigger={false}>
{PREVIEW_ZOOM_LEVELS.map((level) => (
<SelectItem hideIndicator key={level} value={String(level)}>
{zoomLabel(level)}
</SelectItem>
))}
</SelectPopup>
</Select>
}
/>
);
}
function BrowserAppearanceSetting() {
const appearance = useClientSettings((settings) => settings.browserDefaultAppearance);
const updateSettings = useUpdatePrimarySettings();
return (
<SettingsRow
{...searchableSetting("browser-default-appearance")}
description="The color scheme pages are told to prefer. System follows your OS setting."
resetAction={
appearance !== DEFAULT_PREVIEW_APPEARANCE ? (
<SettingResetButton
label="default browser appearance"
onClick={() => updateSettings({ browserDefaultAppearance: DEFAULT_PREVIEW_APPEARANCE })}
/>
) : null
}
control={
<Select
value={appearance}
onValueChange={(value) => {
if (value === "system" || value === "light" || value === "dark") {
updateSettings({ browserDefaultAppearance: value });
}
}}
>
<SelectTrigger className="w-full sm:w-40" aria-label="Default browser appearance">
<SelectValue>{APPEARANCE_LABELS[appearance]}</SelectValue>
</SelectTrigger>
<SelectPopup align="end" alignItemWithTrigger={false}>
{Object.entries(APPEARANCE_LABELS).map(([value, label]) => (
<SelectItem hideIndicator key={value} value={value}>
{label}
</SelectItem>
))}
</SelectPopup>
</Select>
}
/>
);
}
export function IntegrationsSettingsPanel() {
return (
<SettingsPageContainer>
<SettingsSection id="browser" title="Browser">
<BrowserViewportSetting />
<BrowserZoomSetting />
<BrowserAppearanceSetting />
</SettingsSection>
</SettingsPageContainer>
);
}