1+ import { PropsWithChildren } from "react" ;
2+ import { StackOverflowSvg } from "../StackOverflowSvg/StackOverflowSvg" ;
3+ import { StackOverflowLinkDataResponse } from "../config/GithubPermalinkContext" ;
4+ import { ErrorMessages } from "../ErrorMessages/ErrorMessages" ;
5+ import { Inline } from "../common/Inline/Inline" ;
6+
7+
8+ export type StackOverflowLinkBaseProps = {
9+ className ?: string ;
10+ questionLink : string ;
11+ data : StackOverflowLinkDataResponse ;
12+ variant ?: "inline" | "block"
13+ }
14+
15+
16+
17+ export function StackOverflowLinkBase ( props : StackOverflowLinkBaseProps ) {
18+ const { data, variant = "block" , questionLink} = props ;
19+
20+ if ( variant === "inline" ) {
21+ if ( data . status === "ok" ) {
22+ return < Inline href = { questionLink } text = { `stackoverflow.com/q/${ data . questionId } : ${ data . questionTitle } ` } />
23+ }
24+ else {
25+ return < Inline href = { questionLink } text = { questionLink } />
26+ }
27+ }
28+
29+ if ( data . status === "ok" ) {
30+ return < StackOverflowLinkInner { ...props } header = { < >
31+ < div className = "react-stackoverflow-link-header" >
32+ < StackOverflowSvg />
33+ < p >
34+ Stack Overflow
35+ </ p >
36+ </ div >
37+
38+ < div className = "react-stackoverflow-link-body" >
39+ < p > < span className = "react-stackoverflow-link-title" > { data . questionTitle } </ span > </ p >
40+
41+ < div className = "react-stackoverflow-link-stats" >
42+ < div className = "react-stackoverflow-link-score" >
43+ < span className = "stat-label" > Score:</ span > < span className = "stat-value" > { data . score } </ span >
44+ </ div >
45+ < div className = "react-stackoverflow-link-answers" >
46+ < span className = "stat-label" > Answers:</ span > < span className = "stat-value" > { data . answerCount } </ span >
47+ { data . isAnswered && < span className = "answered-indicator" > ✓</ span > }
48+ </ div >
49+ < div className = "react-stackoverflow-link-views" >
50+ < span className = "stat-label" > Views:</ span > < span className = "stat-value" > { data . viewCount . toLocaleString ( ) } </ span >
51+ </ div >
52+ </ div >
53+
54+ { data . tags && data . tags . length > 0 && (
55+ < div className = "react-stackoverflow-link-tags" >
56+ { data . tags . map ( tag => (
57+ < span key = { tag } className = "tag" > { tag } </ span >
58+ ) ) }
59+ </ div >
60+ ) }
61+ </ div >
62+ </ > } >
63+ </ StackOverflowLinkInner >
64+
65+ }
66+
67+ return < StackOverflowLinkInner { ...props } >
68+ < ErrorMessages data = { data } />
69+ </ StackOverflowLinkInner >
70+
71+ }
72+
73+
74+ function StackOverflowLinkInner ( props : PropsWithChildren < {
75+ header ?: React . ReactNode
76+ } & {
77+ questionLink : string ;
78+ className ?: string ;
79+ } > ) {
80+
81+ const { questionLink, className = '' } = props ;
82+
83+ return < div className = { `rgp-base react-stackoverflow-link ${ className } ` } >
84+ < a href = { questionLink } >
85+ < div className = "header" >
86+ { props . header ?? < a href = { questionLink } className = "file-link" > { questionLink } </ a > }
87+ </ div >
88+ { props . children }
89+ </ a >
90+ </ div >
91+ }
0 commit comments