Skip to content

Commit 15e8c5c

Browse files
committed
Implement a dedicated "Recent Donations" view, update the @vscode/codicons package, and refine button styling for accessibility and linking
1 parent 2acd959 commit 15e8c5c

21 files changed

Lines changed: 562 additions & 33 deletions

File tree

‎packages/ui/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"dependencies": {
2323
"@storybook/react": "^7.6.20",
2424
"@storybook/react-vite": "^7.6.20",
25-
"@vscode/codicons": "^0.0.36",
25+
"@vscode/codicons": "^0.0.43",
2626
"classnames": "^2.5.1",
2727
"dayjs": "^1.11.10",
2828
"react": "^18.2.0",

‎packages/ui/src/components/editor/common/Button/Button.module.scss‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
padding: 4px 16px;
99
font-family: system-ui, sans-serif;
1010
border: none;
11+
text-decoration: none;
1112
border-radius: 4px;
1213
cursor: pointer;
1314
background-color: var(--vscode-button-background);
1415
color: var(--vscode-button-foreground);
1516

1617
&:hover {
1718
background-color: var(--vscode-button-hoverBackground);
19+
text-decoration: none;
1820
}
1921

20-
&:focus {
22+
&:focus-visible {
2123
outline-offset: 2px;
2224
outline: 1px solid var(--vscode-focusBorder);
2325
}

‎packages/ui/src/components/editor/common/Button/Button.tsx‎

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import styles from './Button.module.scss'
33
import cn from 'classnames'
44

55
type Props = {
6-
on_click: () => void
6+
on_click?: () => void
7+
url?: string
78
disabled?: boolean
89
children?: React.ReactNode
910
codicon?: string
@@ -14,29 +15,51 @@ type Props = {
1415
}
1516

1617
export const Button: FC<Props> = (props) => {
17-
const button_ref = useRef<HTMLButtonElement>(null)
18+
const button_ref = useRef<HTMLButtonElement | HTMLAnchorElement>(null)
1819

1920
useEffect(() => {
2021
if (props.is_focused) {
2122
button_ref.current?.focus()
2223
}
2324
}, [props.is_focused])
2425

26+
const className = cn(styles.button, {
27+
[styles['button--secondary']]: props.is_secondary,
28+
[styles['button--small']]: props.is_small
29+
})
30+
31+
const children = (
32+
<>
33+
{props.codicon && (
34+
<span className={cn('codicon', `codicon-${props.codicon}`)} />
35+
)}
36+
{props.children}
37+
</>
38+
)
39+
40+
if (props.url) {
41+
return (
42+
<a
43+
ref={button_ref as React.RefObject<HTMLAnchorElement>}
44+
className={className}
45+
href={props.url}
46+
onClick={props.on_click}
47+
title={props.title}
48+
>
49+
{children}
50+
</a>
51+
)
52+
}
53+
2554
return (
2655
<button
27-
ref={button_ref}
28-
className={cn(styles.button, {
29-
[styles['button--secondary']]: props.is_secondary,
30-
[styles['button--small']]: props.is_small
31-
})}
56+
ref={button_ref as React.RefObject<HTMLButtonElement>}
57+
className={className}
3258
onClick={props.on_click}
3359
disabled={props.disabled}
3460
title={props.title}
3561
>
36-
{props.codicon && (
37-
<span className={cn('codicon', `codicon-${props.codicon}`)} />
38-
)}
39-
{props.children}
62+
{children}
4063
</button>
4164
)
4265
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
.container {
2+
display: flex;
3+
flex-direction: column;
4+
gap: 8px;
5+
padding: 4px 12px 12px 12px;
6+
}
7+
8+
.donations {
9+
display: flex;
10+
flex-direction: column;
11+
gap: 8px;
12+
13+
&--revalidating {
14+
opacity: var(--dimmed-opacity);
15+
}
16+
17+
&__donation {
18+
display: flex;
19+
flex-direction: column;
20+
gap: 4px;
21+
user-select: text;
22+
23+
&__header {
24+
overflow: hidden;
25+
text-overflow: ellipsis;
26+
white-space: nowrap;
27+
position: relative;
28+
padding-left: 6px;
29+
border-left: 2px solid #ffdd00;
30+
}
31+
32+
&__note {
33+
background-color: var(--vscode-list-hoverBackground);
34+
padding: 5px 8px;
35+
border-radius: 8px;
36+
}
37+
}
38+
}
39+
40+
.skeleton {
41+
background-color: var(--vscode-list-hoverBackground);
42+
border-radius: 4px;
43+
animation: pulse 1.5s cubic-bezier(0.4, 0, 0.6, 1) infinite;
44+
}
45+
46+
@keyframes pulse {
47+
50% {
48+
opacity: 0.5;
49+
}
50+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Donations, DonationsProps } from './Donations'
2+
3+
export default {
4+
component: Donations
5+
}
6+
7+
const sample_donations: DonationsProps['donations'] = [
8+
{
9+
support_message:
10+
'<span class="suppUsername"><a href="https://github.com/johndoe">JohnDoe</a></span> bought 2 coffees.',
11+
support_note: 'Great work! Keep it up.'
12+
},
13+
{
14+
support_message: '<strong>Jane Smith</strong> bought 5 coffees.',
15+
support_note: ''
16+
},
17+
{
18+
support_message: '<strong>John Doe</strong> bought 1 coffee.',
19+
support_note: 'Happy to help!'
20+
}
21+
]
22+
23+
export const Default = () => {
24+
return (
25+
<Donations
26+
donations={sample_donations}
27+
is_fetching={false}
28+
is_revalidating={false}
29+
on_fetch_next_page={() => console.log('Fetching next page…')}
30+
has_more={false}
31+
/>
32+
)
33+
}
34+
35+
export const Loading = () => {
36+
return (
37+
<Donations
38+
is_fetching={true}
39+
is_revalidating={false}
40+
on_fetch_next_page={() => console.log('Fetching next page…')}
41+
has_more={false}
42+
/>
43+
)
44+
}
45+
46+
export const Revalidating = () => {
47+
return (
48+
<Donations
49+
donations={sample_donations}
50+
is_fetching={false}
51+
is_revalidating={true}
52+
on_fetch_next_page={() => console.log('Fetching next page…')}
53+
has_more={true}
54+
/>
55+
)
56+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import styles from './Donations.module.scss'
2+
import { useEffect, useRef } from 'react'
3+
import cn from 'classnames'
4+
import { Scrollable } from '../Scrollable'
5+
6+
const get_href_from_url_like_string = (text: string): string | null => {
7+
if (/\s/.test(text)) {
8+
return null
9+
}
10+
if (text.startsWith('http://') || text.startsWith('https://')) {
11+
try {
12+
return new URL(text).href
13+
} catch {
14+
return null
15+
}
16+
}
17+
if (text.includes('.') && !text.includes('@')) {
18+
try {
19+
return new URL(`https://${text}`).href
20+
} catch {
21+
return null
22+
}
23+
}
24+
return null
25+
}
26+
27+
const parse_support_message = (
28+
message: string
29+
): {
30+
username: string
31+
href: string | null
32+
after_text: string
33+
} => {
34+
const span_match = message.match(
35+
/<span class="suppUsername"[^>]*>([\s\S]*?)<\/span>/i
36+
)
37+
38+
if (span_match) {
39+
const span_content = span_match[1].trim()
40+
const after_text = (message.split('</span>')[1] ?? '').trim()
41+
const a_match = span_content.match(
42+
/<a[^>]*href="([^"]+)"[^>]*>([^<]+)<\/a>/i
43+
)
44+
45+
if (a_match) {
46+
const href = a_match[1]
47+
const username = a_match[2]
48+
return { username, href, after_text }
49+
} else {
50+
const username = span_content
51+
const href = get_href_from_url_like_string(username)
52+
return { username, href, after_text }
53+
}
54+
}
55+
56+
const bold_match = message.match(/<strong[^>]*>([^<]+)<\/strong>/i)
57+
if (bold_match) {
58+
const username = bold_match[1].trim()
59+
const after_text = (message.split('</strong>')[1] ?? '').trim()
60+
const href = get_href_from_url_like_string(username)
61+
return { username, href, after_text }
62+
}
63+
64+
// Fallback for when no tags are found
65+
const username = message.trim()
66+
const href = get_href_from_url_like_string(username)
67+
return { username: href ? username : message, href, after_text: '' }
68+
}
69+
70+
type Donation = {
71+
support_message: string
72+
support_note: string
73+
}
74+
75+
export type DonationsProps = {
76+
donations?: Donation[]
77+
is_fetching?: boolean
78+
is_revalidating?: boolean
79+
on_fetch_next_page: () => void
80+
has_more?: boolean
81+
}
82+
83+
export const Donations: React.FC<DonationsProps> = (props) => {
84+
const observer_target = useRef<HTMLDivElement>(null)
85+
86+
useEffect(() => {
87+
if (!observer_target.current) return
88+
89+
const observer = new IntersectionObserver(
90+
(entries) => {
91+
if (entries[0].isIntersecting) {
92+
props.on_fetch_next_page()
93+
}
94+
},
95+
{ threshold: 1.0 }
96+
)
97+
98+
const target = observer_target.current
99+
observer.observe(target)
100+
101+
return () => observer.unobserve(target)
102+
}, [props.on_fetch_next_page])
103+
104+
return (
105+
<Scrollable>
106+
<div className={styles.container}>
107+
<div>
108+
Hi there! If you enjoy using Code Web Chat, buying a $3 coffee is a
109+
great way to show your support for the project.
110+
</div>
111+
<div>
112+
{props.is_fetching ? (
113+
<>
114+
<div className={styles.donations__donation}>
115+
<div
116+
className={styles.skeleton}
117+
style={{ width: '50%', height: '14px' }}
118+
/>
119+
<div className={styles.skeleton} style={{ height: '30px' }} />
120+
</div>
121+
<div className={styles.donations__donation}>
122+
<div
123+
className={styles.skeleton}
124+
style={{ width: '70%', height: '14px' }}
125+
/>
126+
<div className={styles.skeleton} style={{ height: '30px' }} />
127+
</div>
128+
<div className={styles.donations__donation}>
129+
<div
130+
className={styles.skeleton}
131+
style={{ width: '40%', height: '14px' }}
132+
/>
133+
<div className={styles.skeleton} style={{ height: '30px' }} />
134+
</div>
135+
</>
136+
) : (
137+
<>
138+
<div
139+
className={cn(styles.donations, {
140+
[styles['donations--revalidating']]: props.is_revalidating
141+
})}
142+
>
143+
{props.donations?.map((donation, index) => (
144+
<div className={styles.donations__donation} key={index}>
145+
{(() => {
146+
const { username, href, after_text } =
147+
parse_support_message(donation.support_message)
148+
const title = `${username} ${after_text}`
149+
return (
150+
<div
151+
className={styles.donations__donation__header}
152+
title={title}
153+
>
154+
<strong>
155+
{href ? (
156+
<a href={href} target="_blank" rel="nofollow">
157+
{username}
158+
</a>
159+
) : (
160+
username
161+
)}
162+
</strong>{' '}
163+
{after_text}
164+
</div>
165+
)
166+
})()}
167+
{donation.support_note && (
168+
<div className={styles.donations__donation__note}>
169+
{donation.support_note}
170+
</div>
171+
)}
172+
</div>
173+
))}
174+
</div>
175+
{props.has_more && <div ref={observer_target} />}
176+
</>
177+
)}
178+
</div>
179+
</div>
180+
</Scrollable>
181+
)
182+
}

0 commit comments

Comments
 (0)