-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathAppWidgets.tsx
More file actions
214 lines (191 loc) · 7.87 KB
/
AppWidgets.tsx
File metadata and controls
214 lines (191 loc) · 7.87 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
import { DashboardCard } from "@dashboard/components/Card";
import Link from "@dashboard/components/Link";
import { APP_VERSION } from "@dashboard/config";
import { AppAvatar } from "@dashboard/extensions/components/AppAvatar/AppAvatar";
import { IframePost } from "@dashboard/extensions/components/IframePost/IframePost";
import { appExtensionManifestOptionsSchema } from "@dashboard/extensions/domain/app-extension-manifest-options";
import { isUrlAbsolute } from "@dashboard/extensions/isUrlAbsolute";
import { extensionActions } from "@dashboard/extensions/messages";
import { type ExtensionWithParams } from "@dashboard/extensions/types";
import { type AppDetailsUrlMountQueryParams, ExtensionsUrls } from "@dashboard/extensions/urls";
import { AppFrame } from "@dashboard/extensions/views/ViewManifestExtension/components/AppFrame/AppFrame";
import useNavigator from "@dashboard/hooks/useNavigator";
import { type ThemeType } from "@saleor/app-sdk/app-bridge";
import { Box, Text } from "@saleor/macaw-ui-next";
import { ExternalLink } from "lucide-react";
import { useRef } from "react";
import { useIntl } from "react-intl";
type AppWidgetsProps = {
extensions: ExtensionWithParams[];
params: AppDetailsUrlMountQueryParams;
};
// TODO We will add size negotiations after render
const defaultIframeSize = 200;
export const AppWidgets = ({ extensions, params }: AppWidgetsProps) => {
const navigate = useNavigator();
const themeRef = useRef<ThemeType>();
const intl = useIntl();
const groupedByApp = extensions.reduce(
(group, extension) => {
const appId = extension.app.id;
const appGroup = group[appId];
if (appGroup) {
group[appId].extensions.push(extension);
} else {
group[appId] = {
app: extension.app,
extensions: [extension],
};
}
return group;
},
{} as Record<string, { app: ExtensionWithParams["app"]; extensions: ExtensionWithParams[] }>,
);
// Sort alphabetically, so order of apps is constant, even if API returns them differently. This allows more consistent UX
const sortedByAppName = Object.entries(groupedByApp).sort((a, b) =>
(a[1].app.name ?? "" < (b[1].app.name ?? "")) ? -1 : 1,
);
return (
<DashboardCard>
<DashboardCard.Header>
<DashboardCard.Title>Apps</DashboardCard.Title>
</DashboardCard.Header>
<DashboardCard.Content>
{sortedByAppName.map(([appId, appWithExtensions]) => {
const logo = appWithExtensions.app.brand?.logo.default;
const appPageUrl = ExtensionsUrls.resolveViewManifestExtensionUrl(
appWithExtensions.app.id,
{},
);
return (
<Box marginBottom={8} key={appId}>
<Box display="flex" alignItems="center" marginBottom={2}>
<AppAvatar size={6} logo={logo ? { source: logo } : undefined} marginRight={2} />
<Text
onClick={e => {
navigate(appPageUrl);
e.preventDefault();
}}
as="a"
size={3}
color="default2"
href={appPageUrl}
>
{appWithExtensions.app.name}
</Text>
</Box>
{appWithExtensions.extensions.map(ext => {
const settingsValidation = appExtensionManifestOptionsSchema.safeParse(
ext.settings,
);
if (!settingsValidation.success) {
return (
<Box marginTop={2} key={ext.id}>
<Text>Error rendering extension</Text>
</Box>
);
}
const settings = settingsValidation.data;
const isIframeType = ext.targetName === "WIDGET";
const isIframePost =
ext.targetName === "WIDGET" && settings?.widgetTarget?.method === "POST";
const isExtensionAbsoluteUrl = isUrlAbsolute(ext.url);
const extensionUrl = isExtensionAbsoluteUrl ? ext.url : ext.app.appUrl + ext.url;
const GETappIframeUrl = ExtensionsUrls.resolveAppIframeUrl(
ext.app.id,
extensionUrl,
{
id: ext.app.id,
theme: themeRef.current!,
},
);
const renderIframeGETvariant = () => (
<Box __height={defaultIframeSize}>
<Text size={3} color="default2" href={appPageUrl}>
{ext.label}
</Text>
<AppFrame
target="WIDGET"
src={GETappIframeUrl}
appToken={ext.accessToken}
appId={ext.app.id}
dashboardVersion={APP_VERSION}
params={params}
/>
</Box>
);
const renderIframePOSTvariant = () => (
<Box>
<Text size={3} color="default2" href={appPageUrl}>
{ext.label}
</Text>
<IframePost
appId={ext.app.id}
accessToken={ext.accessToken}
extensionId={ext.id}
extensionUrl={extensionUrl}
params={params}
/>
</Box>
);
const renderNonIframeExtension = () => {
const onClick = () => ext.open(params);
switch (ext.targetName) {
case "APP_PAGE":
return (
<Box marginTop={2}>
<Link
onClick={onClick}
title={intl.formatMessage(extensionActions.redirectToAppPage)}
>
{ext.label}
</Link>
</Box>
);
case "NEW_TAB":
return (
<Box marginTop={2}>
<Link
onClick={onClick}
title={intl.formatMessage(extensionActions.openInNewTab)}
>
{ext.label}{" "}
<ExternalLink
style={{ width: 16, height: 16, verticalAlign: "text-bottom" }}
/>
</Link>
</Box>
);
case "POPUP":
return (
<Box marginTop={2}>
<Link
onClick={onClick}
title={intl.formatMessage(extensionActions.openInPopup)}
>
{ext.label}...
</Link>
</Box>
);
case "WIDGET":
throw new Error("You should not render widget type as link");
}
};
const renderPOSTiframe = isIframeType && isIframePost;
const renderGETiframe = isIframeType && !isIframePost;
const renderNonIframe = !isIframeType;
return (
<Box marginBottom={4} key={ext.id}>
{renderGETiframe && renderIframeGETvariant()}
{renderPOSTiframe && renderIframePOSTvariant()}
{renderNonIframe && renderNonIframeExtension()}
</Box>
);
})}
</Box>
);
})}
</DashboardCard.Content>
</DashboardCard>
);
};