@backstage/plugin-home 0.5.9-next.2 → 0.5.10-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,613 @@
1
+ import React, { useCallback, useMemo, useEffect } from 'react';
2
+ import { WidthProvider, Responsive } from 'react-grid-layout';
3
+ import { createApiRef, useElementFilter, useApi, storageApiRef, getComponentData } from '@backstage/core-plugin-api';
4
+ import 'react-grid-layout/css/styles.css';
5
+ import 'react-resizable/css/styles.css';
6
+ import { makeStyles, createStyles, Dialog, DialogContent, Grid, Tooltip, DialogTitle, ListItemAvatar, useTheme } from '@material-ui/core';
7
+ import { compact } from 'lodash';
8
+ import useObservable from 'react-use/lib/useObservable';
9
+ import { ContentHeader, ErrorBoundary } from '@backstage/core-components';
10
+ import Typography from '@material-ui/core/Typography';
11
+ import IconButton from '@material-ui/core/IconButton';
12
+ import SettingsIcon from '@material-ui/icons/Settings';
13
+ import DeleteIcon from '@material-ui/icons/Delete';
14
+ import { withTheme } from '@rjsf/core';
15
+ import { Theme } from '@rjsf/material-ui';
16
+ import validator from '@rjsf/validator-ajv8';
17
+ import List from '@material-ui/core/List';
18
+ import ListItem from '@material-ui/core/ListItem';
19
+ import AddIcon from '@material-ui/icons/Add';
20
+ import ListItemText from '@material-ui/core/ListItemText';
21
+ import Button from '@material-ui/core/Button';
22
+ import SaveIcon from '@material-ui/icons/Save';
23
+ import EditIcon from '@material-ui/icons/Edit';
24
+ import CancelIcon from '@material-ui/icons/Cancel';
25
+ import { z } from 'zod';
26
+ import { useLocation } from 'react-router-dom';
27
+ import '@backstage/core-app-api';
28
+ import { stringifyEntityRef } from '@backstage/catalog-model';
29
+
30
+ const visitsApiRef = createApiRef({
31
+ id: "homepage.visits"
32
+ });
33
+
34
+ const Form = withTheme(Theme);
35
+ const useStyles$2 = makeStyles(
36
+ (theme) => createStyles({
37
+ iconGrid: {
38
+ height: "100%",
39
+ "& *": {
40
+ padding: 0
41
+ }
42
+ },
43
+ settingsOverlay: {
44
+ position: "absolute",
45
+ backgroundColor: "rgba(40, 40, 40, 0.93)",
46
+ width: "100%",
47
+ height: "100%",
48
+ top: 0,
49
+ left: 0,
50
+ padding: theme.spacing(2),
51
+ color: "white"
52
+ }
53
+ })
54
+ );
55
+ const WidgetSettingsOverlay = (props) => {
56
+ const { id, widget, settings, handleRemove, handleSettingsSave, deletable } = props;
57
+ const [settingsDialogOpen, setSettingsDialogOpen] = React.useState(false);
58
+ const styles = useStyles$2();
59
+ return /* @__PURE__ */ React.createElement("div", { className: styles.settingsOverlay }, widget.settingsSchema && /* @__PURE__ */ React.createElement(
60
+ Dialog,
61
+ {
62
+ open: settingsDialogOpen,
63
+ className: "widgetSettingsDialog",
64
+ onClose: () => setSettingsDialogOpen(false)
65
+ },
66
+ /* @__PURE__ */ React.createElement(DialogContent, null, /* @__PURE__ */ React.createElement(
67
+ Form,
68
+ {
69
+ validator,
70
+ showErrorList: false,
71
+ schema: widget.settingsSchema,
72
+ uiSchema: widget.uiSchema,
73
+ noHtml5Validate: true,
74
+ formData: settings,
75
+ formContext: { settings },
76
+ onSubmit: ({ formData, errors }) => {
77
+ if (errors.length === 0) {
78
+ handleSettingsSave(id, formData);
79
+ setSettingsDialogOpen(false);
80
+ }
81
+ }
82
+ }
83
+ ))
84
+ ), /* @__PURE__ */ React.createElement(
85
+ Grid,
86
+ {
87
+ container: true,
88
+ className: styles.iconGrid,
89
+ alignItems: "center",
90
+ justifyContent: "center"
91
+ },
92
+ widget.settingsSchema && /* @__PURE__ */ React.createElement(Grid, { item: true, className: "overlayGridItem" }, /* @__PURE__ */ React.createElement(Tooltip, { title: "Edit settings" }, /* @__PURE__ */ React.createElement(
93
+ IconButton,
94
+ {
95
+ color: "primary",
96
+ onClick: () => setSettingsDialogOpen(true)
97
+ },
98
+ /* @__PURE__ */ React.createElement(SettingsIcon, { fontSize: "large" })
99
+ ))),
100
+ deletable !== false && /* @__PURE__ */ React.createElement(Grid, { item: true, className: "overlayGridItem" }, /* @__PURE__ */ React.createElement(Tooltip, { title: "Delete widget" }, /* @__PURE__ */ React.createElement(IconButton, { color: "secondary", onClick: () => handleRemove(id) }, /* @__PURE__ */ React.createElement(DeleteIcon, { fontSize: "large" }))))
101
+ ));
102
+ };
103
+
104
+ const getTitle = (widget) => {
105
+ return widget.title || widget.name;
106
+ };
107
+ const AddWidgetDialog = (props) => {
108
+ const { widgets, handleAdd } = props;
109
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(DialogTitle, null, "Add new widget to dashboard"), /* @__PURE__ */ React.createElement(DialogContent, null, /* @__PURE__ */ React.createElement(List, { dense: true }, widgets.map((widget) => {
110
+ return /* @__PURE__ */ React.createElement(
111
+ ListItem,
112
+ {
113
+ key: widget.name,
114
+ button: true,
115
+ onClick: () => handleAdd(widget)
116
+ },
117
+ /* @__PURE__ */ React.createElement(ListItemAvatar, null, /* @__PURE__ */ React.createElement(AddIcon, null)),
118
+ /* @__PURE__ */ React.createElement(
119
+ ListItemText,
120
+ {
121
+ secondary: widget.description && /* @__PURE__ */ React.createElement(
122
+ Typography,
123
+ {
124
+ component: "span",
125
+ variant: "caption",
126
+ color: "textPrimary"
127
+ },
128
+ widget.description
129
+ ),
130
+ primary: /* @__PURE__ */ React.createElement(Typography, { variant: "body1", color: "textPrimary" }, getTitle(widget))
131
+ }
132
+ )
133
+ );
134
+ }))));
135
+ };
136
+
137
+ const useStyles$1 = makeStyles(
138
+ (theme) => createStyles({
139
+ contentHeaderBtn: {
140
+ marginLeft: theme.spacing(2)
141
+ },
142
+ widgetWrapper: {
143
+ "& > *:first-child": {
144
+ width: "100%",
145
+ height: "100%"
146
+ }
147
+ }
148
+ })
149
+ );
150
+ const CustomHomepageButtons = (props) => {
151
+ const {
152
+ editMode,
153
+ numWidgets,
154
+ clearLayout,
155
+ setAddWidgetDialogOpen,
156
+ changeEditMode,
157
+ defaultConfigAvailable,
158
+ restoreDefault
159
+ } = props;
160
+ const styles = useStyles$1();
161
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, !editMode && numWidgets > 0 ? /* @__PURE__ */ React.createElement(
162
+ Button,
163
+ {
164
+ variant: "contained",
165
+ color: "primary",
166
+ onClick: () => changeEditMode(true),
167
+ size: "small",
168
+ startIcon: /* @__PURE__ */ React.createElement(EditIcon, null)
169
+ },
170
+ "Edit"
171
+ ) : /* @__PURE__ */ React.createElement(React.Fragment, null, defaultConfigAvailable && /* @__PURE__ */ React.createElement(
172
+ Button,
173
+ {
174
+ variant: "contained",
175
+ className: styles.contentHeaderBtn,
176
+ onClick: restoreDefault,
177
+ size: "small",
178
+ startIcon: /* @__PURE__ */ React.createElement(CancelIcon, null)
179
+ },
180
+ "Restore defaults"
181
+ ), numWidgets > 0 && /* @__PURE__ */ React.createElement(
182
+ Button,
183
+ {
184
+ variant: "contained",
185
+ color: "secondary",
186
+ className: styles.contentHeaderBtn,
187
+ onClick: clearLayout,
188
+ size: "small",
189
+ startIcon: /* @__PURE__ */ React.createElement(DeleteIcon, null)
190
+ },
191
+ "Clear all"
192
+ ), /* @__PURE__ */ React.createElement(
193
+ Button,
194
+ {
195
+ variant: "contained",
196
+ className: styles.contentHeaderBtn,
197
+ onClick: () => setAddWidgetDialogOpen(true),
198
+ size: "small",
199
+ startIcon: /* @__PURE__ */ React.createElement(AddIcon, null)
200
+ },
201
+ "Add widget"
202
+ ), numWidgets > 0 && /* @__PURE__ */ React.createElement(
203
+ Button,
204
+ {
205
+ className: styles.contentHeaderBtn,
206
+ variant: "contained",
207
+ color: "primary",
208
+ onClick: () => changeEditMode(false),
209
+ size: "small",
210
+ startIcon: /* @__PURE__ */ React.createElement(SaveIcon, null)
211
+ },
212
+ "Save"
213
+ )));
214
+ };
215
+
216
+ const RSJFTypeSchema = z.any();
217
+ const RSJFTypeUiSchema = z.any();
218
+ const ReactElementSchema = z.any();
219
+ const LayoutSchema = z.any();
220
+ const LayoutConfigurationSchema = z.object({
221
+ component: ReactElementSchema,
222
+ x: z.number().nonnegative("x must be positive number"),
223
+ y: z.number().nonnegative("y must be positive number"),
224
+ width: z.number().positive("width must be positive number"),
225
+ height: z.number().positive("height must be positive number"),
226
+ movable: z.boolean().optional(),
227
+ deletable: z.boolean().optional(),
228
+ resizable: z.boolean().optional()
229
+ });
230
+ const WidgetSchema = z.object({
231
+ name: z.string(),
232
+ title: z.string().optional(),
233
+ description: z.string().optional(),
234
+ component: ReactElementSchema,
235
+ width: z.number().positive("width must be positive number").optional(),
236
+ height: z.number().positive("height must be positive number").optional(),
237
+ minWidth: z.number().positive("minWidth must be positive number").optional(),
238
+ maxWidth: z.number().positive("maxWidth must be positive number").optional(),
239
+ minHeight: z.number().positive("minHeight must be positive number").optional(),
240
+ maxHeight: z.number().positive("maxHeight must be positive number").optional(),
241
+ settingsSchema: RSJFTypeSchema.optional(),
242
+ uiSchema: RSJFTypeUiSchema.optional(),
243
+ movable: z.boolean().optional(),
244
+ deletable: z.boolean().optional(),
245
+ resizable: z.boolean().optional()
246
+ });
247
+ const GridWidgetSchema = z.object({
248
+ id: z.string(),
249
+ layout: LayoutSchema,
250
+ settings: z.record(z.string(), z.any()),
251
+ movable: z.boolean().optional(),
252
+ deletable: z.boolean().optional(),
253
+ resizable: z.boolean().optional()
254
+ });
255
+ const CustomHomepageGridStateV1Schema = z.object({
256
+ version: z.literal(1),
257
+ pages: z.record(z.string(), z.array(GridWidgetSchema))
258
+ });
259
+
260
+ const ResponsiveGrid = WidthProvider(Responsive);
261
+ const useStyles = makeStyles(
262
+ (theme) => createStyles({
263
+ responsiveGrid: {
264
+ "& .react-grid-item > .react-resizable-handle:after": {
265
+ position: "absolute",
266
+ content: '""',
267
+ borderStyle: "solid",
268
+ borderWidth: "0 0 20px 20px",
269
+ borderColor: `transparent transparent ${theme.palette.primary.light} transparent`
270
+ }
271
+ },
272
+ contentHeaderBtn: {
273
+ marginLeft: theme.spacing(2)
274
+ },
275
+ widgetWrapper: {
276
+ '& > div[class*="MuiCard-root"]': {
277
+ width: "100%",
278
+ height: "100%"
279
+ },
280
+ '& div[class*="MuiCardContent-root"]': {
281
+ overflow: "auto"
282
+ },
283
+ "& + .react-grid-placeholder": {
284
+ backgroundColor: theme.palette.primary.light
285
+ },
286
+ "&.edit > :active": {
287
+ cursor: "move"
288
+ }
289
+ }
290
+ })
291
+ );
292
+ function useHomeStorage(defaultWidgets) {
293
+ const key = "home";
294
+ const storageApi = useApi(storageApiRef).forBucket("home.customHomepage");
295
+ const setWidgets = useCallback(
296
+ (value) => {
297
+ const grid = {
298
+ version: 1,
299
+ pages: {
300
+ default: value
301
+ }
302
+ };
303
+ storageApi.set(key, JSON.stringify(grid));
304
+ },
305
+ [key, storageApi]
306
+ );
307
+ const homeSnapshot = useObservable(
308
+ storageApi.observe$(key),
309
+ storageApi.snapshot(key)
310
+ );
311
+ const widgets = useMemo(() => {
312
+ if (homeSnapshot.presence === "absent") {
313
+ return defaultWidgets;
314
+ }
315
+ try {
316
+ const grid = JSON.parse(homeSnapshot.value);
317
+ return CustomHomepageGridStateV1Schema.parse(grid).pages.default;
318
+ } catch (e) {
319
+ return defaultWidgets;
320
+ }
321
+ }, [homeSnapshot, defaultWidgets]);
322
+ return [widgets, setWidgets];
323
+ }
324
+ const convertConfigToDefaultWidgets = (config, availableWidgets) => {
325
+ const ret = config.map((conf, i) => {
326
+ var _a, _b;
327
+ const c = LayoutConfigurationSchema.parse(conf);
328
+ const name = React.isValidElement(c.component) ? getComponentData(c.component, "core.extensionName") : c.component;
329
+ if (!name) {
330
+ return null;
331
+ }
332
+ const widget = availableWidgets.find((w) => w.name === name);
333
+ if (!widget) {
334
+ return null;
335
+ }
336
+ const widgetId = `${widget.name}__${i}${Math.random().toString(36).slice(2)}`;
337
+ return {
338
+ id: widgetId,
339
+ layout: {
340
+ i: widgetId,
341
+ x: c.x,
342
+ y: c.y,
343
+ w: Math.min((_a = widget.maxWidth) != null ? _a : Number.MAX_VALUE, c.width),
344
+ h: Math.min((_b = widget.maxHeight) != null ? _b : Number.MAX_VALUE, c.height),
345
+ minW: widget.minWidth,
346
+ maxW: widget.maxWidth,
347
+ minH: widget.minHeight,
348
+ maxH: widget.maxHeight,
349
+ isDraggable: false,
350
+ isResizable: false
351
+ },
352
+ settings: {},
353
+ movable: conf.movable,
354
+ deletable: conf.deletable,
355
+ resizable: conf.resizable
356
+ };
357
+ });
358
+ return compact(ret);
359
+ };
360
+ const availableWidgetsFilter = (elements) => {
361
+ return elements.selectByComponentData({
362
+ key: "core.extensionName"
363
+ }).getElements().flatMap((elem) => {
364
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
365
+ const config = getComponentData(elem, "home.widget.config");
366
+ return [
367
+ WidgetSchema.parse({
368
+ component: elem,
369
+ name: getComponentData(elem, "core.extensionName"),
370
+ title: getComponentData(elem, "title"),
371
+ description: getComponentData(elem, "description"),
372
+ settingsSchema: (_a = config == null ? void 0 : config.settings) == null ? void 0 : _a.schema,
373
+ uiSchema: (_b = config == null ? void 0 : config.settings) == null ? void 0 : _b.uiSchema,
374
+ width: (_d = (_c = config == null ? void 0 : config.layout) == null ? void 0 : _c.width) == null ? void 0 : _d.defaultColumns,
375
+ minWidth: (_f = (_e = config == null ? void 0 : config.layout) == null ? void 0 : _e.width) == null ? void 0 : _f.minColumns,
376
+ maxWidth: (_h = (_g = config == null ? void 0 : config.layout) == null ? void 0 : _g.width) == null ? void 0 : _h.maxColumns,
377
+ height: (_j = (_i = config == null ? void 0 : config.layout) == null ? void 0 : _i.height) == null ? void 0 : _j.defaultRows,
378
+ minHeight: (_l = (_k = config == null ? void 0 : config.layout) == null ? void 0 : _k.height) == null ? void 0 : _l.minRows,
379
+ maxHeight: (_n = (_m = config == null ? void 0 : config.layout) == null ? void 0 : _m.height) == null ? void 0 : _n.maxRows
380
+ })
381
+ ];
382
+ });
383
+ };
384
+ const CustomHomepageGrid = (props) => {
385
+ var _a;
386
+ const styles = useStyles();
387
+ const theme = useTheme();
388
+ const availableWidgets = useElementFilter(
389
+ props.children,
390
+ availableWidgetsFilter,
391
+ [props]
392
+ );
393
+ const defaultLayout = props.config ? convertConfigToDefaultWidgets(props.config, availableWidgets) : [];
394
+ const [widgets, setWidgets] = useHomeStorage(defaultLayout);
395
+ const [addWidgetDialogOpen, setAddWidgetDialogOpen] = React.useState(false);
396
+ const editModeOn = widgets.find((w) => w.layout.isResizable) !== void 0;
397
+ const [editMode, setEditMode] = React.useState(editModeOn);
398
+ const getWidgetByName = (name) => {
399
+ return availableWidgets.find((widget) => widget.name === name);
400
+ };
401
+ const getWidgetNameFromKey = (key) => {
402
+ return key.split("__")[0];
403
+ };
404
+ const handleAdd = (widget) => {
405
+ var _a2, _b, _c, _d;
406
+ const widgetId = `${widget.name}__${widgets.length + 1}${Math.random().toString(36).slice(2)}`;
407
+ setWidgets([
408
+ ...widgets,
409
+ {
410
+ id: widgetId,
411
+ layout: {
412
+ i: widgetId,
413
+ x: 0,
414
+ y: Math.max(...widgets.map((w) => w.layout.y + w.layout.h)) + 1,
415
+ w: Math.min((_a2 = widget.maxWidth) != null ? _a2 : Number.MAX_VALUE, (_b = widget.width) != null ? _b : 12),
416
+ h: Math.min((_c = widget.maxHeight) != null ? _c : Number.MAX_VALUE, (_d = widget.height) != null ? _d : 4),
417
+ minW: widget.minWidth,
418
+ maxW: widget.maxWidth,
419
+ minH: widget.minHeight,
420
+ maxH: widget.maxHeight,
421
+ isResizable: editMode,
422
+ isDraggable: editMode
423
+ },
424
+ settings: {},
425
+ movable: widget.movable,
426
+ deletable: widget.deletable,
427
+ resizable: widget.resizable
428
+ }
429
+ ]);
430
+ setAddWidgetDialogOpen(false);
431
+ };
432
+ const handleRemove = (widgetId) => {
433
+ setWidgets(widgets.filter((w) => w.id !== widgetId));
434
+ };
435
+ const handleSettingsSave = (widgetId, widgetSettings) => {
436
+ const idx = widgets.findIndex((w) => w.id === widgetId);
437
+ if (idx >= 0) {
438
+ const widget = widgets[idx];
439
+ widget.settings = widgetSettings;
440
+ widgets[idx] = widget;
441
+ setWidgets(widgets);
442
+ }
443
+ };
444
+ const clearLayout = () => {
445
+ setWidgets([]);
446
+ };
447
+ const changeEditMode = (mode) => {
448
+ setEditMode(mode);
449
+ setWidgets(
450
+ widgets.map((w) => {
451
+ const resizable = w.resizable === false ? false : mode;
452
+ const movable = w.movable === false ? false : mode;
453
+ return {
454
+ ...w,
455
+ layout: { ...w.layout, isDraggable: movable, isResizable: resizable }
456
+ };
457
+ })
458
+ );
459
+ };
460
+ const handleLayoutChange = (newLayout, _) => {
461
+ if (editMode) {
462
+ const newWidgets = newLayout.map((l) => {
463
+ const widget = widgets.find((w) => w.id === l.i);
464
+ return {
465
+ ...widget,
466
+ layout: l
467
+ };
468
+ });
469
+ setWidgets(newWidgets);
470
+ }
471
+ };
472
+ const handleRestoreDefaultConfig = () => {
473
+ setWidgets(
474
+ defaultLayout.map((w) => {
475
+ const resizable = w.resizable === false ? false : editMode;
476
+ const movable = w.movable === false ? false : editMode;
477
+ return {
478
+ ...w,
479
+ layout: {
480
+ ...w.layout,
481
+ isDraggable: movable,
482
+ isResizable: resizable
483
+ }
484
+ };
485
+ })
486
+ );
487
+ };
488
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(ContentHeader, { title: "" }, /* @__PURE__ */ React.createElement(
489
+ CustomHomepageButtons,
490
+ {
491
+ editMode,
492
+ numWidgets: widgets.length,
493
+ clearLayout,
494
+ setAddWidgetDialogOpen,
495
+ changeEditMode,
496
+ defaultConfigAvailable: props.config !== void 0,
497
+ restoreDefault: handleRestoreDefaultConfig
498
+ }
499
+ )), /* @__PURE__ */ React.createElement(
500
+ Dialog,
501
+ {
502
+ open: addWidgetDialogOpen,
503
+ onClose: () => setAddWidgetDialogOpen(false)
504
+ },
505
+ /* @__PURE__ */ React.createElement(AddWidgetDialog, { widgets: availableWidgets, handleAdd })
506
+ ), !editMode && widgets.length === 0 && /* @__PURE__ */ React.createElement(Typography, { variant: "h5", align: "center" }, "No widgets added. Start by clicking the 'Add widget' button."), /* @__PURE__ */ React.createElement(
507
+ ResponsiveGrid,
508
+ {
509
+ className: styles.responsiveGrid,
510
+ measureBeforeMount: true,
511
+ compactType: props.compactType,
512
+ style: props.style,
513
+ allowOverlap: props.allowOverlap,
514
+ preventCollision: props.preventCollision,
515
+ draggableCancel: ".overlayGridItem,.widgetSettingsDialog,.disabled",
516
+ containerPadding: props.containerPadding,
517
+ margin: props.containerMargin,
518
+ breakpoints: props.breakpoints ? props.breakpoints : theme.breakpoints.values,
519
+ cols: props.cols ? props.cols : { xl: 12, lg: 12, md: 10, sm: 6, xs: 4, xxs: 2 },
520
+ rowHeight: (_a = props.rowHeight) != null ? _a : 60,
521
+ onLayoutChange: handleLayoutChange,
522
+ layouts: { xl: widgets.map((w) => w.layout) }
523
+ },
524
+ widgets.map((w) => {
525
+ var _a2;
526
+ const l = w.layout;
527
+ const widgetName = getWidgetNameFromKey(l.i);
528
+ const widget = getWidgetByName(widgetName);
529
+ if (!widget || !widget.component) {
530
+ return null;
531
+ }
532
+ const widgetProps = {
533
+ ...widget.component.props,
534
+ ...(_a2 = w.settings) != null ? _a2 : {}
535
+ };
536
+ return /* @__PURE__ */ React.createElement(
537
+ "div",
538
+ {
539
+ key: l.i,
540
+ className: `${styles.widgetWrapper} ${editMode && "edit"} ${w.movable === false && "disabled"}`
541
+ },
542
+ /* @__PURE__ */ React.createElement(ErrorBoundary, null, /* @__PURE__ */ React.createElement(widget.component.type, { ...widgetProps })),
543
+ editMode && /* @__PURE__ */ React.createElement(
544
+ WidgetSettingsOverlay,
545
+ {
546
+ id: l.i,
547
+ widget,
548
+ handleRemove,
549
+ handleSettingsSave,
550
+ settings: w.settings,
551
+ deletable: w.deletable
552
+ }
553
+ )
554
+ );
555
+ })
556
+ ));
557
+ };
558
+
559
+ const getToEntityRef = ({
560
+ rootPath = "catalog",
561
+ stringifyEntityRefImpl = stringifyEntityRef
562
+ } = {}) => ({ pathname }) => {
563
+ const regex = new RegExp(
564
+ `^/${rootPath}/(?<namespace>[^/]+)/(?<kind>[^/]+)/(?<name>[^/]+)`
565
+ );
566
+ const result = regex.exec(pathname);
567
+ if (!result || !(result == null ? void 0 : result.groups))
568
+ return void 0;
569
+ const entity = {
570
+ namespace: result.groups.namespace,
571
+ kind: result.groups.kind,
572
+ name: result.groups.name
573
+ };
574
+ return stringifyEntityRefImpl(entity);
575
+ };
576
+ const getVisitName = ({ rootPath = "catalog", document = global.document } = {}) => ({ pathname }) => {
577
+ const regex = new RegExp(
578
+ `^/${rootPath}/(?<namespace>[^/]+)/(?<kind>[^/]+)/(?<name>[^/]+)`
579
+ );
580
+ let result = regex.exec(pathname);
581
+ if (result && (result == null ? void 0 : result.groups))
582
+ return result.groups.name;
583
+ result = /^\/(?<name>[^\/]+)$/.exec(pathname);
584
+ if (result && (result == null ? void 0 : result.groups))
585
+ return result.groups.name;
586
+ return document.title;
587
+ };
588
+ const VisitListener = ({
589
+ children,
590
+ toEntityRef,
591
+ visitName
592
+ }) => {
593
+ const visitsApi = useApi(visitsApiRef);
594
+ const { pathname } = useLocation();
595
+ const toEntityRefImpl = toEntityRef != null ? toEntityRef : getToEntityRef();
596
+ const visitNameImpl = visitName != null ? visitName : getVisitName();
597
+ useEffect(() => {
598
+ const requestId = requestAnimationFrame(() => {
599
+ visitsApi.save({
600
+ visit: {
601
+ name: visitNameImpl({ pathname }),
602
+ pathname,
603
+ entityRef: toEntityRefImpl({ pathname })
604
+ }
605
+ });
606
+ });
607
+ return () => cancelAnimationFrame(requestId);
608
+ }, [visitsApi, pathname, toEntityRefImpl, visitNameImpl]);
609
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, children);
610
+ };
611
+
612
+ export { CustomHomepageGrid as C, VisitListener as V, visitsApiRef as v };
613
+ //# sourceMappingURL=VisitListener-fa6f752b.esm.js.map