|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { useEffect, useMemo, useState } from "react" |
|
4
|
import { CloudAnnouncement } from "types" |
|
5
|
import { useSiteConfig } from "../../../providers/SiteConfig" |
|
6
|
import { getAnnouncements } from "../../../utils/get-announcements" |
|
7
|
import { ContentMenuSection } from "../Section" |
|
8
|
import clsx from "clsx" |
|
9
|
import { useIsBrowser } from "../../../providers" |
|
10
|
|
|
11
|
export const ContentMenuWhatsNew = () => { |
|
12
|
const { |
|
13
|
config: { version }, |
|
14
|
} = useSiteConfig() |
|
15
|
const [announcements, setAnnouncements] = React.useState<CloudAnnouncement[]>( |
|
16
|
[] |
|
17
|
) |
|
18
|
// merge announcements with the version announcement so that: |
|
19
|
// - we only show three announcements in total |
|
20
|
// - if there's no version announcement, we show the announcements as-is |
|
21
|
// - we sort the announcements based on their published date, showing the most recent ones first |
|
22
|
const mergedAnnouncements = useMemo(() => { |
|
23
|
if (!version) { |
|
24
|
// announcements are already sorted by published date and sliced to 3 |
|
25
|
return announcements |
|
26
|
} |
|
27
|
|
|
28
|
const versionAnnouncement: CloudAnnouncement = { |
|
29
|
id: `version-${version.number}`, |
|
30
|
type: "version", |
|
31
|
title: `v${version.number} is live`, |
|
32
|
subtitle: `See new features and improvements`, |
|
33
|
published_at: version.releaseDate, |
|
34
|
created_at: version.releaseDate, |
|
35
|
link_url: version.releaseUrl, |
|
36
|
} |
|
37
|
|
|
38
|
if (!announcements.length) { |
|
39
|
return [versionAnnouncement] |
|
40
|
} |
|
41
|
|
|
42
|
return [versionAnnouncement, ...announcements] |
|
43
|
.sort((a, b) => { |
|
44
|
const dateA = new Date(a.published_at || a.created_at).getTime() |
|
45
|
const dateB = new Date(b.published_at || b.created_at).getTime() |
|
46
|
return dateB - dateA |
|
47
|
}) |
|
48
|
.slice(0, 3) |
|
49
|
}, [announcements, version]) |
|
50
|
|
|
51
|
useEffect(() => { |
|
52
|
getAnnouncements() |
|
53
|
.then(setAnnouncements) |
|
54
|
.catch((e) => { |
|
55
|
// fail silently, we don't want to break the page if announcements can't be loaded |
|
56
|
console.error("Failed to load announcements", e) |
|
57
|
}) |
|
58
|
}, []) |
|
59
|
|
|
60
|
if (!mergedAnnouncements.length) { |
|
61
|
return null |
|
62
|
} |
|
63
|
|
|
64
|
return ( |
|
65
|
<ContentMenuSection title="What's new" hideChildrenDivider> |
|
66
|
<div className="px-docs_1 pt-docs_1 flex flex-col"> |
|
67
|
{mergedAnnouncements.map((announcement, index) => ( |
|
68
|
<Announcement key={index} announcement={announcement} /> |
|
69
|
))} |
|
70
|
</div> |
|
71
|
</ContentMenuSection> |
|
72
|
) |
|
73
|
} |
|
74
|
|
|
75
|
const Announcement = ({ |
|
76
|
announcement, |
|
77
|
}: { |
|
78
|
announcement: CloudAnnouncement |
|
79
|
}) => { |
|
80
|
const { isBrowser } = useIsBrowser() |
|
81
|
const [isNew, setIsNew] = useState(false) |
|
82
|
useEffect(() => { |
|
83
|
if (!isBrowser) { |
|
84
|
return |
|
85
|
} |
|
86
|
|
|
87
|
const hasSeenAnnouncement = localStorage.getItem( |
|
88
|
`seen-announcement-${announcement.id}` |
|
89
|
) |
|
90
|
|
|
91
|
setIsNew(!hasSeenAnnouncement) |
|
92
|
}, [isBrowser, announcement.id]) |
|
93
|
|
|
94
|
const enableHasSeenAnnouncement = () => { |
|
95
|
localStorage.setItem(`seen-announcement-${announcement.id}`, "true") |
|
96
|
setIsNew(false) |
|
97
|
} |
|
98
|
|
|
99
|
return ( |
|
100
|
<div |
|
101
|
className="flex gap-docs_0.5 items-start last:[&_card]:!mb-0" |
|
102
|
onMouseOver={enableHasSeenAnnouncement} |
|
103
|
> |
|
104
|
<AnnouncementTracker isNew={isNew} /> |
|
105
|
<div |
|
106
|
className={clsx( |
|
107
|
"flex flex-col px-docs_0.5 py-docs_0.25 rounded-docs_DEFAULT overflow-hidden relative", |
|
108
|
"bg-medusa-bg-component shadow-elevation-card-rest dark:shadow-elevation-card-rest-dark", |
|
109
|
"hover:shadow-elevation-card-hover dark:hover:shadow-elevation-card-hover-dark", |
|
110
|
"card mb-docs_0.75" |
|
111
|
)} |
|
112
|
> |
|
113
|
<span className="text-2x-small-plus text-medusa-fg-base truncate"> |
|
114
|
{announcement.title} |
|
115
|
</span> |
|
116
|
<span className="text-2x-small-plus text-medusa-fg-subtle truncate"> |
|
117
|
{announcement.subtitle} |
|
118
|
</span> |
|
119
|
<a |
|
120
|
href={announcement.link_url} |
|
121
|
target="_blank" |
|
122
|
rel="noopener noreferrer" |
|
123
|
className="absolute top-0 left-0 w-full h-full" |
|
124
|
title="Read more" |
|
125
|
aria-label="Read more" |
|
126
|
/> |
|
127
|
</div> |
|
128
|
</div> |
|
129
|
) |
|
130
|
} |
|
131
|
|
|
132
|
const AnnouncementTracker = ({ isNew }: { isNew: boolean }) => { |
|
133
|
return ( |
|
134
|
<div className="flex flex-col gap-docs_0.125 items-center h-full"> |
|
135
|
<div className={clsx("h-docs_0.75 w-docs_0.75 relative rounded-full")}> |
|
136
|
<div |
|
137
|
className={clsx( |
|
138
|
"absolute top-0 left-0 w-full h-full rounded-full opacity-20", |
|
139
|
isNew && "bg-medusa-tag-blue-icon" |
|
140
|
)} |
|
141
|
/> |
|
142
|
<div |
|
143
|
className={clsx( |
|
144
|
"w-[5px] h-[5px] rounded-full", |
|
145
|
"border-[0.5px] border-medusa-alphas-alpha-10", |
|
146
|
"absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2", |
|
147
|
isNew ? "bg-medusa-tag-blue-icon" : "bg-medusa-fg-disabled" |
|
148
|
)} |
|
149
|
/> |
|
150
|
</div> |
|
151
|
<div className="flex-1 h-full w-[1.5px] bg-medusa-alphas-alpha-10" /> |
|
152
|
</div> |
|
153
|
) |
|
154
|
} |