-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdk.ts
More file actions
236 lines (200 loc) · 6.63 KB
/
sdk.ts
File metadata and controls
236 lines (200 loc) · 6.63 KB
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
import dedent from 'dedent';
export function sdkWrapperContent(): string {
return dedent`
import { useCallback, useEffect, useState } from 'react';
import AppExtensionsSDK, { Color, Command, Event } from '@pipedrive/app-extensions-sdk';
export type Surface = 'panel' | 'modal';
type Theme = 'light' | 'dark';
type PageState = 'visible' | 'hidden';
export interface IframeContext {
identifier?: string;
query: Record<string, string>;
data: unknown;
tokenPresent: boolean;
}
function normalizeTheme(theme: unknown): Theme {
return theme === 'dark' ? 'dark' : 'light';
}
function applyTheme(theme: Theme): void {
document.documentElement.setAttribute('data-theme', theme);
}
function sdkErrorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
function parseData(value: string | null): unknown {
if (!value) return null;
try {
return JSON.parse(value);
} catch {
return value;
}
}
export function readIframeContext(): IframeContext {
const params = new URLSearchParams(window.location.search);
const query = Object.fromEntries(params.entries());
return {
identifier: params.get('id') ?? undefined,
query,
data: parseData(params.get('data')),
tokenPresent: params.has('token'),
};
}
function initialSize(surface: Surface): { height: number; width?: number } {
return surface === 'modal' ? { height: 420, width: 640 } : { height: 360 };
}
export function usePipedriveSdk(surface: Surface) {
const [context] = useState<IframeContext>(() => readIframeContext());
const [sdk, setSdk] = useState<AppExtensionsSDK | null>(null);
const [status, setStatus] = useState('Connecting');
const [theme, setTheme] = useState<Theme>('light');
const [visibility, setVisibility] = useState('visible');
const [pageState, setPageState] = useState<PageState>(
document.visibilityState === 'hidden' ? 'hidden' : 'visible',
);
const [lastAction, setLastAction] = useState('None');
const [signedTokenPreview, setSignedTokenPreview] = useState('Not requested');
useEffect(() => {
const stops: Array<() => void> = [];
let cancelled = false;
const waitingTimer = window.setTimeout(() => {
if (!cancelled) setStatus('Waiting for Pipedrive iframe');
}, 2500);
async function initialize(): Promise<void> {
try {
if (!context.identifier) {
window.clearTimeout(waitingTimer);
applyTheme('light');
setTheme('light');
setStatus('Local preview');
setLastAction('Open this URL from Pipedrive to use SDK actions');
return;
}
const instance = new AppExtensionsSDK({ identifier: context.identifier });
const initialTheme = normalizeTheme(instance.userSettings.theme);
applyTheme(initialTheme);
setTheme(initialTheme);
const initializedSdk = await instance.initialize({ size: initialSize(surface) });
if (cancelled) return;
window.clearTimeout(waitingTimer);
setSdk(initializedSdk);
setStatus('Ready');
stops.push(
initializedSdk.listen(Event.USER_SETTINGS_CHANGE, ({ data }) => {
if (!data?.theme) return;
const nextTheme = normalizeTheme(data.theme);
applyTheme(nextTheme);
setTheme(nextTheme);
}),
);
stops.push(
initializedSdk.listen(Event.VISIBILITY, ({ data, error }) => {
if (error) {
setLastAction('Visibility event failed: ' + error);
return;
}
if (data) setVisibility(data.is_visible ? 'visible' : 'hidden');
}),
);
stops.push(
initializedSdk.listen(Event.PAGE_VISIBILITY_STATE, ({ data, error }) => {
if (error) {
setLastAction('Page state event failed: ' + error);
return;
}
if (data?.state) setPageState(data.state);
}),
);
} catch (error) {
if (!cancelled) {
window.clearTimeout(waitingTimer);
setStatus('SDK initialization failed');
setLastAction(sdkErrorMessage(error));
}
}
}
void initialize();
return () => {
cancelled = true;
window.clearTimeout(waitingTimer);
for (const stop of stops) stop();
};
}, [context.identifier, surface]);
const runSdkAction = useCallback(
async <T,>(successMessage: string, action: (client: AppExtensionsSDK) => Promise<T>): Promise<T | undefined> => {
if (!sdk) {
setLastAction('SDK is not ready');
return undefined;
}
try {
const result = await action(sdk);
setLastAction(successMessage);
return result;
} catch (error) {
setLastAction(sdkErrorMessage(error));
return undefined;
}
},
[sdk],
);
const showSnackbar = useCallback(async () => {
await runSdkAction('Snackbar sent', (client) =>
client.execute(Command.SHOW_SNACKBAR, {
message: 'Action completed',
link: {
url: 'https://app.pipedrive.com',
label: 'Open Pipedrive',
},
}),
);
}, [runSdkAction]);
const showConfirmation = useCallback(async () => {
const response = await runSdkAction('Confirmation answered', (client) =>
client.execute(Command.SHOW_CONFIRMATION, {
title: 'Confirm action',
description: 'This dialog is rendered by Pipedrive through the App Extensions SDK.',
okText: 'Confirm',
cancelText: 'Cancel',
okColor: Color.PRIMARY,
}),
);
if (response) setLastAction(response.confirmed ? 'Confirmed' : 'Cancelled');
}, [runSdkAction]);
const [isExpanded, setIsExpanded] = useState(false);
const resize = useCallback(async () => {
const nextExpanded = !isExpanded;
const size = surface === 'modal'
? (nextExpanded ? { height: 480, width: 720 } : { height: 420, width: 640 })
: (nextExpanded ? { height: 420 } : { height: 360 });
await runSdkAction(nextExpanded ? 'Expanded' : 'Collapsed', (client) =>
client.execute(Command.RESIZE, size),
);
setIsExpanded(nextExpanded);
}, [runSdkAction, surface, isExpanded]);
const getSignedToken = useCallback(async () => {
const response = await runSdkAction('Signed token received', (client) => client.execute(Command.GET_SIGNED_TOKEN));
if (response) {
setSignedTokenPreview(response.token.slice(0, 16) + '...' + response.token.length + ' chars');
}
}, [runSdkAction]);
return {
sdk,
context,
status,
theme,
visibility,
pageState,
lastAction,
signedTokenPreview,
isReady: sdk !== null,
runSdkAction,
actions: {
showSnackbar,
showConfirmation,
resize,
getSignedToken,
isExpanded,
},
};
}
`;
}