Files
CharisandJoshen Lim 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

63 lines
1.6 KiB
TypeScript

import { parseAsBoolean, useQueryState } from 'nuqs'
import {
createContext,
useCallback,
useContext,
useMemo,
useRef,
type PropsWithChildren,
} from 'react'
interface HighlightProjectRefContextValue<T extends HTMLElement = HTMLDivElement> {
ref: React.RefObject<T>
shouldHighlightRef: boolean
setShouldHighlightRef: (value: boolean) => void
scrollToRef: () => void
}
const HighlightProjectRefContext = createContext<HighlightProjectRefContextValue | undefined>(
undefined
)
export function HighlightProjectRefProvider({ children }: PropsWithChildren) {
const projectRefContainerRef = useRef<HTMLDivElement>(null)
const [shouldHighlightRef, setShouldHighlightRef] = useQueryState(
'highlightRef',
parseAsBoolean.withDefault(false).withOptions({ clearOnDefault: true })
)
const scrollToRef = useCallback(() => {
projectRefContainerRef.current?.scrollIntoView({
behavior: 'smooth',
block: 'center',
inline: 'center',
})
}, [])
const ctx = useMemo(
() => ({
ref: projectRefContainerRef,
shouldHighlightRef,
setShouldHighlightRef,
scrollToRef,
}),
[shouldHighlightRef, setShouldHighlightRef, scrollToRef]
)
return (
<HighlightProjectRefContext.Provider value={ctx}>
{children}
</HighlightProjectRefContext.Provider>
)
}
export function useHighlightProjectRefContext() {
const context = useContext(HighlightProjectRefContext)
if (!context) {
throw new Error(
'useHighlightProjectRefContext must be used within a HighlightProjectRefProvider'
)
}
return context
}