Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: Refactor some suspicious Valtio uses (#48141)

Ivan Vasilov99fd5d01177d
diff --git a/apps/studio/state/ai-assistant-state.tsx b/apps/studio/state/ai-assistant-state.tsx
index 9f9599b32f9..fc9d188f969 100644
--- a/apps/studio/state/ai-assistant-state.tsx
+++ b/apps/studio/state/ai-assistant-state.tsx
@@ -511,37 +511,14 @@ export const createAiAssistantState = (): AiAssistantState => {
chat.updatedAt = new Date()
},
- saveMessage: (message: MessageType | MessageType[]) => {
- const chat = state.activeChat
- if (!chat) return
-
- const incomingMessages = Array.isArray(message) ? message : [message]
-
- const messagesToAdd: AssistantMessageType[] = []
-
- incomingMessages.forEach((msg) => {
- const index = chat.messages.findIndex((existing) => existing.id === msg.id)
-
- if (index !== -1) {
- state.updateMessage(msg)
- } else {
- messagesToAdd.push(msg)
- }
- })
-
- if (messagesToAdd.length > 0) {
- chat.messages.push(...messagesToAdd)
- chat.updatedAt = new Date()
- }
- },
-
updateMessage: (updatedMessage: MessageType) => {
const chat = state.activeChat
if (!chat) return
const messageIndex = chat.messages.findIndex((msg) => msg.id === updatedMessage.id)
if (messageIndex !== -1) {
- chat.messages[messageIndex] = updatedMessage
+ // Clone first — valtio's proxy() mutates nested properties in place and would corrupt the SDK's live array
+ chat.messages[messageIndex] = sanitizeForCloning(updatedMessage)
chat.updatedAt = new Date()
}
},
@@ -637,7 +614,6 @@ export type AiAssistantState = AiAssistantData & {
renameChat: (id: string, name: string) => void
clearMessages: () => void
deleteMessagesAfter: (id: string, options?: { includeSelf?: boolean }) => void
- saveMessage: (message: MessageType | MessageType[]) => void
updateMessage: (message: MessageType) => void
setSqlSnippets: (snippets: SqlSnippet[]) => void
clearSqlSnippets: () => void
diff --git a/apps/studio/state/table-editor-table.tsx b/apps/studio/state/table-editor-table.tsx
index 010e6c67259..2c0f0b9eb50 100644
--- a/apps/studio/state/table-editor-table.tsx
+++ b/apps/studio/state/table-editor-table.tsx
@@ -93,8 +93,10 @@ export const createTableEditorTableState = ({
state.table = supaTable
state.gridColumns = gridColumns
- state.originalTable = table
+ // ref() must run before the assignment below — otherwise valtio's proxy() would wrap
+ // and mutate `table`'s nested properties in place, corrupting the shared react-query cache object
state._originalTableRef = ref(table)
+ state.originalTable = table
},
/* Rows */
diff --git a/packages/common/consent-state.ts b/packages/common/consent-state.ts
index 99ffee7fea1..5b39ae5f99a 100644
--- a/packages/common/consent-state.ts
+++ b/packages/common/consent-state.ts
@@ -1,6 +1,6 @@
import type Usercentrics from '@usercentrics/cmp-browser-sdk'
import type { BaseCategory, UserDecision } from '@usercentrics/cmp-browser-sdk'
-import { proxy, snapshot, useSnapshot } from 'valtio'
+import { proxy, ref, snapshot, useSnapshot } from 'valtio'
import { IS_PLATFORM, LOCAL_STORAGE_KEYS } from './constants'
@@ -192,7 +192,9 @@ export function applyPriorDecisionToSDK(
initialUIValues: { initialLayer: number },
priorDecision: PriorConsentDecision
): void {
- consentState.UC = UC
+ // ref() prevents valtio from recursively proxying the SDK instance's internals in place,
+ // which would corrupt them the same way it once corrupted the AI Assistant's message array
+ consentState.UC = ref(UC)
const hasConsented = UC.areAllConsentsAccepted()
// If the SDK wants to show the banner but the user previously made a