|
1 | 1 | import { openUrl } from "@tauri-apps/plugin-opener"; |
| 2 | +import { ExternalLinkIcon, SparklesIcon } from "lucide-react"; |
2 | 3 | import { |
3 | | - ExternalLinkIcon, |
4 | | - GitCompareArrowsIcon, |
5 | | - SparklesIcon, |
6 | | -} from "lucide-react"; |
| 4 | + type RefObject, |
| 5 | + useCallback, |
| 6 | + useEffect, |
| 7 | + useRef, |
| 8 | + useState, |
| 9 | +} from "react"; |
| 10 | +import { useResizeObserver } from "usehooks-ts"; |
| 11 | + |
| 12 | +import NoteEditor from "@hypr/tiptap/editor"; |
| 13 | +import { md2json } from "@hypr/tiptap/shared"; |
| 14 | +import { |
| 15 | + Breadcrumb, |
| 16 | + BreadcrumbItem, |
| 17 | + BreadcrumbList, |
| 18 | + BreadcrumbPage, |
| 19 | + BreadcrumbSeparator, |
| 20 | +} from "@hypr/ui/components/ui/breadcrumb"; |
| 21 | +import { Button } from "@hypr/ui/components/ui/button"; |
| 22 | +import { cn } from "@hypr/utils"; |
7 | 23 |
|
8 | 24 | import { type Tab } from "../../../../store/zustand/tabs"; |
9 | 25 | import { StandardTabWrapper } from "../index"; |
10 | 26 | import { type TabItem, TabItemBase } from "../shared"; |
11 | 27 |
|
| 28 | +export const changelogFiles = import.meta.glob( |
| 29 | + "../../../../../../web/content/changelog/*.mdx", |
| 30 | + { query: "?raw", import: "default" }, |
| 31 | +); |
| 32 | + |
| 33 | +export function getLatestVersion(): string | null { |
| 34 | + const versions = Object.keys(changelogFiles) |
| 35 | + .map((k) => { |
| 36 | + const match = k.match(/\/([^/]+)\.mdx$/); |
| 37 | + return match ? match[1] : null; |
| 38 | + }) |
| 39 | + .filter((v): v is string => v !== null) |
| 40 | + .filter((v) => !v.includes("nightly")) |
| 41 | + .sort((a, b) => b.localeCompare(a, undefined, { numeric: true })); |
| 42 | + |
| 43 | + return versions[0] || null; |
| 44 | +} |
| 45 | + |
| 46 | +function stripFrontmatter(content: string): string { |
| 47 | + return content.trim(); |
| 48 | +} |
| 49 | + |
| 50 | +function stripImageLine(content: string): string { |
| 51 | + return content.replace(/^!\[.*?\]\(.*?\)\s*\n*/m, ""); |
| 52 | +} |
| 53 | + |
| 54 | +function addEmptyParagraphsBeforeHeaders( |
| 55 | + json: ReturnType<typeof md2json>, |
| 56 | +): ReturnType<typeof md2json> { |
| 57 | + if (!json.content) return json; |
| 58 | + |
| 59 | + const newContent: typeof json.content = []; |
| 60 | + for (let i = 0; i < json.content.length; i++) { |
| 61 | + const node = json.content[i]; |
| 62 | + if (node.type === "heading" && i > 0) { |
| 63 | + newContent.push({ type: "paragraph" }); |
| 64 | + } |
| 65 | + newContent.push(node); |
| 66 | + } |
| 67 | + |
| 68 | + return { ...json, content: newContent }; |
| 69 | +} |
| 70 | + |
12 | 71 | export const TabItemChangelog: TabItem<Extract<Tab, { type: "changelog" }>> = ({ |
13 | 72 | tab, |
14 | 73 | tabIndex, |
@@ -39,43 +98,149 @@ export function TabContentChangelog({ |
39 | 98 | }: { |
40 | 99 | tab: Extract<Tab, { type: "changelog" }>; |
41 | 100 | }) { |
42 | | - const { previous, current } = tab.state; |
| 101 | + const { current } = tab.state; |
| 102 | + |
| 103 | + const { content, loading } = useChangelogContent(current); |
| 104 | + const { scrollRef, atStart, atEnd } = useScrollFade<HTMLDivElement>(); |
43 | 105 |
|
44 | 106 | return ( |
45 | 107 | <StandardTabWrapper> |
46 | | - <div className="flex flex-1 flex-col items-center justify-center gap-6"> |
47 | | - <div className="text-center"> |
48 | | - <h1 className="text-2xl font-semibold text-neutral-900"> |
49 | | - Updated to v{current} |
| 108 | + <div className="flex flex-col h-full"> |
| 109 | + <div className="pl-2 pr-1 shrink-0"> |
| 110 | + <ChangelogHeader version={current} /> |
| 111 | + </div> |
| 112 | + |
| 113 | + <div className="mt-2 px-3 shrink-0"> |
| 114 | + <h1 className="text-xl font-semibold text-neutral-900"> |
| 115 | + What's new in {current}? |
50 | 116 | </h1> |
51 | | - {previous && ( |
52 | | - <p className="mt-1 text-sm text-neutral-500">from v{previous}</p> |
53 | | - )} |
54 | 117 | </div> |
55 | 118 |
|
56 | | - <div className="flex flex-col gap-2"> |
57 | | - <button |
58 | | - onClick={() => openUrl(`https://hyprnote.com/changelog/${current}`)} |
59 | | - className="flex items-center gap-3 px-4 py-2.5 text-sm text-neutral-600 hover:text-neutral-900 hover:bg-neutral-100 rounded-lg transition-colors" |
60 | | - > |
61 | | - <ExternalLinkIcon className="w-4 h-4" /> |
62 | | - View Changelog |
63 | | - </button> |
64 | | - {previous && ( |
65 | | - <button |
66 | | - onClick={() => |
67 | | - openUrl( |
68 | | - `https://github.com/fastrepl/hyprnote/compare/desktop_${previous}...desktop_v${current}`, |
69 | | - ) |
70 | | - } |
71 | | - className="flex items-center gap-3 px-4 py-2.5 text-sm text-neutral-600 hover:text-neutral-900 hover:bg-neutral-100 rounded-lg transition-colors" |
72 | | - > |
73 | | - <GitCompareArrowsIcon className="w-4 h-4" /> |
74 | | - View GitHub Diff |
75 | | - </button> |
76 | | - )} |
| 119 | + <div className="mt-4 flex-1 min-h-0 relative overflow-hidden"> |
| 120 | + {!atStart && <ScrollFadeOverlay position="top" />} |
| 121 | + {!atEnd && <ScrollFadeOverlay position="bottom" />} |
| 122 | + <div ref={scrollRef} className="h-full overflow-y-auto px-3"> |
| 123 | + {loading ? ( |
| 124 | + <p className="text-neutral-500">Loading...</p> |
| 125 | + ) : content ? ( |
| 126 | + <NoteEditor initialContent={content} editable={false} /> |
| 127 | + ) : ( |
| 128 | + <p className="text-neutral-500"> |
| 129 | + No changelog available for this version. |
| 130 | + </p> |
| 131 | + )} |
| 132 | + </div> |
77 | 133 | </div> |
78 | 134 | </div> |
79 | 135 | </StandardTabWrapper> |
80 | 136 | ); |
81 | 137 | } |
| 138 | + |
| 139 | +function ChangelogHeader({ version }: { version: string }) { |
| 140 | + return ( |
| 141 | + <div className="w-full pt-1"> |
| 142 | + <div className="flex items-center gap-2"> |
| 143 | + <div className="min-w-0 flex-1"> |
| 144 | + <Breadcrumb className="ml-1.5 min-w-0"> |
| 145 | + <BreadcrumbList className="text-neutral-700 text-xs flex-nowrap overflow-hidden gap-0.5"> |
| 146 | + <BreadcrumbItem className="shrink-0"> |
| 147 | + <span className="text-neutral-500">Changelog</span> |
| 148 | + </BreadcrumbItem> |
| 149 | + <BreadcrumbSeparator className="shrink-0" /> |
| 150 | + <BreadcrumbItem className="overflow-hidden"> |
| 151 | + <BreadcrumbPage className="truncate">{version}</BreadcrumbPage> |
| 152 | + </BreadcrumbItem> |
| 153 | + </BreadcrumbList> |
| 154 | + </Breadcrumb> |
| 155 | + </div> |
| 156 | + |
| 157 | + <div className="flex items-center shrink-0"> |
| 158 | + <Button |
| 159 | + size="sm" |
| 160 | + variant="ghost" |
| 161 | + className="gap-1.5" |
| 162 | + onClick={() => openUrl("https://hyprnote.com/changelog")} |
| 163 | + > |
| 164 | + <ExternalLinkIcon size={14} className="-mt-0.5" /> |
| 165 | + <span>See all</span> |
| 166 | + </Button> |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + </div> |
| 170 | + ); |
| 171 | +} |
| 172 | + |
| 173 | +function useChangelogContent(version: string) { |
| 174 | + const [content, setContent] = useState<ReturnType<typeof md2json> | null>( |
| 175 | + null, |
| 176 | + ); |
| 177 | + const [loading, setLoading] = useState(true); |
| 178 | + |
| 179 | + useEffect(() => { |
| 180 | + const key = Object.keys(changelogFiles).find((k) => |
| 181 | + k.endsWith(`/${version}.mdx`), |
| 182 | + ); |
| 183 | + |
| 184 | + if (!key) { |
| 185 | + setLoading(false); |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + changelogFiles[key]() |
| 190 | + .then((raw) => { |
| 191 | + const markdown = stripImageLine(stripFrontmatter(raw as string)); |
| 192 | + const json = md2json(markdown); |
| 193 | + setContent(addEmptyParagraphsBeforeHeaders(json)); |
| 194 | + setLoading(false); |
| 195 | + }) |
| 196 | + .catch(() => { |
| 197 | + setContent(null); |
| 198 | + setLoading(false); |
| 199 | + }); |
| 200 | + }, [version]); |
| 201 | + |
| 202 | + return { content, loading }; |
| 203 | +} |
| 204 | + |
| 205 | +function useScrollFade<T extends HTMLElement>() { |
| 206 | + const scrollRef = useRef<T>(null); |
| 207 | + const [state, setState] = useState({ atStart: true, atEnd: true }); |
| 208 | + |
| 209 | + const update = useCallback(() => { |
| 210 | + const el = scrollRef.current; |
| 211 | + if (!el) return; |
| 212 | + |
| 213 | + const { scrollTop, scrollHeight, clientHeight } = el; |
| 214 | + setState({ |
| 215 | + atStart: scrollTop <= 1, |
| 216 | + atEnd: scrollTop + clientHeight >= scrollHeight - 1, |
| 217 | + }); |
| 218 | + }, []); |
| 219 | + |
| 220 | + useResizeObserver({ ref: scrollRef as RefObject<T>, onResize: update }); |
| 221 | + |
| 222 | + useEffect(() => { |
| 223 | + const el = scrollRef.current; |
| 224 | + if (!el) return; |
| 225 | + |
| 226 | + update(); |
| 227 | + el.addEventListener("scroll", update); |
| 228 | + return () => el.removeEventListener("scroll", update); |
| 229 | + }, [update]); |
| 230 | + |
| 231 | + return { scrollRef, ...state }; |
| 232 | +} |
| 233 | + |
| 234 | +function ScrollFadeOverlay({ position }: { position: "top" | "bottom" }) { |
| 235 | + return ( |
| 236 | + <div |
| 237 | + className={cn([ |
| 238 | + "absolute left-0 w-full h-8 z-20 pointer-events-none", |
| 239 | + position === "top" && |
| 240 | + "top-0 bg-gradient-to-b from-white to-transparent", |
| 241 | + position === "bottom" && |
| 242 | + "bottom-0 bg-gradient-to-t from-white to-transparent", |
| 243 | + ])} |
| 244 | + /> |
| 245 | + ); |
| 246 | +} |
0 commit comments