-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDocLink.tsx
More file actions
76 lines (70 loc) · 2.2 KB
/
DocLink.tsx
File metadata and controls
76 lines (70 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { MouseEvent } from 'react'
import { DOCUMENTATION_HOME_PAGE } from '@Common/Constants'
import { Button, ButtonComponentType, ButtonVariantType, Icon } from '@Shared/Components'
import { ComponentSizeType } from '@Shared/constants'
import { useIsSecureConnection } from '@Shared/Hooks'
import { SidePanelTab, useMainContext } from '@Shared/Providers'
import { DocLinkProps } from './types'
import { getDocumentationUrl } from './utils'
export const DocLink = <T extends boolean = false>({
docLinkKey,
text = 'Learn more',
dataTestId,
startIcon,
showExternalIcon,
onClick,
fontWeight,
size = ComponentSizeType.medium,
variant = ButtonVariantType.text,
isExternalLink,
openInNewTab = false,
fullWidth = false,
}: DocLinkProps<T>) => {
// HOOKS
const { isEnterprise, setSidePanelConfig, isLicenseDashboard } = useMainContext()
const isSecureConnection = useIsSecureConnection()
// CONSTANTS
const documentationLink = getDocumentationUrl({
docLinkKey,
isEnterprise,
isExternalLink,
isLicenseDashboard,
})
// HANDLERS
const handleClick = (e: MouseEvent<HTMLAnchorElement>) => {
if (
isSecureConnection &&
!isExternalLink &&
!openInNewTab &&
!e.metaKey &&
!isLicenseDashboard &&
documentationLink.startsWith(DOCUMENTATION_HOME_PAGE)
) {
e.preventDefault()
setSidePanelConfig((prev) => ({
...prev,
state: SidePanelTab.DOCUMENTATION,
docLink: documentationLink,
reinitialize: true,
}))
}
onClick?.(e)
}
return (
<Button
component={ButtonComponentType.anchor}
anchorProps={{
href: documentationLink,
}}
onClick={handleClick}
dataTestId={dataTestId}
text={text}
variant={variant}
size={size}
startIcon={startIcon}
endIcon={showExternalIcon && <Icon name="ic-open-in-new" color={null} />}
fullWidth={fullWidth}
fontWeight={fontWeight}
/>
)
}