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
5 changes: 5 additions & 0 deletions .changeset/tidy-lines-render.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@devgateway/dvz-ui-react": patch
---

Fix new timeline crashing on missing categories and re-drawing on every render by memoizing the parsed config
20 changes: 16 additions & 4 deletions packages/dvz-ui/src/embeddable/new-time-line/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {PostConsumer, PostProvider} from "@devgateway/wp-react-lib";
import PostIntro from "../connected-templates/PostIntro";
import React, {useState} from "react";
import React, {useMemo, useState} from "react";
import {Container} from "semantic-ui-react";
import {CarouselProvider, Slide, Slider} from "pure-react-carousel";
import TimeLine from "./timeline";
Expand Down Expand Up @@ -54,11 +54,23 @@ const Component = (props) => {
}
return decodeURIComponent(value)
}

const parse = (value) => {
return JSON.parse(decode(value))
if (value === undefined || value === null || value === "") {
return undefined
}
Comment thread
Copilot marked this conversation as resolved.
Resolved
const decoded = decode(value)
try {
return JSON.parse(decoded)
} catch (e) {
return decoded.split(",").map(v => v.trim()).filter(Boolean)
}
}

// keep the same object between renders so the timeline's effect (which
// depends on `config`) only re-runs when the config actually changes
const parsedConfig = useMemo(() => JSON.parse(decodeURIComponent(config)), [config])

const timeProps = {
marginLeft,
marginTop,
Expand All @@ -68,7 +80,7 @@ const Component = (props) => {
height: 150,
position,
lineColor: decode(lineColor),
config: JSON.parse(decodeURIComponent(config)),
config: parsedConfig,
fontSize,
titleWidth,
subtitleWidth
Expand Down
5 changes: 4 additions & 1 deletion packages/dvz-ui/src/embeddable/new-time-line/timeline.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ const TimeLine = ({
/*making svg take available width*/
const svgElement = d3.select(ref.current)
svgElement.attr("width", svgWidth).attr("height", svgHeight)
/* redraw from scratch when settings or posts change */
svgElement.selectAll("*").remove()


const onclick=(i)=>{
Expand Down Expand Up @@ -273,7 +275,8 @@ const TimeLine = ({



}, [])
}, [config, position, lineWidth, lineColor, height, marginLeft, marginTop, marginRight,
marginBottom, fontSize, titleWidth, subtitleWidth, posts, currentSlide])



Expand Down
Loading