mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 17:35:10 +03:00
## Summary - extract the reusable assistant conversation body from the sidebar interface - drive shared chat rendering and mutations with an explicit chat ID - keep sidebar selection, shortcuts, SQL-editor context, and header controls in the sidebar wrapper - add selection-free chat creation and branching primitives while preserving existing sidebar helpers - add store tests for cross-surface selection and mutation isolation This is PR 1 of 3. Review this foundation first, then #49031 for the routed Explorer chat lifecycle, followed by #49032 for discovery and cross-surface entry points. Supersedes #48970 after renaming the branch to use the repository's `chore/` convention. ## Test plan - `mise exec node@22 -- pnpm --dir apps/studio exec tsc --noEmit` - `mise exec node@22 -- pnpm --dir apps/studio exec vitest run state/ai-assistant-state.test.ts` - ESLint on changed files - Prettier on changed files
66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
import { createContext, useContext, type PropsWithChildren } from 'react'
|
|
|
|
export type AddToolApprovalResponse = (args: {
|
|
id: string
|
|
approved: boolean
|
|
reason?: string
|
|
}) => void | PromiseLike<void>
|
|
|
|
export interface MessageInfo {
|
|
id: string
|
|
|
|
variant?: 'default' | 'warning'
|
|
|
|
isLoading: boolean
|
|
readOnly?: boolean
|
|
|
|
isUserMessage?: boolean
|
|
isLastMessage?: boolean
|
|
|
|
state: 'idle' | 'editing' | 'predecessor-editing'
|
|
rating?: 'positive' | 'negative' | null
|
|
}
|
|
|
|
export interface MessageActions {
|
|
addToolApprovalResponse?: AddToolApprovalResponse
|
|
|
|
onDelete: (id: string) => void
|
|
onEdit: (id: string) => void
|
|
onBranch: (id: string) => void
|
|
onCancelEdit: () => void
|
|
onRate?: (id: string, rating: 'positive' | 'negative', reason?: string) => void
|
|
}
|
|
|
|
const MessageInfoContext = createContext<MessageInfo | null>(null)
|
|
const MessageActionsContext = createContext<MessageActions | null>(null)
|
|
|
|
export function useMessageInfoContext() {
|
|
const ctx = useContext(MessageInfoContext)
|
|
if (!ctx) {
|
|
throw Error('useMessageInfoContext must be used within a MessageProvider')
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
export function useMessageActionsContext() {
|
|
const ctx = useContext(MessageActionsContext)
|
|
if (!ctx) {
|
|
throw Error('useMessageActionsContext must be used within a MessageProvider')
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
export function MessageProvider({
|
|
messageInfo,
|
|
messageActions,
|
|
children,
|
|
}: PropsWithChildren<{ messageInfo: MessageInfo; messageActions: MessageActions }>) {
|
|
return (
|
|
<MessageInfoContext.Provider value={messageInfo}>
|
|
<MessageActionsContext.Provider value={messageActions}>
|
|
{children}
|
|
</MessageActionsContext.Provider>
|
|
</MessageInfoContext.Provider>
|
|
)
|
|
}
|