Skip to content

Commit f70612b

Browse files
committed
fix: enhance changelog functionality with version filtering and refactor state management
1 parent fdae6b6 commit f70612b

File tree

4 files changed

+75
-51
lines changed

4 files changed

+75
-51
lines changed
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
import { useQuery } from '@tanstack/react-query';
2-
import { ExtractFnReturnType, QueryConfig } from 'src/lib/react-query';
3-
import { Version } from "../types";
4-
import { axios } from 'src/lib/axios';
1+
import { useQuery } from '@tanstack/react-query'
2+
import { axios } from 'src/lib/axios'
3+
import { ExtractFnReturnType, QueryConfig } from 'src/lib/react-query'
4+
import { Version } from '../types'
55

66
const getAd = async (): Promise<Version[]> => {
7-
8-
return axios.get<Version[]>('https://api.github.com/repos/medyo/hackertab.dev/releases')
9-
.then(response => {
10-
const versions = response as unknown as Version[];
11-
return versions
12-
})
7+
return axios
8+
.get<Version[]>('https://api.github.com/repos/medyo/hackertab.dev/releases')
9+
.then((response) => {
10+
const versions = response as unknown as Version[]
11+
return versions
12+
})
1313
}
1414

15-
type QueryFnType = typeof getAd;
15+
type QueryFnType = typeof getAd
1616

1717
type UseGetAdOptions = {
18-
config?: QueryConfig<QueryFnType>;
19-
};
18+
config?: QueryConfig<QueryFnType>
19+
}
2020
export const useGetVersions = ({ config }: UseGetAdOptions = {}) => {
2121
return useQuery<ExtractFnReturnType<QueryFnType>>({
2222
...config,
2323
queryKey: ['versions'],
2424
queryFn: () => getAd(),
25-
});
26-
}
25+
})
26+
}

src/features/changelog/components/Changelog.tsx

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react'
1+
import { useEffect, useMemo, useState } from 'react'
22
import { HiSparkles } from 'react-icons/hi'
33
import ReactMarkdown from 'react-markdown'
44
import BeatLoader from 'react-spinners/BeatLoader'
@@ -8,6 +8,7 @@ import { getAppVersion } from 'src/utils/Os'
88
import { format } from 'timeago.js'
99
import { useGetVersions } from '../api/getVersions'
1010
import { useChangelogStore } from '../stores/changelog'
11+
import { isVersionLowerOrEqual } from '../utils/semver'
1112

1213
export const Changelog = () => {
1314
const tooltipId = 'tl-1'
@@ -19,10 +20,15 @@ export const Changelog = () => {
1920
} = useGetVersions({
2021
config: {
2122
enabled: tooltipShown,
23+
select: (data) => {
24+
const currentVersion = getAppVersion()
25+
if (!currentVersion) return data
26+
return data.filter((item) => isVersionLowerOrEqual(item.name, currentVersion))
27+
},
2228
},
2329
})
2430

25-
const { lastReadVersion, setVersionAsRead } = useChangelogStore()
31+
const { lastReadVersion, markVersionAsRead } = useChangelogStore()
2632

2733
const isChangelogRead = (): boolean => {
2834
return lastReadVersion === getAppVersion()
@@ -34,11 +40,31 @@ export const Changelog = () => {
3440
trackChangeLogOpen()
3541

3642
if (currentVersion) {
37-
setVersionAsRead(currentVersion)
43+
markVersionAsRead(currentVersion)
3844
}
3945
}
40-
}, [tooltipShown, setVersionAsRead])
46+
}, [tooltipShown, markVersionAsRead])
4147

48+
const versionsMemo = useMemo(() => {
49+
return (versions || []).map((item) => {
50+
return (
51+
<div key={item.name}>
52+
<div className="tooltipHeader">
53+
<a
54+
href="/#"
55+
className="tooltipVersion"
56+
onClick={() => window.open(item.html_url, '_blank')}>
57+
{item.name}
58+
</a>
59+
<span className="tooltipDate">{format(new Date(item.published_at))}</span>
60+
</div>
61+
<div className="tooltipContent">
62+
<ReactMarkdown children={item.body} />
63+
</div>
64+
</div>
65+
)
66+
})
67+
}, [versions])
4268
return (
4369
<>
4470
<ReactTooltip
@@ -58,24 +84,7 @@ export const Changelog = () => {
5884
) : isError || !versions.length ? (
5985
<p className="tooltipErrorMsg">Failed to load the changelog</p>
6086
) : (
61-
versions.map((item) => {
62-
return (
63-
<div key={item.name}>
64-
<div className="tooltipHeader">
65-
<a
66-
href="/#"
67-
className="tooltipVersion"
68-
onClick={() => window.open(item.html_url, '_blank')}>
69-
{item.name}
70-
</a>
71-
<span className="tooltipDate">{format(new Date(item.published_at))}</span>
72-
</div>
73-
<div className="tooltipContent">
74-
<ReactMarkdown children={item.body} />
75-
</div>
76-
</div>
77-
)
78-
})
87+
versionsMemo
7988
)}
8089
</ReactTooltip>
8190
<button
Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
import { create } from 'zustand';
1+
import { create } from 'zustand'
22

3-
import { persist } from 'zustand/middleware';
3+
import { persist } from 'zustand/middleware'
44

55
type ChangelogVersionStore = {
6-
lastReadVersion: string | undefined | null;
7-
setVersionAsRead: (versionName: string | undefined | null) => void;
8-
};
6+
lastReadVersion: string | undefined | null
7+
markVersionAsRead: (versionName: string | undefined | null) => void
8+
}
99

10-
export const useChangelogStore = create(persist<ChangelogVersionStore>((set) => ({
11-
lastReadVersion: undefined,
12-
setVersionAsRead: (versionName: string | undefined | null) =>
13-
set(() => ({
14-
lastReadVersion: versionName,
15-
}))
16-
}),{
17-
name: 'changelog_storage',
18-
}));
10+
export const useChangelogStore = create(
11+
persist<ChangelogVersionStore>(
12+
(set) => ({
13+
lastReadVersion: undefined,
14+
markVersionAsRead: (versionName: string | undefined | null) =>
15+
set(() => ({
16+
lastReadVersion: versionName,
17+
})),
18+
}),
19+
{
20+
name: 'changelog_storage',
21+
}
22+
)
23+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const isVersionLowerOrEqual = (v1: string, v2: string) => {
2+
const a = v1.replace(/^v/, '').split('.').map(Number)
3+
const b = v2.replace(/^v/, '').split('.').map(Number)
4+
for (let i = 0; i < Math.max(a.length, b.length); i++) {
5+
const diff = (a[i] || 0) - (b[i] || 0)
6+
if (diff < 0) return true // v1 < v2
7+
if (diff > 0) return false // v1 > v2
8+
}
9+
return true // equal
10+
}

0 commit comments

Comments
 (0)