-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
144 lines (120 loc) · 4.49 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import React, { useMemo } from 'react'
import { Icon } from '@iconify/react'
import toast from 'react-hot-toast'
import { useLocation, useParams } from 'react-router-dom'
import { useTheme } from 'styled-components'
import { LoadingButton } from '@workduck-io/mex-components'
import { mog } from '@mexit/core'
import { useLinks } from '../../Hooks/useLinks'
import { usePortals } from '../../Hooks/usePortals'
import usePortalStore from '../../Stores/usePortalStore'
import { GlobalSectionHeader, GlobalSectionContainer } from '../../Style/GlobalSection'
import { QuickLink } from '../NodeSelect/NodeSelect'
import CreateInput from '../createInput'
import ServiceHeader from './ServiceHeader'
import ServiceInfo from './ServiceInfo'
import { useNamespaces } from '../../Hooks/useNamespaces'
const Portals = () => {
const theme = useTheme()
const params = useParams()
const location = useLocation()
const query = new URLSearchParams(location.search)
const serviceId = query.get('serviceId')
const [isEdit, setIsEdit] = React.useState(false)
const [isLoading, setIsLoading] = React.useState(false)
const [parentNote, setParentNote] = React.useState<QuickLink>(undefined)
const { getPathFromNodeid } = useLinks()
const { connectToPortal, updateParentNote } = usePortals()
const apps = usePortalStore((store) => store.apps)
const connectedPortals = usePortalStore((store) => store.connectedPortals)
const getIsPortalConnected = usePortalStore((store) => store.getIsPortalConnected)
const { getNamespaceOfNodeid } = useNamespaces()
const actionGroup = apps[params.actionGroupId]
mog('Inside Specific Portal', { location, query, serviceId, apps, connectedPortals, params, actionGroup })
const { connectedPortalInfo, parentNoteName } = useMemo(() => {
const connectedPortalInfo = getIsPortalConnected(actionGroup.actionGroupId)
let parentNoteName = undefined
if (connectedPortalInfo) {
const namespace = getNamespaceOfNodeid(connectedPortalInfo?.parentNodeId)
parentNoteName = {
path: getPathFromNodeid(connectedPortalInfo?.parentNodeId),
namespace: namespace?.id
}
}
return {
parentNoteName,
connectedPortalInfo
}
}, [params.actionGroupId, connectedPortals])
const isNewPortal = serviceId && !connectedPortalInfo
const onClick = () => {
const url = actionGroup?.authConfig?.authURL
if (url) window.open(url, '_blank')
}
const onSaveDetails = async () => {
if (!isEdit && !isNewPortal) {
setIsEdit(true)
return
}
if (!parentNote && !connectedPortalInfo) {
toast('Select a Note first')
return
}
try {
setIsLoading(true)
const isUpdate = connectedPortalInfo && connectedPortalInfo.parentNodeId !== parentNote?.nodeid
if (isUpdate) {
await updateParentNote(
params.actionGroupId,
connectedPortalInfo.serviceId,
parentNote.nodeid,
parentNote?.namespace
)
toast(`Updated Successfully! All new notes will be added under "${parentNote.text}"`)
} else {
await connectToPortal(params.actionGroupId, serviceId, parentNote?.nodeid, parentNote?.namespace)
toast(`Updated Successfully! All new notes will be added under "${parentNoteName}"`)
}
} catch (err) {
mog('Error connecting to portal', { err })
} finally {
setIsLoading(false)
setIsEdit(false)
}
}
const onNodeChange = (note: QuickLink) => {
setParentNote(note)
}
return (
<ServiceInfo>
<ServiceHeader
description={actionGroup.description}
icon={actionGroup.icon}
isConnected={!!connectedPortalInfo}
title={actionGroup.name}
onClick={onClick}
/>
{(isNewPortal || connectedPortalInfo) && (
<GlobalSectionContainer>
<div>Select a Parent Note</div>
<GlobalSectionHeader>
<CreateInput
value={parentNoteName}
autoFocus={isNewPortal || isEdit}
disabled={!isNewPortal && !isEdit}
onChange={onNodeChange}
/>
</GlobalSectionHeader>
<LoadingButton dots={2} loading={isLoading} onClick={onSaveDetails} transparent>
<Icon
color={theme.colors.primary}
width={24}
icon={isEdit || isNewPortal ? 'teenyicons:tick-circle-outline' : 'clarity:note-edit-line'}
/>
</LoadingButton>
</GlobalSectionContainer>
)}
</ServiceInfo>
)
}
export default Portals