Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
import { create } from "@bufbuild/protobuf";
import { ConnectError } from "@connectrpc/connect";
import { cloneDeep, isEqual } from "lodash-es";
import { ShieldCheck, TriangleAlert, X } from "lucide-react";
import {
type ReactNode,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import { useTranslation } from "react-i18next";
import { router, useCurrentRoute } from "@/app/router";
import {
PROJECT_V1_ROUTE_DASHBOARD,
WORKSPACE_ROUTE_CUSTOM_APPROVAL,
WORKSPACE_ROUTE_SQL_REVIEW_CREATE,
WORKSPACE_ROUTE_SQL_REVIEW_DETAIL,
} from "@/app/router/handles";
import { FeatureBadge } from "@/components/FeatureBadge";
import { LabelListEditor } from "@/components/LabelListEditor";
import { PermissionGuard } from "@/components/PermissionGuard";
import { ResourceIdField } from "@/components/ResourceIdField";
import { RouterLink } from "@/components/RouterLink";
import { Button } from "@/components/ui/button";
import { ColorSwatchInput } from "@/components/ui/color-input";
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";
import {
FormControlRow,
FormField,
FormFieldGroup,
FormSection,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { NumberInput } from "@/components/ui/number-input";
import { StickyActionFooter } from "@/components/ui/sticky-action-footer";
import { Switch } from "@/components/ui/switch";
import { Tooltip } from "@/components/ui/tooltip";
import { useProjectByName } from "@/hooks/useProjectByName";
import { useUnsavedChangesGuard } from "@/hooks/useUnsavedChangesGuard";
import { pushNotification } from "@/stores";
import { useAppStore } from "@/stores/app";
import { projectNamePrefix } from "@/stores/modules/v1/common";
import { useSQLReviewStore } from "@/stores/sqlReview";
import { useWorkspaceApprovalSettingStore } from "@/stores/workspaceApprovalSetting";
import type { Permission, SQLReviewPolicy } from "@/types";
import { isDefaultProject } from "@/types";
import { State } from "@/types/proto-es/v1/common_pb";
import {
PolicyResourceType,
PolicyType,
QueryDataPolicySchema,
} from "@/types/proto-es/v1/org_policy_service_pb";
import type { Label } from "@/types/proto-es/v1/project_service_pb";
import {
LabelSchema,
Project_ExecutionRetryPolicySchema,
Project_ExportSettingSchema,
} from "@/types/proto-es/v1/project_service_pb";
import { WorkspaceApprovalSetting_Rule_Source } from "@/types/proto-es/v1/setting_service_pb";
import { PlanFeature } from "@/types/proto-es/v1/subscription_service_pb";
import {
colorToHex,
convertKVListToLabels,
convertLabelsToKVList,
extractProjectResourceName,
hasProjectPermissionV2,
hasWorkspacePermissionV2,
hexToColor,
sqlReviewPolicySlug,
} from "@/utils";
import { sqlReviewV2Enabled } from "@/utils/featureGates";
import { ProjectExportDestinationField } from "./ProjectExportDestinationField";
// ---------------------------------------------------------------------------
// ApprovalFlowIndicator
// ---------------------------------------------------------------------------
function ApprovalFlowIndicator({
source,
}: {
source: WorkspaceApprovalSetting_Rule_Source;
}) {
const { t } = useTranslation();
const [ready, setReady] = useState(false);
useEffect(() => {
if (!hasWorkspacePermissionV2("bb.settings.get")) return;
useWorkspaceApprovalSettingStore
.getState()
.fetchConfig()
.then(() => setReady(true));
}, []);
const status = useMemo((): "source" | "fallback" | "none" => {
if (!ready) return "none";
const store = useWorkspaceApprovalSettingStore.getState();
if (store.getRulesBySource(source).length > 0) return "source";
if (
store.getRulesBySource(
WorkspaceApprovalSetting_Rule_Source.SOURCE_UNSPECIFIED
).length > 0
)
return "fallback";
return "none";
}, [ready, source]);
if (!ready) return null;
const configured = status !== "none";
const tooltipText =
status === "source"
? t("project.settings.issue-related.approval-flow-configured")
: status === "fallback"
? t("project.settings.issue-related.approval-flow-fallback")
: t("project.settings.issue-related.approval-flow-not-configured");
return (
<Tooltip
content={
<div className="flex flex-col gap-y-1">
<span>{tooltipText}</span>
<RouterLink
to={{ name: WORKSPACE_ROUTE_CUSTOM_APPROVAL }}
className="text-accent underline text-left hover:text-accent-hover"
>
{t("project.settings.issue-related.view-approval-flow")}
</RouterLink>
</div>
}
>
{configured ? (
<ShieldCheck className="size-4 text-success" />
) : (
<TriangleAlert className="size-4 text-warning" />
)}
</Tooltip>
);
}
// ---------------------------------------------------------------------------
// Main Page
// ---------------------------------------------------------------------------
export function ProjectSettingsPage() {
const { t } = useTranslation();
const projectsByName = useAppStore((s) => s.projectsByName);
const reviewStore = useSQLReviewStore();
const projectId = useCurrentRoute().params.projectId as string;
const projectName = `${projectNamePrefix}${projectId}`;
// subscribe to re-render on project cache change
void projectsByName;
const project = useProjectByName(projectName);
const isDefault = isDefaultProject(projectName);
// The v1 review policy settings go away with SQL Review V2.
const sqlReviewV2 = sqlReviewV2Enabled();
const hasPermission = useCallback(
(permission: Permission) =>
project ? hasProjectPermissionV2(project, permission) : false,
[project]
);
const allowEdit = useMemo(
() =>
hasPermission("bb.projects.update") && project?.state === State.ACTIVE,
[hasPermission, project]
);
// -----------------------------------------------------------------------
// General state
// -----------------------------------------------------------------------
const [title, setTitle] = useState(project?.title ?? "");
const [labelKVList, setLabelKVList] = useState<
{ key: string; value: string }[]
>(() => convertLabelsToKVList(project?.labels ?? {}, true));
// Sync label state when project labels change externally
const projectLabels = project?.labels;
useEffect(() => {
setLabelKVList(convertLabelsToKVList(projectLabels ?? {}, true));
}, [projectLabels]);
// -----------------------------------------------------------------------
// Security state
// -----------------------------------------------------------------------
const reviewPolicyList = reviewStore.reviewPolicyList;
const currentReviewPolicy = reviewStore.getReviewPolicyByResouce(projectName);
const [pendingReviewPolicy, setPendingReviewPolicy] = useState<
SQLReviewPolicy | undefined
>(undefined);
const [enforceReview, setEnforceReview] = useState(false);
const [showReviewDialog, setShowReviewDialog] = useState(false);
const queryDataPolicy = useAppStore((s) =>
s.getQueryDataPolicyByParent(projectName)
);
const getInitialMaxRows = useCallback(() => {
const rows = Number(queryDataPolicy?.maximumResultRows ?? 0);
return rows < 0 ? 0 : rows;
}, [queryDataPolicy]);
// `null` represents a transiently empty input while the user is typing;
// coerced to 0 on save.
const [maxRows, setMaxRows] = useState<number | null>(() =>
getInitialMaxRows()
);
const [allowRequestRole, setAllowRequestRole] = useState(
project?.allowRequestRole ?? false
);
const [allowJustInTimeAccess, setAllowJustInTimeAccess] = useState(
project?.allowJustInTimeAccess ?? false
);
const hasQueryPolicyFeature = useAppStore((s) =>
s.hasInstanceFeature(PlanFeature.FEATURE_QUERY_POLICY)
);
const hasSharePointExportFeature = useAppStore((s) =>
s.hasFeature(PlanFeature.FEATURE_SHAREPOINT_EXPORT)
);
// -----------------------------------------------------------------------
// Export destination state
// -----------------------------------------------------------------------
const savedExportDestinationUrl =
project?.exportSetting?.sharepoint?.url ?? "";
const [exportDestinationUrl, setExportDestinationUrl] = useState(
savedExportDestinationUrl
);
const [exportDestinationError, setExportDestinationError] = useState("");
const handleExportDestinationChange = useCallback((value: string) => {
setExportDestinationUrl(value);
setExportDestinationError("");
}, []);
// -----------------------------------------------------------------------
// Issue-related state
// -----------------------------------------------------------------------
const [issueLabels, setIssueLabels] = useState<Label[]>(() =>
cloneDeep(project?.issueLabels ?? [])
);
const [forceIssueLabels, setForceIssueLabels] = useState(
project?.forceIssueLabels ?? false
);
const [enforceIssueTitle, setEnforceIssueTitle] = useState(
project?.enforceIssueTitle ?? false
);
const [enforceSqlReview, setEnforceSqlReview] = useState(
project?.enforceSqlReview ?? false
);
const [allowSelfApproval, setAllowSelfApproval] = useState(
project?.allowSelfApproval ?? false
);
const [allowLastPlanEditorApproval, setAllowLastPlanEditorApproval] =
useState(project?.allowLastPlanEditorApproval ?? false);
const [requireIssueApproval, setRequireIssueApproval] = useState(
project?.requireIssueApproval ?? false
);
const [requirePlanCheckNoError, setRequirePlanCheckNoError] = useState(
project?.requirePlanCheckNoError ?? false
);
const [postgresDatabaseTenantMode, setPostgresDatabaseTenantMode] = useState(
project?.postgresDatabaseTenantMode ?? false
);
const [maxRetries, setMaxRetries] = useState<number | null>(
project?.executionRetryPolicy?.maximumRetries ?? 0
);
const [ciSamplingSize, setCiSamplingSize] = useState<number | null>(
project?.ciSamplingSize ?? 0
);
const [parallelTasksPerRollout, setParallelTasksPerRollout] = useState<
number | null
>(project?.parallelTasksPerRollout ?? 1);
// New issue label input
const [newLabelValue, setNewLabelValue] = useState("");
// -----------------------------------------------------------------------
// Danger zone
// -----------------------------------------------------------------------
const [dangerAction, setDangerAction] = useState<
"archive" | "restore" | "delete" | null
>(null);
const [executing, setExecuting] = useState(false);
// -----------------------------------------------------------------------
// Fetch on mount and when projectName changes
// -----------------------------------------------------------------------
const lastFetchedProject = useRef("");
useEffect(() => {
if (lastFetchedProject.current === projectName) return;
lastFetchedProject.current = projectName;
if (!sqlReviewV2Enabled()) {
useSQLReviewStore.getState().fetchReviewPolicyList();
}
useAppStore.getState().getOrFetchPolicyByParentAndType({
parentPath: projectName,
policyType: PolicyType.DATA_QUERY,
});
}, [projectName]);
// Sync review policy state when it loads or changes externally
useEffect(() => {
setPendingReviewPolicy(currentReviewPolicy);
setEnforceReview(currentReviewPolicy?.enforce ?? false);
}, [currentReviewPolicy]);
// Sync max rows when policy loads
useEffect(() => {
setMaxRows(getInitialMaxRows());
}, [getInitialMaxRows]);
// Saving state
const [saving, setSaving] = useState(false);
// -----------------------------------------------------------------------
// Dirty tracking
// -----------------------------------------------------------------------
const isDirty = useMemo(() => {
if (!project) return false;
if (title !== project.title) return true;
if (!isEqual(convertKVListToLabels(labelKVList, false), project.labels))
return true;
// SQL review
if (!isEqual(pendingReviewPolicy, currentReviewPolicy)) return true;
if (
enforceReview !== (pendingReviewPolicy?.enforce ?? false) &&
pendingReviewPolicy
)
return true;
// Max rows
if ((maxRows ?? 0) !== getInitialMaxRows()) return true;
// Project toggles
if (allowRequestRole !== project.allowRequestRole) return true;
if (allowJustInTimeAccess !== project.allowJustInTimeAccess) return true;
// Issue-related
if (!isEqual(issueLabels, project.issueLabels)) return true;
if (forceIssueLabels !== project.forceIssueLabels) return true;
if (enforceIssueTitle !== project.enforceIssueTitle) return true;
if (enforceSqlReview !== project.enforceSqlReview) return true;
if (allowSelfApproval !== project.allowSelfApproval) return true;
if (allowLastPlanEditorApproval !== project.allowLastPlanEditorApproval)
return true;
if (requireIssueApproval !== project.requireIssueApproval) return true;
if (requirePlanCheckNoError !== project.requirePlanCheckNoError)
return true;
if (postgresDatabaseTenantMode !== project.postgresDatabaseTenantMode)
return true;
if (
(maxRetries ?? 0) !== (project.executionRetryPolicy?.maximumRetries ?? 0)
)
return true;
if ((ciSamplingSize ?? 0) !== (project.ciSamplingSize ?? 0)) return true;
if (
(parallelTasksPerRollout ?? 0) !== (project.parallelTasksPerRollout ?? 0)
)
return true;
if (exportDestinationUrl.trim() !== savedExportDestinationUrl) return true;
return false;
}, [
project,
title,
labelKVList,
pendingReviewPolicy,
currentReviewPolicy,
enforceReview,
maxRows,
getInitialMaxRows,
allowRequestRole,
allowJustInTimeAccess,
issueLabels,
forceIssueLabels,
enforceIssueTitle,
enforceSqlReview,
allowSelfApproval,
allowLastPlanEditorApproval,
requireIssueApproval,
requirePlanCheckNoError,
postgresDatabaseTenantMode,
maxRetries,
ciSamplingSize,
parallelTasksPerRollout,
exportDestinationUrl,
savedExportDestinationUrl,
]);
useUnsavedChangesGuard(isDirty);
// -----------------------------------------------------------------------
// Revert
// -----------------------------------------------------------------------
const revert = useCallback(() => {
if (!project) return;
setTitle(project.title);
setLabelKVList(convertLabelsToKVList(project.labels, true));
setPendingReviewPolicy(currentReviewPolicy);
setEnforceReview(currentReviewPolicy?.enforce ?? false);
setMaxRows(getInitialMaxRows());
setAllowRequestRole(project.allowRequestRole);
setAllowJustInTimeAccess(project.allowJustInTimeAccess);
setIssueLabels(cloneDeep(project.issueLabels));
setForceIssueLabels(project.forceIssueLabels);
setEnforceIssueTitle(project.enforceIssueTitle);
setEnforceSqlReview(project.enforceSqlReview);
setAllowSelfApproval(project.allowSelfApproval);
setAllowLastPlanEditorApproval(project.allowLastPlanEditorApproval);
setRequireIssueApproval(project.requireIssueApproval);
setRequirePlanCheckNoError(project.requirePlanCheckNoError);
setPostgresDatabaseTenantMode(project.postgresDatabaseTenantMode);
setMaxRetries(project.executionRetryPolicy?.maximumRetries ?? 0);
setCiSamplingSize(project.ciSamplingSize);
setParallelTasksPerRollout(project.parallelTasksPerRollout);
setExportDestinationUrl(savedExportDestinationUrl);
setExportDestinationError("");
}, [
project,
currentReviewPolicy,
getInitialMaxRows,
savedExportDestinationUrl,
]);
// -----------------------------------------------------------------------
// Save
// -----------------------------------------------------------------------
const handleSave = useCallback(async () => {
if (!project) return;
setSaving(true);
try {
// 1. SQL review policy (separate API, not part of project update)
if (!isEqual(pendingReviewPolicy, currentReviewPolicy)) {
if (currentReviewPolicy) {
await reviewStore.upsertReviewConfigTag({
oldResources: [...currentReviewPolicy.resources],
newResources: currentReviewPolicy.resources.filter(
(r) => r !== projectName
),
review: currentReviewPolicy.id,
});
}
if (pendingReviewPolicy) {
await reviewStore.upsertReviewConfigTag({
oldResources: [...pendingReviewPolicy.resources],
newResources: [...pendingReviewPolicy.resources, projectName],
review: pendingReviewPolicy.id,
});
}
}
if (
pendingReviewPolicy &&
pendingReviewPolicy.enforce !== enforceReview
) {
await reviewStore.upsertReviewPolicy({
id: pendingReviewPolicy.id,
enforce: enforceReview,
});
}
// 2. Max rows policy (separate API)
const maxRowsValue = maxRows ?? 0;
if (maxRowsValue !== getInitialMaxRows()) {
await useAppStore.getState().upsertPolicy({
parentPath: projectName,
policy: {
type: PolicyType.DATA_QUERY,
resourceType: PolicyResourceType.PROJECT,
policy: {
case: "queryDataPolicy",
value: create(QueryDataPolicySchema, {
...queryDataPolicy,
maximumResultRows: maxRowsValue,
}),
},
},
});
}
// 3. All project fields in a single updateProject call
const updateMask: string[] = [];
const projectPatch = cloneDeep(project);
if (title !== project.title && title.trim()) {
projectPatch.title = title.trim();
updateMask.push("title");
}
const currentLabels = convertKVListToLabels(labelKVList, false);
if (!isEqual(currentLabels, project.labels)) {
projectPatch.labels = currentLabels;
updateMask.push("labels");
}
if (allowRequestRole !== project.allowRequestRole) {
projectPatch.allowRequestRole = allowRequestRole;
updateMask.push("allow_request_role");
}
if (allowJustInTimeAccess !== project.allowJustInTimeAccess) {
projectPatch.allowJustInTimeAccess = allowJustInTimeAccess;
updateMask.push("allow_just_in_time_access");
}
if (!isEqual(issueLabels, project.issueLabels)) {
projectPatch.issueLabels = issueLabels;
updateMask.push("issue_labels");
}
if (forceIssueLabels !== project.forceIssueLabels) {
projectPatch.forceIssueLabels = forceIssueLabels;
updateMask.push("force_issue_labels");
}
if (enforceIssueTitle !== project.enforceIssueTitle) {
projectPatch.enforceIssueTitle = enforceIssueTitle;
updateMask.push("enforce_issue_title");
}
if (enforceSqlReview !== project.enforceSqlReview) {
projectPatch.enforceSqlReview = enforceSqlReview;
updateMask.push("enforce_sql_review");
}
if (allowSelfApproval !== project.allowSelfApproval) {
projectPatch.allowSelfApproval = allowSelfApproval;
updateMask.push("allow_self_approval");
}
if (allowLastPlanEditorApproval !== project.allowLastPlanEditorApproval) {
projectPatch.allowLastPlanEditorApproval = allowLastPlanEditorApproval;
updateMask.push("allow_last_plan_editor_approval");
}
if (requireIssueApproval !== project.requireIssueApproval) {
projectPatch.requireIssueApproval = requireIssueApproval;
updateMask.push("require_issue_approval");
}
if (requirePlanCheckNoError !== project.requirePlanCheckNoError) {
projectPatch.requirePlanCheckNoError = requirePlanCheckNoError;
updateMask.push("require_plan_check_no_error");
}
if (postgresDatabaseTenantMode !== project.postgresDatabaseTenantMode) {
projectPatch.postgresDatabaseTenantMode = postgresDatabaseTenantMode;
updateMask.push("postgres_database_tenant_mode");
}
const maxRetriesValue = maxRetries ?? 0;
if (
maxRetriesValue !== (project.executionRetryPolicy?.maximumRetries ?? 0)
) {
projectPatch.executionRetryPolicy = create(
Project_ExecutionRetryPolicySchema,
{ maximumRetries: maxRetriesValue }
);
updateMask.push("execution_retry_policy");
}
const ciSamplingSizeValue = ciSamplingSize ?? 0;
if (ciSamplingSizeValue !== (project.ciSamplingSize ?? 0)) {
projectPatch.ciSamplingSize = ciSamplingSizeValue;
updateMask.push("ci_sampling_size");
}
const parallelTasksPerRolloutValue = parallelTasksPerRollout ?? 0;
if (
parallelTasksPerRolloutValue !== (project.parallelTasksPerRollout ?? 0)
) {
projectPatch.parallelTasksPerRollout = parallelTasksPerRolloutValue;
updateMask.push("parallel_tasks_per_rollout");
}
if (updateMask.length > 0) {
const updatedProject = await useAppStore
.getState()
.updateProject(projectPatch, updateMask);
if (updateMask.includes("issue_labels")) {
setIssueLabels(cloneDeep(updatedProject.issueLabels));
}
}
// Saved on its own so a URL that does not resolve is reported beside the
// field without discarding the other changes.
const nextExportDestinationUrl = exportDestinationUrl.trim();
if (nextExportDestinationUrl !== savedExportDestinationUrl) {
try {
const destinationPatch = cloneDeep(project);
// An unset sharepoint clears the destination.
destinationPatch.exportSetting = create(
Project_ExportSettingSchema,
nextExportDestinationUrl
? { sharepoint: { url: nextExportDestinationUrl } }
: {}
);
const updatedProject = await useAppStore
.getState()
.updateProject(destinationPatch, ["export_setting"], {
byCaller: true,
});
setExportDestinationUrl(
updatedProject.exportSetting?.sharepoint?.url ?? ""
);
} catch (error) {
setExportDestinationError(
error instanceof ConnectError ? error.rawMessage : String(error)
);
pushNotification({
module: "bytebase",
style: "WARN",
title: t("project.settings.export-destination.update-failed"),
});
return;
}
}
pushNotification({
module: "bytebase",
style: "SUCCESS",
title: t("project.settings.success-updated"),
});
} catch {
pushNotification({
module: "bytebase",
style: "CRITICAL",
title: t("project.settings.update-failed"),
});
} finally {
setSaving(false);
}
}, [
project,
reviewStore,
projectName,
title,
labelKVList,
pendingReviewPolicy,
currentReviewPolicy,
enforceReview,
maxRows,
getInitialMaxRows,
queryDataPolicy,
allowRequestRole,
allowJustInTimeAccess,
issueLabels,
forceIssueLabels,
enforceIssueTitle,
enforceSqlReview,
allowSelfApproval,
allowLastPlanEditorApproval,
requireIssueApproval,
requirePlanCheckNoError,
postgresDatabaseTenantMode,
maxRetries,
ciSamplingSize,
parallelTasksPerRollout,
exportDestinationUrl,
savedExportDestinationUrl,
t,
]);
// -----------------------------------------------------------------------
// Danger zone handlers
// -----------------------------------------------------------------------
const handleDangerConfirm = useCallback(async () => {
if (!project || !dangerAction) return;
setExecuting(true);
try {
if (dangerAction === "archive") {
await useAppStore.getState().archiveProject(project);
pushNotification({
module: "bytebase",
style: "SUCCESS",
title: `${project.title || project.name} ${t("common.archived")}`,
});
} else if (dangerAction === "restore") {
await useAppStore.getState().restoreProject(project);
pushNotification({
module: "bytebase",
style: "SUCCESS",
title: `${project.title || project.name} ${t("common.restored")}`,
});
} else if (dangerAction === "delete") {
await useAppStore.getState().deleteProject(project.name);
pushNotification({
module: "bytebase",
style: "SUCCESS",
title: `${project.title || project.name} ${t("common.deleted")}`,
});
}
router.push({ name: PROJECT_V1_ROUTE_DASHBOARD });
} finally {
setExecuting(false);
setDangerAction(null);
}
}, [project, dangerAction, t]);
// Label validation errors (set by LabelListEditor via onErrorsChange)
const [labelErrors, setLabelErrors] = useState<string[]>([]);
// -----------------------------------------------------------------------
// Issue label helpers
// -----------------------------------------------------------------------
const addIssueLabel = useCallback(() => {
const trimmed = newLabelValue.trim();
if (!trimmed) return;
if (issueLabels.some((l) => l.value === trimmed)) return;
setIssueLabels((prev) => [
...prev,
create(LabelSchema, {
value: trimmed,
color: hexToColor("#4f46e5"),
group: "",
}),
]);
setNewLabelValue("");
}, [newLabelValue, issueLabels]);
const removeIssueLabel = useCallback((index: number) => {
setIssueLabels((prev) => {
const next = prev.filter((_, i) => i !== index);
if (next.length === 0) {
setForceIssueLabels(false);
}
return next;
});
}, []);
const updateIssueLabelColor = useCallback((index: number, color: string) => {
setIssueLabels((prev) => {
const next = [...prev];
next[index] = { ...next[index], color: hexToColor(color) };
return next;
});
}, []);
if (!project) return null;
const canUpdateProject = hasPermission("bb.projects.update");
const canGetPolicies = hasPermission("bb.policies.get");
const canUpdatePolicies = hasPermission("bb.policies.update");
const canDelete = hasPermission("bb.projects.delete");
const canUndelete = hasPermission("bb.projects.undelete");
// -----------------------------------------------------------------------
// Render
// -----------------------------------------------------------------------
return (
<div className="w-full flex flex-col gap-y-0 px-4">
<div className="divide-y divide-block-border">
{/* ============================================================= */}
{/* Section 1: General */}
{/* ============================================================= */}
<FormSection title={t("common.general")}>
<PermissionGuard
permissions={["bb.projects.update"]}
project={project}
display="block"
>
<form className="w-full">
<FormFieldGroup>
<FormField
title={
<>
{t("common.name")} <span className="text-error">*</span>
</>
}
>
<Input
value={title}
onChange={(e) => setTitle(e.target.value)}
disabled={
!canUpdateProject ||
isDefault ||
project.state !== State.ACTIVE
}
required
/>
<ResourceIdField
readonly
value={extractProjectResourceName(project.name)}
resourceName={t("common.project")}
/>
</FormField>
{/* Project Labels */}
<FormField
title={t("project.settings.project-labels.self")}
description={t("project.settings.project-labels.description")}
>
<LabelListEditor
kvList={labelKVList}
onChange={setLabelKVList}
readonly={!canUpdateProject}
onErrorsChange={setLabelErrors}
/>
</FormField>
</FormFieldGroup>
</form>
</PermissionGuard>
</FormSection>
{/* ============================================================= */}
{/* Section 2: Security & Policy */}
{/* ============================================================= */}
<FormSection title={t("settings.sidebar.security-and-policy")}>
<FormFieldGroup>
{/* SQL Review + Max Rows: gated on bb.policies.get */}
{canGetPolicies && (
<>
{/* SQL Review */}
{!sqlReviewV2 &&
hasWorkspacePermissionV2("bb.reviewConfigs.get") && (
<FormField title={t("sql-review.title")}>
<div>
{pendingReviewPolicy ? (
<FormControlRow>
<Switch
checked={enforceReview}
onCheckedChange={setEnforceReview}
disabled={
!hasWorkspacePermissionV2(
"bb.reviewConfigs.update"
)
}
/>
<span
className="text-sm font-medium text-accent cursor-pointer hover:underline"
onClick={() =>
router.push({
name: WORKSPACE_ROUTE_SQL_REVIEW_DETAIL,
params: {
sqlReviewPolicySlug:
sqlReviewPolicySlug(pendingReviewPolicy),
},
})
}
>
{pendingReviewPolicy.name}
</span>
{canUpdatePolicies && (
<Button
appearance="secondary"
size="sm"
onClick={() => {
setPendingReviewPolicy(undefined);
setEnforceReview(false);
}}
>
<X className="size-4" />
</Button>
)}
</FormControlRow>
) : (
<Button
appearance="outline"
disabled={
!canUpdatePolicies ||
!hasWorkspacePermissionV2("bb.reviewConfigs.list")
}
onClick={() => setShowReviewDialog(true)}
>
{t("sql-review.configure-policy")}
</Button>
)}
</div>
</FormField>
)}
{/* Maximum SQL Result Rows */}
<FormField
title={
<FormControlRow className="w-auto">
<span>
{t(
"settings.general.workspace.maximum-sql-result.rows.self"
)}
</span>
<FeatureBadge
feature={PlanFeature.FEATURE_QUERY_POLICY}
/>
</FormControlRow>
}
description={
<>
{t(
"settings.general.workspace.maximum-sql-result.rows.description"
)}{" "}
<span className="font-semibold">
{t("settings.general.workspace.no-limit")}
</span>
</>
}
>
<FormControlRow>
<NumberInput
className="w-60"
min={0}
value={maxRows}
onValueChange={setMaxRows}
disabled={!hasQueryPolicyFeature || !canUpdatePolicies}
/>
<span className="text-sm text-control-light">
{t(
"settings.general.workspace.maximum-sql-result.rows.rows"
)}
</span>
</FormControlRow>
</FormField>
</>
)}
<ProjectExportDestinationField
value={exportDestinationUrl}
onChange={handleExportDestinationChange}
disabled={!canUpdateProject || !hasSharePointExportFeature}
canClear={canUpdateProject}
error={exportDestinationError}
/>
{/* Allow Request Role */}
<ToggleRow
checked={allowRequestRole}
onCheckedChange={setAllowRequestRole}
disabled={!canUpdateProject}
label={t(
"project.settings.issue-related.allow-request-role.self"
)}
description={t(
"project.settings.issue-related.allow-request-role.description"
)}
trailing={
<ApprovalFlowIndicator
source={WorkspaceApprovalSetting_Rule_Source.REQUEST_ROLE}
/>
}
/>
{/* Allow access grants */}
<ToggleRow
checked={allowJustInTimeAccess}
onCheckedChange={setAllowJustInTimeAccess}
disabled={!canUpdateProject}
label={t(
"project.settings.issue-related.allow-access-grants.self"
)}
description={t(
"project.settings.issue-related.allow-access-grants.description"
)}
trailing={
<ApprovalFlowIndicator
source={WorkspaceApprovalSetting_Rule_Source.REQUEST_ACCESS}
/>
}
/>
</FormFieldGroup>
</FormSection>
{/* ============================================================= */}
{/* Section 3: Issue-Related */}
{/* ============================================================= */}
<FormSection
id="issue-related"
title={t("project.settings.issue-related.self")}
>
<FormFieldGroup>
{/* Issue Labels */}
<FormField
title={t("project.settings.issue-related.labels.self")}
description={t(
"project.settings.issue-related.labels.description"
)}
>
<div className="flex flex-wrap items-center gap-2">
{issueLabels.map((label, index) => (
<span
key={index}
className="inline-flex h-9 items-center gap-x-2 border border-control-border rounded-sm px-1 py-1"
>
<ColorSwatchInput
value={label.color ? colorToHex(label.color) : "#4f46e5"}
onChange={(value) => updateIssueLabelColor(index, value)}
disabled={!canUpdateProject}
className="h-6 w-5 rounded-sm border-0"
/>
<span className="text-sm">{label.value}</span>
{canUpdateProject && (
<Button
appearance="secondary"
size="xs"
type="button"
className="text-control-light hover:text-main"
onClick={() => removeIssueLabel(index)}
>
<X className="size-3" />
</Button>
)}
</span>
))}
{canUpdateProject && (
<div className="inline-flex items-center gap-x-1">
<Input
className="w-48"
placeholder={t(
"project.settings.issue-related.labels.placeholder"
)}
value={newLabelValue}
onChange={(e) => setNewLabelValue(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
addIssueLabel();
}
}}
/>
<Button
appearance="outline"
size="md"
onClick={addIssueLabel}
disabled={!newLabelValue.trim()}
>
{t("common.add")}
</Button>
</div>
)}
</div>
</FormField>
{/* Boolean toggles */}
<ToggleRow
checked={forceIssueLabels}
onCheckedChange={setForceIssueLabels}
disabled={!canUpdateProject || issueLabels.length === 0}
label={t(
"project.settings.issue-related.labels.force-issue-labels.self"
)}
description={t(
"project.settings.issue-related.labels.force-issue-labels.description"
)}
warning={
canUpdateProject && issueLabels.length === 0
? t(
"project.settings.issue-related.labels.force-issue-labels.warning"
)
: undefined
}
/>
<ToggleRow
checked={enforceIssueTitle}
onCheckedChange={setEnforceIssueTitle}
disabled={!canUpdateProject}
label={t(
"project.settings.issue-related.enforce-issue-title.self"
)}
description={t(
"project.settings.issue-related.enforce-issue-title.description"
)}
/>
{!sqlReviewV2 && (
<ToggleRow
checked={enforceSqlReview}
onCheckedChange={setEnforceSqlReview}
disabled={!canUpdateProject}
label={t(
"project.settings.issue-related.enforce-sql-review.self"
)}
description={t(
"project.settings.issue-related.enforce-sql-review.description"
)}
/>
)}
<FormField
title={t(
"project.settings.issue-related.approval-permissions.self"
)}
description={t(
"project.settings.issue-related.approval-permissions.description"
)}
/>
<ToggleRow
checked={allowSelfApproval}
onCheckedChange={setAllowSelfApproval}
disabled={!canUpdateProject}
label={t(
"project.settings.issue-related.allow-self-approval.self"
)}
description={t(
"project.settings.issue-related.allow-self-approval.description"
)}
/>
<ToggleRow
checked={allowLastPlanEditorApproval}
onCheckedChange={setAllowLastPlanEditorApproval}
disabled={!canUpdateProject}
label={t(
"project.settings.issue-related.allow-last-plan-editor-approval.self"
)}
description={t(
"project.settings.issue-related.allow-last-plan-editor-approval.description"
)}
/>
<ToggleRow
checked={requireIssueApproval}
onCheckedChange={setRequireIssueApproval}
disabled={!canUpdateProject}
label={t(
"project.settings.issue-related.require-issue-approval.self"
)}
description={t(
"project.settings.issue-related.require-issue-approval.description"
)}
/>
<ToggleRow
checked={requirePlanCheckNoError}
onCheckedChange={setRequirePlanCheckNoError}
disabled={!canUpdateProject}
label={t(
"project.settings.issue-related.require-plan-check-no-error.self"
)}
description={t(
"project.settings.issue-related.require-plan-check-no-error.description"
)}
/>
<ToggleRow
checked={postgresDatabaseTenantMode}
onCheckedChange={setPostgresDatabaseTenantMode}
disabled={!canUpdateProject}
label={t(
"project.settings.issue-related.postgres-database-tenant-mode.self"
)}
description={t(
"project.settings.issue-related.postgres-database-tenant-mode.description"
)}
/>
{/* Numeric inputs */}
<NumericRow
label={t("project.settings.issue-related.max-retries.self")}
description={t(
"project.settings.issue-related.max-retries.description"
)}
value={maxRetries}
onChange={setMaxRetries}
disabled={!canUpdateProject}
suffix="Times"
/>
<NumericRow
label={t("project.settings.issue-related.ci-sampling-size.self")}
description={t(
"project.settings.issue-related.ci-sampling-size.description"
)}
value={ciSamplingSize}
onChange={setCiSamplingSize}
disabled={!canUpdateProject}
/>
<NumericRow
label={t(
"project.settings.issue-related.parallel_tasks_per_rollout.self"
)}
description={t(
"project.settings.issue-related.parallel_tasks_per_rollout.description"
)}
value={parallelTasksPerRollout}
onChange={setParallelTasksPerRollout}
disabled={!canUpdateProject}
/>
</FormFieldGroup>
</FormSection>
{/* ============================================================= */}
{/* Section 4: Danger Zone */}
{/* ============================================================= */}
<FormSection title={t("common.danger-zone")}>
<div className="border border-error-alpha bg-error-alpha rounded-sm divide-y divide-error-alpha">
{/* Archive / Restore */}
<div className="p-6 flex items-start justify-between gap-x-6">
{project.state === State.ACTIVE ? (
<>
<FormField
className="flex-1"
title={t("common.archive-resource", {
type: t("common.project"),
})}
description={t("common.archive-description", {
name: project.title || project.name,
})}
/>
<Button
appearance="outline"
disabled={!canDelete || executing}
onClick={() => setDangerAction("archive")}
>
{t("common.archive")}
</Button>
</>
) : project.state === State.DELETED ? (
<>
<FormField
className="flex-1"
title={t("project.settings.restore.title")}
description={t("project.settings.restore.btn-text")}
/>
<Button
appearance="outline"
disabled={!canUndelete || executing}
onClick={() => setDangerAction("restore")}
>
{t("common.restore")}
</Button>
</>
) : null}
</div>
{/* Delete */}
<div className="p-6 flex items-start justify-between gap-x-6">
<FormField
className="flex-1"
title={
<span className="text-error">
{t("common.delete-resource", {
type: t("common.project"),
})}
</span>
}
description={t("common.delete-resource-description", {
name: project.title || project.name,
})}
/>
<Button
variant="destructive"
disabled={!canDelete || executing}
onClick={() => setDangerAction("delete")}
>
{t("common.delete")}
</Button>
</div>
</div>
</FormSection>
{/* ============================================================= */}
{/* Save / Cancel Bar */}
{/* ============================================================= */}
{allowEdit && isDirty && (
<StickyActionFooter
contentPadding={false}
left={
<Button appearance="outline" onClick={revert}>
{t("common.cancel")}
</Button>
}
right={
<Button
onClick={handleSave}
disabled={labelErrors.length > 0 || saving || !title.trim()}
>
{t("common.update")}
</Button>
}
/>
)}
</div>
{/* ============================================================= */}
{/* Dialogs */}
{/* ============================================================= */}
{/* SQL Review Policy Select Dialog */}
<Dialog open={showReviewDialog} onOpenChange={setShowReviewDialog}>
<DialogContent className="p-6">
<DialogTitle>{t("sql-review.configure-policy")}</DialogTitle>
<div className="mt-4 flex flex-col gap-y-2 max-h-96 overflow-y-auto">
{reviewPolicyList.length === 0 ? (
<div className="flex flex-col items-center gap-y-3 py-4">
<p className="text-sm text-control-light">
{t("common.no-data")}
</p>
{hasWorkspacePermissionV2("bb.reviewConfigs.create") && (
<Button
appearance="outline"
onClick={() => {
setShowReviewDialog(false);
router.push({
name: WORKSPACE_ROUTE_SQL_REVIEW_CREATE,
query: { attachedResource: projectName },
});
}}
>
{t("common.create")}
</Button>
)}
</div>
) : (
reviewPolicyList.map((policy) => (
<Button
appearance="secondary"
size="md"
key={policy.id}
type="button"
className="h-auto w-full flex-col items-start rounded-sm border border-control-border px-4 py-3 text-left whitespace-normal transition-colors hover:bg-control-bg"
onClick={() => {
setPendingReviewPolicy(policy);
setEnforceReview(true);
setShowReviewDialog(false);
}}
>
<div className="w-full min-w-0 break-words font-medium">
{policy.name}
</div>
{policy.resources.length > 0 && (
<div className="mt-1 w-full min-w-0 break-words text-xs text-control-light">
{policy.resources.length}{" "}
{t("common.resource", {
count: policy.resources.length,
})}
</div>
)}
</Button>
))
)}
</div>
<div className="mt-4 flex justify-end">
<Button
appearance="outline"
onClick={() => setShowReviewDialog(false)}
>
{t("common.cancel")}
</Button>
</div>
</DialogContent>
</Dialog>
{/* Danger Zone Confirmation Dialog */}
<Dialog
open={dangerAction !== null}
onOpenChange={(open) => {
if (!open) setDangerAction(null);
}}
>
<DialogContent className="p-6 max-w-md">
<DialogTitle>
{dangerAction === "archive" && t("common.confirm-archive")}
{dangerAction === "restore" && t("project.settings.restore.title")}
{dangerAction === "delete" && t("common.confirm-delete")}
</DialogTitle>
<p className="text-sm text-control-light mt-2">
{dangerAction === "archive" &&
t("project.settings.confirm-archive-project", {
name: project.title || project.name,
})}
{dangerAction === "restore" &&
`${t("project.settings.restore.title")} '${project.title || project.name}'?`}
{dangerAction === "delete" &&
t("project.settings.confirm-delete-project", {
name: project.title || project.name,
})}
</p>
<div className="mt-6 flex justify-end items-center gap-x-2">
<Button
appearance="outline"
onClick={() => setDangerAction(null)}
disabled={executing}
>
{t("common.cancel")}
</Button>
<Button
variant={dangerAction === "delete" ? "destructive" : "default"}
onClick={handleDangerConfirm}
disabled={executing}
>
{t("common.confirm")}
</Button>
</div>
</DialogContent>
</Dialog>
</div>
);
}
// ---------------------------------------------------------------------------
// Helper components
// ---------------------------------------------------------------------------
function ToggleRow({
checked,
onCheckedChange,
disabled,
label,
description,
trailing,
warning,
}: {
checked: boolean;
onCheckedChange: (v: boolean) => void;
disabled: boolean;
label: string;
description: string;
trailing?: ReactNode;
warning?: string;
}) {
return (
<FormField
title={
<FormControlRow>
<Switch
checked={checked}
onCheckedChange={onCheckedChange}
disabled={disabled}
/>
<span className="text-sm font-medium">{label}</span>
{trailing}
{warning && (
<Tooltip content={warning}>
<TriangleAlert className="size-4 text-warning" />
</Tooltip>
)}
</FormControlRow>
}
description={description}
/>
);
}
function NumericRow({
label,
description,
value,
onChange,
disabled,
suffix,
}: {
label: string;
description: string;
value: number | null;
onChange: (v: number | null) => void;
disabled: boolean;
suffix?: string;
}) {
return (
<FormField title={label} description={description}>
<FormControlRow>
<NumberInput
className="w-60"
min={0}
value={value}
onValueChange={onChange}
disabled={disabled}
/>
{suffix && <span className="text-sm text-control-light">{suffix}</span>}
</FormControlRow>
</FormField>
);
}