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

Improve type narrowing and error handling #1073

Merged
merged 10 commits into from
Nov 19, 2024
Prev Previous commit
Next Next commit
Properly check that flatData[0] is defined
The previous check was loosely coupled, relying on a non-null assertion.
This should be more clear to both the compiler and humans.
victorlin committed Nov 19, 2024
commit 7aaeb484c4850db7760308df52ccb42b1bb99482
10 changes: 5 additions & 5 deletions static-site/src/components/ListResources/Modal.tsx
Original file line number Diff line number Diff line change
@@ -146,15 +146,16 @@ function _snapshotSummary(dates: string[]) {
}

function _draw(ref, resource: VersionedResource) {
genehack marked this conversation as resolved.
Show resolved Hide resolved
// do nothing if resource has no dates
if (!resource.dates) return

/* Note that _page_ resizes by themselves will not result in this function
rerunning, which isn't great, but for a modal I think it's perfectly
acceptable */
const sortedDateStrings = [...resource.dates].sort();
const flatData = sortedDateStrings.map((version) => ({version, 'date': new Date(version)}));

if (flatData[0] === undefined) {
throw new InternalError("Resource does not have any dates.");
}

const width = ref.clientWidth;
const graphIndent = 20;
const heights = {
@@ -178,8 +179,7 @@ function _draw(ref, resource: VersionedResource) {

/* Create the x-scale and draw the x-axis */
const x = d3.scaleTime()
// presence of dates on resource has already been checked so this assertion is safe
.domain([flatData[0]!.date, new Date()]) // the domain extends to the present day
.domain([flatData[0].date, new Date()]) // the domain extends to the present day
.range([graphIndent, width-graphIndent])
svg.append('g')
.attr("transform", `translate(0, ${heights.height - heights.marginBelowAxis})`)