Connect.utils.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { ConnectionType, FRAMEWORKS, MOBILES, ORMS } from './Connect.constants'
  2. export function getProjectRef(url: string): string | null {
  3. const regex: RegExp = /https:\/\/([^\.]+)\./
  4. const match: RegExpMatchArray | null = url.match(regex)
  5. if (match) {
  6. return match[1]
  7. } else {
  8. return null
  9. }
  10. }
  11. export const getContentFilePath = ({
  12. connectionObject,
  13. selectedParent,
  14. selectedChild,
  15. selectedGrandchild,
  16. }: {
  17. selectedParent: string
  18. selectedChild: string
  19. selectedGrandchild: string
  20. connectionObject: ConnectionType[]
  21. }) => {
  22. const parent = connectionObject.find((item) => item.key === selectedParent)
  23. if (parent) {
  24. const child = parent.children.find((child) => child.key === selectedChild)
  25. // check grandchild first, then child, then parent as the fallback
  26. if (child) {
  27. const grandchild = child.children.find((grandchild) => grandchild.key === selectedGrandchild)
  28. if (grandchild) {
  29. return `${selectedParent}/${selectedChild}/${selectedGrandchild}`
  30. } else {
  31. return `${selectedParent}/${selectedChild}`
  32. }
  33. } else {
  34. return selectedParent
  35. }
  36. }
  37. return ''
  38. }
  39. export function inferConnectTabFromParentKey(
  40. parentKey: string | null
  41. ): 'frameworks' | 'mobiles' | 'orms' | null {
  42. if (!parentKey) return null
  43. if (FRAMEWORKS.find((x: ConnectionType) => x.key === parentKey)) return 'frameworks'
  44. if (MOBILES.find((x: ConnectionType) => x.key === parentKey)) return 'mobiles'
  45. if (ORMS.find((x: ConnectionType) => x.key === parentKey)) return 'orms'
  46. return null
  47. }