Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(issues): Add anchor links back to issue sections #79333

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions static/app/views/issueDetails/streamline/eventNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,14 @@ function EventNavigationLink({
config?.initialCollapse ?? false
);
return (
<Button
onClick={() => {
<LinkButton
to={{
...location,
hash: `#${config.key}`,
replace: true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is replace the right choice

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind it, but I don't think the other one replaced did it?

}}
onClick={event => {
event.preventDefault();
setIsCollapsed(false);
document
.getElementById(config.key)
Expand All @@ -354,7 +360,7 @@ function EventNavigationLink({
css={propCss}
>
{sectionLabels[config.key]}
</Button>
</LinkButton>
);
}

Expand Down
31 changes: 28 additions & 3 deletions static/app/views/issueDetails/streamline/foldSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Fragment,
useCallback,
useLayoutEffect,
useRef,
useState,
} from 'react';
import styled from '@emotion/styled';
Expand All @@ -13,6 +14,7 @@ import InteractionStateLayer from 'sentry/components/interactionStateLayer';
import {IconChevron} from 'sentry/icons';
import {space} from 'sentry/styles/space';
import {trackAnalytics} from 'sentry/utils/analytics';
import mergeRefs from 'sentry/utils/mergeRefs';
import useOrganization from 'sentry/utils/useOrganization';
import {useSyncedLocalStorageState} from 'sentry/utils/useSyncedLocalStorageState';
import type {SectionKey} from 'sentry/views/issueDetails/streamline/context';
Expand Down Expand Up @@ -53,7 +55,6 @@ export const FoldSection = forwardRef<HTMLElement, FoldSectionProps>(function Fo
sectionKey,
initialCollapse = false,
preventCollapse = false,
...props
},
forwardedRef
) {
Expand All @@ -64,6 +65,31 @@ export const FoldSection = forwardRef<HTMLElement, FoldSectionProps>(function Fo
getFoldSectionKey(sectionKey),
initialCollapse
);
const hasAttemptedScroll = useRef(false);

const scrollToSection = useCallback(
(element: HTMLElement | null) => {
if (!element || !navScrollMargin || hasAttemptedScroll.current) {
return;
}
// Prevent scrolling to element on rerenders
hasAttemptedScroll.current = true;

// scroll to element if it's the current section on page load
if (window.location.hash) {
const [, hash] = window.location.hash.split('#');
if (hash === sectionKey) {
if (isCollapsed) {
setIsCollapsed(false);
}

// Delay scrollIntoView to allow for layout changes to take place
setTimeout(() => element?.scrollIntoView(), 100);
}
}
},
[sectionKey, navScrollMargin, isCollapsed, setIsCollapsed]
);

useLayoutEffect(() => {
if (!sectionData.hasOwnProperty(sectionKey)) {
Expand Down Expand Up @@ -92,8 +118,7 @@ export const FoldSection = forwardRef<HTMLElement, FoldSectionProps>(function Fo
return (
<Fragment>
<Section
{...props}
ref={forwardedRef}
ref={mergeRefs([forwardedRef, scrollToSection])}
id={sectionKey}
scrollMargin={navScrollMargin ?? 0}
>
Expand Down
Loading