Connect.utils.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { FRAMEWORKS, MOBILES } from './Connect.constants'
  2. type FieldValue = string | boolean | string[] | undefined
  3. interface FrameworkLibraryInput {
  4. framework?: FieldValue
  5. frameworkVariant?: FieldValue
  6. library?: FieldValue
  7. [key: string]: FieldValue
  8. }
  9. export function resolveFrameworkLibraryKey(state: FrameworkLibraryInput): string | null {
  10. const { framework, frameworkVariant, library } = state
  11. if (!framework) return null
  12. if (library) return String(library)
  13. const allFrameworks = [...FRAMEWORKS, ...MOBILES]
  14. const selectedFramework = allFrameworks.find((f) => f.key === framework)
  15. if (!selectedFramework?.children?.length) return null
  16. if (frameworkVariant) {
  17. const variant = selectedFramework.children.find((c) => c.key === frameworkVariant)
  18. if (variant?.children?.length) {
  19. return variant.children[0].key
  20. }
  21. }
  22. const firstChild = selectedFramework.children[0]
  23. if (firstChild?.children?.length) {
  24. return firstChild.children[0].key
  25. }
  26. return firstChild?.key ?? null
  27. }