Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const DrawerContent: React.FC<{
<>
<LearningResourceExpandedV2
imgConfig={imgConfigs.large}
resourceId={resourceId}
resource={resource.data}
carousels={[similarResourcesCarousel]}
user={user}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ const RunDates: React.FC<{ resource: LearningResource }> = ({ resource }) => {
const showMoreLink = (
<NoWrap>
<ShowHideLink
scroll={false}
color="red"
size="small"
onClick={() => setShowingMore(!showingMore)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const setup = (resource: LearningResource, isLearningPathEditor?: boolean) => {
return render(
<BrowserRouter>
<LearningResourceExpandedV2
resourceId={resource.id}
resource={resource}
user={user}
shareUrl={`https://learn.mit.edu/search?resource=${resource.id}`}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useRef, useState } from "react"
import React, { useCallback, useEffect, useRef, useState } from "react"
import styled from "@emotion/styled"
import Skeleton from "@mui/material/Skeleton"
import { default as NextImage } from "next/image"
Expand Down Expand Up @@ -292,6 +292,7 @@ const CarouselContainer = styled.div({
})

type LearningResourceExpandedV2Props = {
resourceId: number
resource?: LearningResource
user?: User
shareUrl?: string
Expand Down Expand Up @@ -658,13 +659,18 @@ const ResourceDescription = ({ resource }: { resource?: LearningResource }) => {
data-testid="drawer-description-text"
ref={descriptionRendered}
/**
* Resource descriptions can contain HTML. They are santiized on the
* Resource descriptions can contain HTML. They are sanitized on the
* backend during ETL. This is safe to render.
*/
dangerouslySetInnerHTML={{ __html: resource.description || "" }}
/>
{(isClamped || clampedOnFirstRender.current) && (
<Link color="red" size="small" onClick={() => setExpanded(!isExpanded)}>
<Link
scroll={false}
color="red"
size="small"
onClick={() => setExpanded(!isExpanded)}
>
{isExpanded ? "Show less" : "Show more"}
</Link>
)}
Expand All @@ -673,6 +679,7 @@ const ResourceDescription = ({ resource }: { resource?: LearningResource }) => {
}

const LearningResourceExpandedV2: React.FC<LearningResourceExpandedV2Props> = ({
resourceId,
resource,
imgConfig,
user,
Expand All @@ -684,8 +691,14 @@ const LearningResourceExpandedV2: React.FC<LearningResourceExpandedV2Props> = ({
onAddToUserListClick,
closeDrawer,
}) => {
const outerContainerRef = useRef<HTMLDivElement>(null)
useEffect(() => {
if (outerContainerRef.current && outerContainerRef.current.scrollTo) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this work instead?

  const outerContainerRef = useRef<HTMLDivElement>(null)
  useEffect(() => {
    if (outerContainerRef.current) {
      outerContainerRef.current.scrollTo(0, 0)
    }
  }, [resource?.id])

Only possible quirk i see is that if you scroll while the resource is still loading, it will scroll up when when the resource finishes loading.

If that seems undesirable, maybe pass the ID into v2expanded as a prop. (Alternatively the effect could run when searchParams.get("resource") changes, and eliminate the search params ref, but currently v2expanded has no knowledge of routing, e.g., it doesn't know the "resource" querykey is involved at all, mildly nice to presreve that..)

outerContainerRef.current.scrollTo(0, 0)
}
}, [resourceId])
return (
<OuterContainer>
<OuterContainer ref={outerContainerRef}>
<TitleSection
resource={resource}
closeDrawer={closeDrawer ?? (() => {})}
Expand Down
11 changes: 10 additions & 1 deletion frontends/ol-components/src/components/Link/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,17 @@ type LinkProps = LinkStyleProps &
* re-fetch API data.
*/
shallow?: boolean
scroll?: boolean
}

const BaseLink = ({ href, shallow, nohover, onClick, ...rest }: LinkProps) => {
const BaseLink = ({
href,
shallow,
nohover,
scroll,
onClick,
...rest
}: LinkProps) => {
if (process.env.NODE_ENV === "development") {
invariant(
!shallow || href?.startsWith("?"),
Expand All @@ -82,6 +90,7 @@ const BaseLink = ({ href, shallow, nohover, onClick, ...rest }: LinkProps) => {
}
return (
<NextLink
scroll={scroll}
href={href || ""}
{...rest}
onClick={
Expand Down
Loading