Skip to content

Commit 4aac9b8

Browse files
committed
feat: self favicon in MLink
Signed-off-by: Innei <[email protected]>
1 parent 44f7151 commit 4aac9b8

File tree

1 file changed

+66
-44
lines changed

1 file changed

+66
-44
lines changed

src/components/ui/link/MLink.tsx

+66-44
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,80 @@
11
'use client'
22

3-
import { memo, useCallback } from 'react'
3+
import { memo, useCallback, useMemo } from 'react'
44
import { useRouter } from 'next/navigation'
55
import type { FC, ReactNode } from 'react'
66

7+
import { isServerSide } from '~/lib/env'
8+
import { useAppConfigSelector } from '~/providers/root/aggregation-data-provider'
9+
710
import { FloatPopover } from '../float-popover'
811
import { Favicon } from '../rich-link/Favicon'
912

1013
export const MLink: FC<{
1114
href: string
1215
title?: string
1316
children?: ReactNode
14-
text?: string
15-
}> = memo(({ href, children, title, text }) => {
17+
}> = memo(({ href, children, title }) => {
1618
const router = useRouter()
19+
const isSelfUrl = useMemo(() => {
20+
if (isServerSide) return false
21+
const locateUrl = new URL(location.href)
22+
23+
const toUrlParser = new URL(href)
24+
return (
25+
toUrlParser.host === locateUrl.host ||
26+
(process.env.NODE_ENV === 'development' &&
27+
toUrlParser.host === 'innei.in')
28+
)
29+
}, [href])
30+
1731
const handleRedirect = useCallback(
1832
(e: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => {
19-
const locateUrl = new URL(location.href)
20-
2133
const toUrlParser = new URL(href)
34+
if (!isSelfUrl) {
35+
return
36+
}
37+
e.preventDefault()
38+
const pathArr = toUrlParser.pathname.split('/').filter(Boolean)
39+
const headPath = pathArr[0]
2240

23-
if (
24-
toUrlParser.host === locateUrl.host ||
25-
(process.env.NODE_ENV === 'development' &&
26-
toUrlParser.host === 'innei.in')
27-
) {
28-
e.preventDefault()
29-
const pathArr = toUrlParser.pathname.split('/').filter(Boolean)
30-
const headPath = pathArr[0]
31-
32-
switch (headPath) {
33-
case 'posts':
34-
case 'notes':
35-
case 'category': {
36-
router.push(toUrlParser.pathname)
37-
break
38-
}
39-
default: {
40-
window.open(toUrlParser.pathname)
41-
}
41+
switch (headPath) {
42+
case 'posts':
43+
case 'notes':
44+
case 'category': {
45+
router.push(toUrlParser.pathname)
46+
break
47+
}
48+
default: {
49+
window.open(toUrlParser.pathname)
4250
}
4351
}
4452
},
45-
[href, router],
53+
[href, isSelfUrl, router],
4654
)
4755

4856
return (
4957
<FloatPopover
5058
as="span"
5159
wrapperClassName="!inline"
5260
type="tooltip"
53-
TriggerComponent={useCallback(
54-
() => (
55-
<span className="inline items-center font-sans">
56-
<Favicon href={href} />
57-
<a
58-
className="shiro-link--underline"
59-
href={href}
60-
target="_blank"
61-
onClick={handleRedirect}
62-
title={title}
63-
rel="noreferrer"
64-
>
65-
{children}
66-
</a>
61+
triggerElement={
62+
<span className="inline-flex items-center font-sans">
63+
{isSelfUrl ? <BizSelfFavicon /> : <Favicon href={href} />}
64+
<a
65+
className="shiro-link--underline"
66+
href={href}
67+
target="_blank"
68+
onClick={handleRedirect}
69+
title={title}
70+
rel="noreferrer"
71+
>
72+
{children}
73+
</a>
6774

68-
<i className="icon-[mingcute--arrow-right-up-line] translate-y-[2px] opacity-70" />
69-
</span>
70-
),
71-
[handleRedirect, children, href, title],
72-
)}
75+
<i className="icon-[mingcute--arrow-right-up-line] translate-y-[2px] opacity-70" />
76+
</span>
77+
}
7378
>
7479
<a href={href} target="_blank" rel="noreferrer">
7580
<span>{href}</span>
@@ -78,3 +83,20 @@ export const MLink: FC<{
7883
)
7984
})
8085
MLink.displayName = 'MLink'
86+
87+
const BizSelfFavicon = () => {
88+
const { favicon, faviconDark } = useAppConfigSelector((a) => a.site) || {}
89+
if (!favicon && !faviconDark) return null
90+
return (
91+
<span className="mr-1 inline-flex size-4 center">
92+
<img
93+
className="inline size-4 dark:hidden"
94+
src={favicon ? favicon : faviconDark ? faviconDark : ''}
95+
/>
96+
<img
97+
className="hidden size-4 dark:inline"
98+
src={faviconDark ? faviconDark : favicon ? favicon : ''}
99+
/>
100+
</span>
101+
)
102+
}

0 commit comments

Comments
 (0)