-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathRecentDesigns.js
More file actions
126 lines (116 loc) · 3.73 KB
/
RecentDesigns.js
File metadata and controls
126 lines (116 loc) · 3.73 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
import React, { useEffect, useState } from "react";
import {
Card,
CardHeader,
CardContent,
List,
ListItem,
ListItemIcon,
ListItemText,
IconButton,
Typography,
CircularProgress,
Box,
} from "@mui/material";
import { ExternalLinkIcon } from "@sistent/sistent";
import { DesignIcon } from "@sistent/sistent";
import { getFormatDate } from "@sistent/sistent";
import { SectionCard } from "./styledComponents";
import { ProxyUrl } from "../utils/constants";
import { useCallback } from "react";
export const REFRESH_RECENT_DESIGNS_EVENT = "refreshRecentDesigns";
// Create a custom event with optional detail payload
export const refreshRecentDesignsEvent = new CustomEvent(
REFRESH_RECENT_DESIGNS_EVENT,
{},
);
export default function RecentDesignsCard({ isDarkTheme }) {
const [designs, setDesigns] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const fetchRecentDesigns = useCallback(() => {
setLoading(true);
fetch(ProxyUrl + "/api/pattern?page=0&pagesize=10&search=&order=updated_at") // Replace with your actual API endpoint
.then(async (res) => {
if (!res.ok) {
const message = await res.text();
throw new Error(message || "Failed to fetch designs");
}
return res.json();
})
.then((data) => {
console.log("data", data);
setDesigns(data?.patterns || []);
setLoading(false);
})
.catch((err) => {
setError(err.message || "Unknown error");
setLoading(false);
});
}, []);
useEffect(() => {
fetchRecentDesigns();
}, [fetchRecentDesigns]);
useEffect(() => {
document.addEventListener(REFRESH_RECENT_DESIGNS_EVENT, fetchRecentDesigns);
return () =>
document.removeEventListener(
REFRESH_RECENT_DESIGNS_EVENT,
fetchRecentDesigns,
);
}, [fetchRecentDesigns]);
const openDesign = (design) => {
const name = design.name.replace(" ", "-").toLowerCase();
// const url = `http://localhost:9081/extension/meshmap?mode=design&design=${design.id}`;
const url = `https://cloud.layer5.io/catalog/content/my-designs/${name}-${design.id}?source=%257B%2522type%2522%253A%2522my-designs%2522%257D`;
window.ddClient.host.openExternal(url);
// window.location.href = url;
};
return (
<SectionCard
isDarkTheme={isDarkTheme}
sx={{ flexDirection: "column", pt: "0.5rem", pb: "0.5rem" }}
>
<CardHeader title="Recent Designs" />
<CardContent>
{loading ? (
<Box display="flex" justifyContent="center" my={3}>
<CircularProgress />
</Box>
) : error ? (
<Typography color="error">{error}</Typography>
) : designs.length === 0 ? (
<Typography color="text.secondary">
No recent designs found.
</Typography>
) : (
<List disablePadding>
{designs.map((design) => (
<ListItem
key={design.id}
onClick={() => openDesign(design)}
secondaryAction={
<IconButton
onClick={() => openDesign(design)}
edge="end"
aria-label="open"
>
<ExternalLinkIcon fill="#eee" />
</IconButton>
}
>
<ListItemIcon>
<DesignIcon />
</ListItemIcon>
<ListItemText primary={design.name} />
<ListItemText
primary={`Updated ${getFormatDate(design.updated_at)}`}
/>
</ListItem>
))}
</List>
)}
</CardContent>
</SectionCard>
);
}