-
-
Notifications
You must be signed in to change notification settings - Fork 10k
/
Integrations.tsx
241 lines (218 loc) · 9.18 KB
/
Integrations.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import NiceModal from '@ebay/nice-modal-react';
import React, {useState} from 'react';
import TopLevelGroup from '../../TopLevelGroup';
import usePinturaEditor from '../../../hooks/usePinturaEditor';
import {ReactComponent as AmpIcon} from '../../../assets/icons/amp.svg';
import {Button, ConfirmationModal, Icon, List, ListItem, NoValueLabel, TabView, showToast, withErrorBoundary} from '@tryghost/admin-x-design-system';
import {ReactComponent as FirstPromoterIcon} from '../../../assets/icons/firstpromoter.svg';
import {Integration, useBrowseIntegrations, useDeleteIntegration} from '@tryghost/admin-x-framework/api/integrations';
import {ReactComponent as PinturaIcon} from '../../../assets/icons/pintura.svg';
import {ReactComponent as SlackIcon} from '../../../assets/icons/slack.svg';
import {ReactComponent as UnsplashIcon} from '../../../assets/icons/unsplash.svg';
import {ReactComponent as ZapierIcon} from '../../../assets/icons/zapier.svg';
import {getSettingValues} from '@tryghost/admin-x-framework/api/settings';
import {useGlobalData} from '../../providers/GlobalDataProvider';
import {useHandleError} from '@tryghost/admin-x-framework/hooks';
import {useRouting} from '@tryghost/admin-x-framework/routing';
interface IntegrationItemProps {
icon?: React.ReactNode,
title: string,
detail: string,
action: () => void;
onDelete?: () => void;
active?: boolean;
disabled?: boolean;
testId?: string;
custom?: boolean;
}
const IntegrationItem: React.FC<IntegrationItemProps> = ({
icon,
title,
detail,
action,
onDelete,
active,
disabled,
testId,
custom = false
}) => {
const {updateRoute} = useRouting();
const handleClick = () => {
if (disabled) {
updateRoute({route: 'pro', isExternal: true});
} else {
action();
}
};
const buttons = custom ?
<Button color='red' label='Delete' link onClick={onDelete} />
:
(disabled ?
<Button icon='lock-locked' label='Upgrade' link onClick={handleClick} /> :
<Button color='green' label='Configure' link onClick={handleClick} />
);
return <ListItem
action={buttons}
avatar={icon}
className={disabled ? 'opacity-50 saturate-0' : ''}
detail={detail}
hideActions={!disabled}
testId={testId}
title={active ? <span className='inline-flex items-center gap-1'>{title} <span className='inline-flex items-center rounded-full bg-[rgba(48,207,67,0.15)] px-1.5 py-0.5 text-2xs font-semibold uppercase tracking-wide text-green'>Active</span></span> : title}
onClick={handleClick}
/>;
};
const BuiltInIntegrations: React.FC = () => {
const {config} = useGlobalData();
const {updateRoute} = useRouting();
const openModal = (modal: string) => {
updateRoute(modal);
};
const zapierDisabled = config.hostSettings?.limits?.customIntegrations?.disabled;
const pinturaEditor = usePinturaEditor();
const {settings} = useGlobalData();
const [ampEnabled, unsplashEnabled, firstPromoterEnabled, slackUrl, slackUsername] = getSettingValues<boolean>(settings, ['amp', 'unsplash', 'pintura', 'firstpromoter', 'slack_url', 'slack_username']);
return (
<List titleSeparator={false}>
<IntegrationItem
action={() => {
openModal('integrations/zapier');
}}
detail='Automation for your apps'
disabled={zapierDisabled}
icon={<ZapierIcon className='h-8 w-8' />}
testId='zapier-integration'
title='Zapier' />
<IntegrationItem
action={() => {
openModal('integrations/slack');
}}
active={slackUrl && slackUsername}
detail='A messaging app for teams'
icon={<SlackIcon className='h-8 w-8' />}
title='Slack' />
<IntegrationItem
action={() => {
openModal('integrations/amp');
}}
active={ampEnabled}
detail='Google AMP will be removed in Ghost 6.0'
icon={<AmpIcon className='h-8 w-8' />}
title='AMP' />
<IntegrationItem
action={() => {
openModal('integrations/unsplash');
}}
active={unsplashEnabled}
detail='Beautiful, free photos'
icon={<UnsplashIcon className='h-8 w-8' />}
title='Unsplash' />
<IntegrationItem
action={() => {
openModal('integrations/firstpromoter');
}}
active={firstPromoterEnabled}
detail='Launch your member referral program'
icon={<FirstPromoterIcon className='h-8 w-8' />}
title='FirstPromoter' />
<IntegrationItem
action={() => {
openModal('integrations/pintura');
}}
active={pinturaEditor.isEnabled}
detail='Advanced image editing' icon=
{<PinturaIcon className='h-8 w-8' />} title
='Pintura' />
</List>
);
};
const CustomIntegrations: React.FC<{integrations: Integration[]}> = ({integrations}) => {
const {updateRoute} = useRouting();
const {mutateAsync: deleteIntegration} = useDeleteIntegration();
const handleError = useHandleError();
if (integrations.length) {
return (
<List borderTop={false}>
{integrations.map(integration => (
<IntegrationItem
action={() => updateRoute({route: `integrations/${integration.id}`})}
detail={integration.description || 'No description'}
icon={
integration.icon_image ?
<img className='h-8 w-8 object-cover' role='presentation' src={integration.icon_image} /> :
<Icon className='w-8' name='integration' />
}
title={integration.name}
custom
onDelete={() => {
NiceModal.show(ConfirmationModal, {
title: 'Are you sure?',
prompt: 'Deleting this integration will remove all webhooks and api keys associated with it.',
okColor: 'red',
okLabel: 'Delete Integration',
onOk: async (confirmModal) => {
try {
await deleteIntegration(integration.id);
confirmModal?.remove();
showToast({
message: 'Integration deleted',
type: 'success'
});
} catch (e) {
handleError(e);
}
}
});
}}
/>)
)}
</List>
);
} else {
return <NoValueLabel icon='integration'>
No custom integration.
</NoValueLabel>;
}
};
const Integrations: React.FC<{ keywords: string[] }> = ({keywords}) => {
const [selectedTab, setSelectedTab] = useState<'built-in' | 'custom'>('built-in');
const {data: {integrations} = {integrations: []}} = useBrowseIntegrations();
const {updateRoute} = useRouting();
const tabs = [
{
id: 'built-in',
title: 'Built-in',
contents: <BuiltInIntegrations />
},
{
id: 'custom',
title: 'Custom',
contents: <CustomIntegrations integrations={integrations.filter(integration => integration.type === 'custom')} />
}
] as const;
const buttons = (
<Button className='hidden md:!visible md:!block' color='green' label='Add custom integration' link linkWithPadding onClick={() => {
updateRoute('integrations/new');
setSelectedTab('custom');
}} />
);
return (
<TopLevelGroup
customButtons={buttons}
description="Make Ghost work with apps and tools"
keywords={keywords}
navid='integrations'
testId='integrations'
title="Integrations"
>
<div className='flex justify-center rounded border border-green px-4 py-2 md:hidden'>
<Button color='green' label='Add custom integration' link onClick={() => {
updateRoute('integrations/new');
setSelectedTab('custom');
}} />
</div>
<TabView<'built-in' | 'custom'> selectedTab={selectedTab} tabs={tabs} onTabChange={setSelectedTab} />
</TopLevelGroup>
);
};
export default withErrorBoundary(Integrations, 'Integrations');