Files
supabase/apps/studio/data/telemetry/send-event-mutation.ts
T
Joshen LimandSaxon Fletcher 12d92aed99 Assistant V2 (#30523)
* start

* added panels

* remove stuff

* fixes and refinements

* clean up

* remove old assistant panel

* resizable assistant kinda

* use icon

* Add missing package

* remove canvas

* add suggestions

* updated empty state if no tables exist

* fix table condition

* Implement diffing if using assistant in sql editor

* Reinstate old assistant in SQL editor if feature preview is off

* pane size adjustment

* assistant button corners

* Add SQL snippet content to assistant if opening assistant in sql editor

* Add the necessary checks for opt in and hipaa

* revert adding snippet to assistant when opening assistant in sql editor

* Add cmd i shortcut

* Add admonitions for when disablePrompt is toggled on, and if no api key is set. Add footer note RE rate limitation

* Bump ai package in packages

* some fixes for backwards compability depending on feature preview toggled

* Rename feature preview property for new assistant

* Smol fix

* Prevent SQL snippet from running until message is finished

* only loading last message

* fix z-index

* save chat state to global state

* add debug to failed ai queries

* Add basic contextual invalidation

* Add explain code action to SQL editor

* Add link to abort ongoing queries from SqlSnippet

* Update feature preview content

* Fix

* Fix

* Fix

* Te4st

* Fix tests

* ONly show ai button within a project

* Fix PH tracking

* Beef up a bit more event tracking

* Rough fix to padding when assistant is open

* A bit more telemetry stuff

* Update prompts

* fix rls editing via assistant

* Update generate-v3.ts

prompt to get auth schema too

* Add policy satement to assistant when editing

* Address all comments

* fixc

* Fix SqlSnippet not taking full width on larger viewports

* Adjust max width

---------

Co-authored-by: Saxon Fletcher <saxonafletcher@gmail.com>
2024-11-25 18:50:56 +08:00

93 lines
2.8 KiB
TypeScript

import { useMutation, UseMutationOptions } from '@tanstack/react-query'
import { components } from 'api-types'
import { isBrowser, LOCAL_STORAGE_KEYS } from 'common'
import { handleError, post } from 'data/fetchers'
import { IS_PLATFORM } from 'lib/constants'
import { useRouter } from 'next/router'
import type { ResponseError } from 'types'
type SendEvent = components['schemas']['TelemetryEventBodyV2']
export type SendEventVariables = {
/** Defines the name of the event, refer to TELEMETRY_EVENTS in lib/constants */
action: string
/** These are all under the event's properties (customizable on the FE) */
/** value: refer to TELEMETRY_VALUES in lib/constants */
value?: string
/** label: secondary tag to the event for further identification */
label?: string
/** To deprecate - seems unnecessary */
category?: string
}
type SendEventPayload = any
export async function sendEvent({ body }: { body: SendEventPayload }) {
const consent =
(typeof window !== 'undefined'
? localStorage.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT)
: null) === 'true'
if (!consent || !IS_PLATFORM) return undefined
const headers = { Version: '2' }
const { data, error } = await post(`/platform/telemetry/event`, { body, headers })
if (error) handleError(error)
return data
}
type SendEventData = Awaited<ReturnType<typeof sendEvent>>
export const useSendEventMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<SendEventData, ResponseError, SendEventVariables>,
'mutationFn'
> = {}) => {
const router = useRouter()
const title = typeof document !== 'undefined' ? document?.title : ''
const referrer = typeof document !== 'undefined' ? document?.referrer : ''
return useMutation<SendEventData, ResponseError, SendEventVariables>(
(vars) => {
const { action, ...otherVars } = vars
const body: SendEvent = {
action,
page_url: window.location.href,
page_title: title,
pathname: router.pathname,
ph: {
referrer,
language: router?.locale ?? 'en-US',
user_agent: navigator.userAgent,
search: window.location.search,
viewport_height: isBrowser ? window.innerHeight : 0,
viewport_width: isBrowser ? window.innerWidth : 0,
},
// @ts-expect-error - API is returning a wrong type
custom_properties: otherVars,
}
return sendEvent({ body })
},
{
async onSuccess(data, variables, context) {
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
console.error(`Failed to send Telemetry event: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}