|
| 1 | +import React, { useState, useEffect } from "react"; |
| 2 | +import { getPagesUnderRoute } from "nextra/context"; |
| 3 | +import { ImageFrame } from "./ImageFrame"; |
| 4 | +import { VideoButtonWithModal } from "./VideoButtonWithModal"; |
| 5 | +import Link from "next/link"; |
| 6 | +import { MdxFile } from "nextra"; |
| 7 | + |
| 8 | +enum PostFilterOptions { |
| 9 | + All = `all`, |
| 10 | + Announcements = `announcements`, |
| 11 | + Updates = `updates`, |
| 12 | +} |
| 13 | + |
| 14 | +const renderImage = (page) => { |
| 15 | + return ( |
| 16 | + <ImageFrame |
| 17 | + src={page.frontMatter.thumbnail} |
| 18 | + alt={page.frontMatter.title} |
| 19 | + isAnnouncement={page.frontMatter?.isAnnouncement} |
| 20 | + /> |
| 21 | + ); |
| 22 | +}; |
| 23 | + |
| 24 | +const getVideoEmbedURL = (videoURL) => { |
| 25 | + try { |
| 26 | + const parsedURL = new URL(videoURL); |
| 27 | + const host = parsedURL.host; |
| 28 | + if (host === "www.youtube.com" || host === "youtube.com" || host === "youtu.be") { |
| 29 | + const videoId = parsedURL.searchParams.get("v") || parsedURL.pathname.split("/").pop(); |
| 30 | + return `https://www.youtube.com/embed/${videoId}`; |
| 31 | + } else if (host === "www.loom.com" || host === "loom.com") { |
| 32 | + const videoId = parsedURL.pathname.split("/").pop(); |
| 33 | + return `https://www.loom.com/embed/${videoId}?hideEmbedTopBar=true`; |
| 34 | + } |
| 35 | + } catch (e) { |
| 36 | + console.error("Invalid URL:", e); |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +const renderVideo = (videoURL) => { |
| 41 | + const embedURL = getVideoEmbedURL(videoURL); |
| 42 | + |
| 43 | + return ( |
| 44 | + <iframe |
| 45 | + src={embedURL} |
| 46 | + style={{ |
| 47 | + width: "100%", |
| 48 | + aspectRatio: 16 / 9, |
| 49 | + height: "auto", |
| 50 | + borderRadius: "16px", |
| 51 | + marginBottom: "16px", |
| 52 | + }} |
| 53 | + allow="clipboard-write; encrypted-media; picture-in-picture" |
| 54 | + allowFullScreen |
| 55 | + title="Video" |
| 56 | + ></iframe> |
| 57 | + ); |
| 58 | +}; |
| 59 | + |
| 60 | +const renderMedia: (page: MdxFile) => React.JSX.Element = (page) => { |
| 61 | + if (page.frontMatter?.thumbnail) { |
| 62 | + return renderImage(page); |
| 63 | + } else if (page.frontMatter?.video) { |
| 64 | + return renderVideo(page.frontMatter?.video); |
| 65 | + } |
| 66 | + return null; |
| 67 | +}; |
| 68 | + |
| 69 | +export default function ChangelogIndex({ more = "Learn More" }) { |
| 70 | + // naturally sorts pages from a-z rather than z-a |
| 71 | + const allPages = getPagesUnderRoute("/changelogs").reverse(); |
| 72 | + const itemsPerPage = 10; |
| 73 | + const [displayedPages, setDisplayedPages] = useState([]); |
| 74 | + const [pageIndex, setPageIndex] = useState(0); |
| 75 | + const [filter, setFilter] = useState(PostFilterOptions.All); |
| 76 | + |
| 77 | + const getAllFilteredPages = () => { |
| 78 | + return allPages.filter((page) => { |
| 79 | + switch (filter) { |
| 80 | + case PostFilterOptions.Updates: |
| 81 | + // @ts-ignore:next-line |
| 82 | + return !page.frontMatter?.isAnnouncement; |
| 83 | + case PostFilterOptions.Announcements: |
| 84 | + // @ts-ignore:next-line |
| 85 | + return page.frontMatter?.isAnnouncement === true; |
| 86 | + default: |
| 87 | + return true; |
| 88 | + } |
| 89 | + }); |
| 90 | + }; |
| 91 | + |
| 92 | + // Load initial or additional pages |
| 93 | + useEffect(() => { |
| 94 | + const morePages = getAllFilteredPages().slice(0, pageIndex + itemsPerPage); |
| 95 | + |
| 96 | + setDisplayedPages(morePages); |
| 97 | + }, [pageIndex, filter]); |
| 98 | + |
| 99 | + const loadMore = () => { |
| 100 | + setPageIndex((prev) => prev + itemsPerPage); |
| 101 | + }; |
| 102 | + |
| 103 | + const filterButton = (id: PostFilterOptions, label: string) => { |
| 104 | + let className = "changelogFilterButton"; |
| 105 | + if (filter === id) { |
| 106 | + className += " active"; |
| 107 | + } |
| 108 | + return ( |
| 109 | + <button |
| 110 | + className={className} |
| 111 | + onClick={() => { |
| 112 | + setFilter(id); |
| 113 | + setPageIndex(0); |
| 114 | + }} |
| 115 | + > |
| 116 | + {label} |
| 117 | + </button> |
| 118 | + ); |
| 119 | + }; |
| 120 | + |
| 121 | + const filterOptions = [ |
| 122 | + { id: PostFilterOptions.All, label: "All Posts" }, |
| 123 | + { id: PostFilterOptions.Announcements, label: "Announcements" }, |
| 124 | + { id: PostFilterOptions.Updates, label: "Updates" }, |
| 125 | + ]; |
| 126 | + |
| 127 | + return ( |
| 128 | + <div |
| 129 | + style={{ |
| 130 | + display: "flex", |
| 131 | + alignItems: "center", |
| 132 | + flexDirection: "column", |
| 133 | + }} |
| 134 | + className="changelogIndexContainer" |
| 135 | + > |
| 136 | + <div className="changelogIndexFilterBorder" /> |
| 137 | + <div className="changelogIndexFilterBar"> |
| 138 | + {filterOptions.map((filter) => filterButton(filter.id, filter.label))} |
| 139 | + </div> |
| 140 | + <div className="changelogIndexFilterBorder changelogIndexFilterBorderBottom" /> |
| 141 | + |
| 142 | + {displayedPages.map((page) => ( |
| 143 | + <div key={page.route} className="changelogIndexItem nx-mt-16"> |
| 144 | + <div className="changelogIndexItemDate"> |
| 145 | + {page.frontMatter?.date ? ( |
| 146 | + <p className="changelogDate"> |
| 147 | + {new Date(page.frontMatter.date).toLocaleDateString(undefined, { |
| 148 | + year: "numeric", |
| 149 | + month: "long", |
| 150 | + day: "numeric", |
| 151 | + })} |
| 152 | + </p> |
| 153 | + ) : null} |
| 154 | + </div> |
| 155 | + |
| 156 | + <div className="changelogIndexItemBody"> |
| 157 | + {(page.frontMatter?.thumbnail || page.frontMatter?.video) && |
| 158 | + renderMedia(page)} |
| 159 | + |
| 160 | + <h3 |
| 161 | + className={ |
| 162 | + page.frontMatter?.thumbnail && "changelogItemTitleWrapper" |
| 163 | + } |
| 164 | + > |
| 165 | + <Link |
| 166 | + href={page.route} |
| 167 | + style={{ color: "inherit", textDecoration: "none" }} |
| 168 | + className="changelogItemTitle block" |
| 169 | + > |
| 170 | + {page.meta?.title || page.frontMatter?.title || page.name} |
| 171 | + </Link> |
| 172 | + </h3> |
| 173 | + |
| 174 | + <p className="opacity-80 mt-6 leading-7"> |
| 175 | + {page.frontMatter?.description} |
| 176 | + </p> |
| 177 | + <div className="nx-isolate nx-inline-flex nx-items-center nx-space-x-5 nx-mt-8"> |
| 178 | + {page.frontMatter?.isAnnouncement && ( |
| 179 | + <a |
| 180 | + href="https://mixpanel.com/contact-us/demo-request/" |
| 181 | + className="nx-px-5 nx-py-3 nx-my-4 nx-drop-shadow-sm nx-bg-gradient-to-t nx-from-purple100 nx-to-purple50 nx-rounded-full nx-text-white nx-font-medium nx-text-medium" |
| 182 | + > |
| 183 | + Request a Demo |
| 184 | + </a> |
| 185 | + )} |
| 186 | + {page.frontMatter?.video && page.frontMatter?.thumbnail && ( |
| 187 | + <VideoButtonWithModal |
| 188 | + src={page.frontMatter.video} |
| 189 | + showThumbnail={false} |
| 190 | + /> |
| 191 | + )} |
| 192 | + <Link |
| 193 | + target="_blank" |
| 194 | + href={page.route} |
| 195 | + className="changelogReadMoreLink" |
| 196 | + > |
| 197 | + {more + " →"} |
| 198 | + </Link> |
| 199 | + </div> |
| 200 | + <div className="changelogDivider nx-mt-16"></div> |
| 201 | + </div> |
| 202 | + </div> |
| 203 | + ))} |
| 204 | + {pageIndex + itemsPerPage < getAllFilteredPages().length && ( |
| 205 | + <div className="changelogLoadMoreButtonContainer"> |
| 206 | + <button onClick={loadMore} className="changelogLoadMoreButton"> |
| 207 | + Load More |
| 208 | + </button> |
| 209 | + </div> |
| 210 | + )} |
| 211 | + </div> |
| 212 | + ); |
| 213 | +} |
0 commit comments