Files
supabase/apps/studio/components/ui/AIAssistantPanel/OptInRequest.tsx
Joshen Lim 6d08d66b7d Add eslint rule for google translate dynamic spans + address all issues (#52949)
## Context

We've been seeing many `NotFoundError: Failed to execute 'removeChild'
on 'Node'` and `NotFoundError: Failed to execute 'insertBefore' on
'Node'` events on Sentry which are historically known to be caused by
Google Translate in the browser.

There's technically no _right way_ to address this unfortunately, but
one of the solutions which should address most of these issues is to
wrap any dynamic text within a span - we've considered that quite a
while back tbh but never took the time to do this until today with the
help of Claude. Although just a heads up that it might not necessarily
completely eradicate those issues.

Also added an eslint rule which should prevent future occurences of this
<img width="1264" height="192" alt="image"
src="https://github.com/user-attachments/assets/b2c32ea0-45a2-4ccd-82fe-1a859905fc29"
/>
2026-11-05 23:28:46 +08:00

104 lines
3.8 KiB
TypeScript

import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useState } from 'react'
import { AI_OPT_IN_LEVEL_LABELS } from './AiAssistant.constants'
import { AIOptInModal } from './AIOptInModal'
import { Confirm } from './Confirm'
import type { ConfirmFooterApprovalState } from './Confirm.utils'
import { NoPermission } from '@/components/ui/NoPermission'
import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
import { useOrgAiOptInLevel, type AiOptInLevel } from '@/hooks/misc/useOrgOptedIntoAi'
import { contract } from '@/lib/ai/tools/update-opt-in-level'
interface OptInRequestProps {
/** Omitted when the Assistant is only asking the user to review the setting. */
requiredLevel?: AiOptInLevel
/** The org's level when the Assistant asked. Falls back to the live level on older chats. */
levelWhenAsked?: AiOptInLevel
output?: unknown
confirmState?: ConfirmFooterApprovalState
onApprove?: () => void
onDeny?: () => void
}
/** Approval card for `update_opt_in_level`. Saving in the modal approves, Skip denies. */
export const OptInRequest = ({
requiredLevel,
levelWhenAsked,
output,
confirmState,
onApprove,
onDeny,
}: OptInRequestProps) => {
const [isModalVisible, setIsModalVisible] = useState(false)
const { aiOptInLevel: liveLevel } = useOrgAiOptInLevel()
const aiOptInLevel = levelWhenAsked ?? liveLevel
const { can: canUpdateOrganization } = useAsyncCheckPermissions(
PermissionAction.UPDATE,
'organizations'
)
const requiredLabel = requiredLevel && AI_OPT_IN_LEVEL_LABELS[requiredLevel]
const result = contract.outputSchema.safeParse(output).data
// Saving a lower level still approves the call
const isBelowRequest = confirmState === 'success' && result?.sufficient === false
const newLevel = result?.levelAfterReview
const successMessage =
newLevel && newLevel !== aiOptInLevel
? `Opt-in level updated to ${AI_OPT_IN_LEVEL_LABELS[newLevel]}`
: `Opt-in level is still ${AI_OPT_IN_LEVEL_LABELS[aiOptInLevel]}`
let message = 'Only an owner or administrator can change the opt-in level'
if (canUpdateOrganization) message = 'Assistant wants you to review the opt-in level'
if (requiredLabel) {
message = canUpdateOrganization
? `Assistant wants ${requiredLabel} access`
: `The Assistant will answer without ${requiredLabel} access`
}
return (
<>
<Confirm
state={isBelowRequest ? 'error' : confirmState}
className="my-4"
message={message}
successMessage={successMessage}
errorMessage={`${requiredLabel} access was not granted`}
cancelLabel={canUpdateOrganization ? 'Skip' : 'Continue'}
deniedMessage={
canUpdateOrganization
? 'Skipped opt-in request'
: 'Continued without changing the opt-in level'
}
confirmLabel="Review opt-in level"
denyOnly={!canUpdateOrganization}
onCancel={onDeny}
onConfirm={() => setIsModalVisible(true)}
>
{canUpdateOrganization ? (
<div className="p-4 text-sm">
<p className="text-foreground">
{requiredLabel ? `${requiredLabel} access needed` : 'Review opt-in level'}
</p>
<p className="text-foreground-light">
{requiredLabel && (
<span>{`The Assistant needs the ${requiredLabel} opt-in level to answer this. `}</span>
)}
{`Your organization is set to ${AI_OPT_IN_LEVEL_LABELS[aiOptInLevel]}.`}
</p>
</div>
) : (
<NoPermission resourceText="change the Assistant's opt-in level" />
)}
</Confirm>
<AIOptInModal
visible={isModalVisible}
requiredLevel={requiredLevel}
onCancel={() => setIsModalVisible(false)}
onSaved={() => onApprove?.()}
/>
</>
)
}