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