|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as React from 'react'; |
| 7 | +import { getStatus } from './header'; |
| 8 | +import { copyIcon } from './icon'; |
| 9 | +import { PullRequest } from '../../src/github/views'; |
| 10 | +import PullRequestContext from '../common/context'; |
| 11 | + |
| 12 | +export function useStickyHeader(titleRef: React.RefObject<HTMLDivElement | null>): boolean { |
| 13 | + const [isStuck, setIsStuck] = React.useState(false); |
| 14 | + |
| 15 | + React.useEffect(() => { |
| 16 | + const el = titleRef.current; |
| 17 | + if (!el) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + const observer = new IntersectionObserver( |
| 22 | + ([entry]) => setIsStuck(!entry.isIntersecting), |
| 23 | + { threshold: 0 }, |
| 24 | + ); |
| 25 | + observer.observe(el); |
| 26 | + |
| 27 | + return () => observer.disconnect(); |
| 28 | + }, [titleRef]); |
| 29 | + |
| 30 | + return isStuck; |
| 31 | +} |
| 32 | + |
| 33 | +export function StickyHeader({ pr, visible }: { pr: PullRequest; visible: boolean }): JSX.Element { |
| 34 | + const { text, color, icon } = getStatus(pr.state, !!pr.isDraft, pr.isIssue, pr.stateReason); |
| 35 | + const { copyPrLink } = React.useContext(PullRequestContext); |
| 36 | + |
| 37 | + const stickyRef = React.useCallback((node: HTMLDivElement | null) => { |
| 38 | + if (node) { |
| 39 | + if (visible) { |
| 40 | + node.removeAttribute('inert'); |
| 41 | + } else { |
| 42 | + node.setAttribute('inert', ''); |
| 43 | + } |
| 44 | + } |
| 45 | + }, [visible]); |
| 46 | + |
| 47 | + return ( |
| 48 | + <div ref={stickyRef} className={`sticky-header${visible ? ' visible' : ''}`}> |
| 49 | + <div className="sticky-header-left"> |
| 50 | + <div id="sticky-status" className={`status-badge-${color}`}> |
| 51 | + <span className="icon">{icon}</span> |
| 52 | + <span>{text}</span> |
| 53 | + </div> |
| 54 | + <span className="sticky-header-title" dangerouslySetInnerHTML={{ __html: pr.titleHTML }} /> |
| 55 | + <a |
| 56 | + className="sticky-header-number" |
| 57 | + href={pr.url} |
| 58 | + title={pr.url} |
| 59 | + data-vscode-context={JSON.stringify({ |
| 60 | + url: pr.url, |
| 61 | + preventDefaultContextMenuItems: true, |
| 62 | + owner: pr.owner, |
| 63 | + repo: pr.repo, |
| 64 | + number: pr.number, |
| 65 | + 'github:copyMenu': true, |
| 66 | + })} |
| 67 | + > |
| 68 | + #{pr.number} |
| 69 | + </a> |
| 70 | + <button title="Copy Link" onClick={copyPrLink} className="icon-button sticky-header-copy" aria-label="Copy Pull Request Link"> |
| 71 | + {copyIcon} |
| 72 | + </button> |
| 73 | + </div> |
| 74 | + </div> |
| 75 | + ); |
| 76 | +} |
0 commit comments