Skip to content

Commit 9eb7041

Browse files
committed
fix: add app webUrl for self link parser
Signed-off-by: Innei <[email protected]>
1 parent 4d1dcda commit 9eb7041

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/atoms/url.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { queryClient } from '~/providers/root/react-query-provider'
12
import { atom, useAtomValue } from 'jotai'
23

34
import { getToken } from '~/lib/cookie'
@@ -12,15 +13,23 @@ export interface UrlConfig {
1213
}
1314

1415
const adminUrlAtom = atom<string | null>(null)
16+
const webUrlAtom = atom<string | null>(null)
1517

1618
export const fetchAppUrl = async () => {
17-
const { data } = await apiClient.proxy.options.url.get<{
18-
data: UrlConfig
19-
}>()
20-
21-
jotaiStore.set(adminUrlAtom, data.adminUrl)
19+
const { data } = await queryClient.fetchQuery({
20+
queryKey: ['app.url'],
21+
queryFn: () =>
22+
apiClient.proxy.options.url.get<{
23+
data: UrlConfig
24+
}>(),
25+
})
26+
27+
if (data.adminUrl) jotaiStore.set(adminUrlAtom, data.adminUrl)
28+
jotaiStore.set(webUrlAtom, data.webUrl)
2229
}
2330

31+
export const getWebUrl = () => jotaiStore.get(webUrlAtom)
32+
export const setWebUrl = (url: string) => jotaiStore.set(webUrlAtom, url)
2433
export const getAdminUrl = () => jotaiStore.get(adminUrlAtom)
2534
export const useAppUrl = () => {
2635
const url = useAggregationSelector((a) => a.url)

src/lib/link-parser.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getWebUrl } from '~/atoms'
2+
13
import { isClientSide, isDev } from './env'
24

35
export const getTweetId = (url: URL) => {
@@ -74,9 +76,13 @@ export const isBilibiliUrl = (url: URL) => {
7476

7577
export const isSelfArticleUrl = (url: URL) => {
7678
if (!isClientSide) return false
79+
80+
const webUrl = getWebUrl()
81+
const webHost = webUrl ? new URL(webUrl).hostname : ''
82+
7783
if (isDev && url.hostname === 'innei.in') return true
7884
return (
79-
url.hostname === location.hostname &&
85+
(url.hostname === location.hostname || webHost === location.hostname) &&
8086
['/posts/', '/notes/'].some((path) => url.pathname.startsWith(path))
8187
)
8288
}

src/providers/root/aggregation-data-provider.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { AggregateRoot } from '@mx-space/api-client'
77
import type { AppConfig } from '~/app/config'
88
import type { FC, PropsWithChildren } from 'react'
99

10-
import { fetchAppUrl } from '~/atoms'
10+
import { fetchAppUrl, setWebUrl } from '~/atoms'
1111
import { login } from '~/atoms/owner'
1212
import { useBeforeMounted } from '~/hooks/common/use-before-mounted'
1313
import { jotaiStore } from '~/lib/store'
@@ -24,6 +24,7 @@ export const AggregationProvider: FC<
2424
useBeforeMounted(() => {
2525
if (!aggregationData) return
2626
jotaiStore.set(aggregationDataAtom, aggregationData)
27+
setWebUrl(aggregationData.url.webUrl)
2728
})
2829
useBeforeMounted(() => {
2930
if (!appConfig) return
@@ -38,9 +39,11 @@ export const AggregationProvider: FC<
3839
useEffect(() => {
3940
if (!aggregationData) return
4041
jotaiStore.set(aggregationDataAtom, aggregationData)
42+
setWebUrl(aggregationData.url.webUrl)
4143
}, [aggregationData])
4244

4345
const callOnceRef = useRef(false)
46+
4447
useEffect(() => {
4548
if (callOnceRef.current) return
4649
if (!aggregationData?.user) return

0 commit comments

Comments
 (0)