Files
supabase/apps/studio/components/interfaces/Support/SupportForm.state.ts
Charis f5ff10e195 refactor,tests(support form) (#39410)
* refactor: refactor support form

Refactor support form to make it easier to maintain:
- Split up large components into smaller components and hooks
- Lift state up so we don't have to do complex child/parent
state-syncing via useEffect
- Use nuqs parsing for consistent serialization/deserialization of
support form prefilled fields

* test: support form

Add comprehensive tests for support form

* fix(support form): project and org empty state

* Nit clean up

* More clean up

* cleannnn

* fix(support form): allow case-insensitive category in url

* clean(support form tests): remove unused param

* fix(support form): incorrect logic for sending affected services in payload

* clean(support form): use NO_ORG_MARKER and NO_PROJECT_MARKER instead of strings

* fix(support form): don't show upgrade cta if already on enterprise

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-10-14 11:04:33 +08:00

97 lines
2.6 KiB
TypeScript

import { neverGuard } from 'lib/helpers'
import type { ExtendedSupportCategories } from './Support.constants'
export type SupportFormState =
| {
type: 'initializing'
}
| {
type: 'editing'
}
| {
type: 'submitting'
}
| {
type: 'success'
sentProjectRef: string | undefined
sentOrgSlug: string | undefined
sentCategory: ExtendedSupportCategories
}
| {
type: 'error'
message: string
}
export type SupportFormActions =
| { type: 'INITIALIZE'; debugSource?: string }
| { type: 'SUBMIT'; debugSource?: string }
| {
type: 'SUCCESS'
sentProjectRef: string | undefined
sentOrgSlug: string | undefined
sentCategory: ExtendedSupportCategories
debugSource?: string
}
| { type: 'ERROR'; message: string; debugSource?: string }
| { type: 'RETURN_TO_EDITING'; debugSource?: string }
export function createInitialSupportFormState(): SupportFormState {
return {
type: 'initializing',
}
}
export function supportFormReducer(
state: SupportFormState,
action: SupportFormActions
): SupportFormState {
switch (state.type) {
case 'initializing':
if (action.type === 'INITIALIZE') {
return { type: 'editing' }
}
console.warn(
`[SupportForm > supportFormReducer] ${action.type} action not allowed in 'initializing' state`
)
return state
case 'editing':
if (action.type === 'SUBMIT') {
return { type: 'submitting' }
}
console.warn(
`[SupportForm > supportFromReducer] ${action.type} action not allowed in 'filling_out' state`
)
return state
case 'submitting':
if (action.type === 'SUCCESS') {
return {
type: 'success',
sentProjectRef: action.sentProjectRef,
sentOrgSlug: action.sentOrgSlug,
sentCategory: action.sentCategory,
}
}
if (action.type === 'ERROR') {
return {
type: 'error',
message: action.message,
}
}
console.warn(
`[SupportForm > supportFormReducer] ${action.type} action not allowed in 'submitting' state`
)
return state
case 'success':
console.warn(`[SupportForm > supportFormReducer] ${action.type} allowed in 'success' state`)
return state
case 'error':
if (action.type === 'RETURN_TO_EDITING') {
return { type: 'editing' }
}
console.warn(`[SupportForm > supportFormReducer] ${action.type} allowed in 'success' state`)
return state
default:
return neverGuard(state)
}
}