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

fix: Ensure issuesGroupedState is up to date #348

Merged
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
2 changes: 1 addition & 1 deletion components/icons/svgr/SvgListViewIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const SvgDetailViewIcon = () => (
* From https://www.iconfinder.com/icons/326721/list_view_icon#svg
*/
<svg width="19" height="14" viewBox="0 0 19 14" xmlns="http://www.w3.org/2000/svg" version="1.1">
<g fill="none" fill-rule="evenodd" id="Page-1" stroke="#fff" stroke-width="1">
<g fill="none" fillRule="evenodd" id="Page-1" stroke="#fff" strokeWidth="1">
Copy link
Collaborator

Choose a reason for hiding this comment

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

🤔 Did this API change?

<g fill="#000" id="Core" transform="translate(-87.000000, -509.000000)">
<g id="view-list" transform="translate(87.500000, 509.000000)">
<path d="M0,9 L4,9 L4,5 L0,5 L0,9 L0,9 Z M0,14 L4,14 L4,10 L0,10 L0,14 L0,14 Z M0,4 L4,4 L4,0 L0,0 L0,4 L0,4 Z M5,9 L17,9 L17,5 L5,5 L5,9 L5,9 Z M5,14 L17,14 L17,10 L5,10 L5,14 L5,14 Z M5,0 L5,4 L17,4 L17,0 L5,0 L5,0 Z" id="Shape"/>
Expand Down
45 changes: 18 additions & 27 deletions components/roadmap-grid/RoadmapDetailedView.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Box, Spinner, Stack, Skeleton } from '@chakra-ui/react';
import { useHookstate } from '@hookstate/core';
import { hookstate, useHookstateMemo } from '@hookstate/core';
import type { Dayjs } from 'dayjs';
import _ from 'lodash';
import React, { useEffect, useMemo, useState } from 'react';
Expand All @@ -22,8 +22,6 @@ import { globalTimeScaler } from '../../lib/client/TimeScaler';
import { convertIssueDataStateToDetailedViewGroupOld } from '../../lib/client/convertIssueDataToDetailedViewGroup';
import { useRouter } from 'next/router';
import { ErrorBoundary } from '../errors/ErrorBoundary';
import { usePrevious } from '../../hooks/usePrevious';
import getUniqIdForGroupedIssues from '../../lib/client/getUniqIdForGroupedIssues';
import { useShowTodayMarker } from '../../hooks/useShowTodayMarker';
import { useGlobalLoadingState } from '../../hooks/useGlobalLoadingState';

Expand All @@ -36,19 +34,14 @@ export function RoadmapDetailed({
const [isDevMode, _setIsDevMode] = useState(false);
const viewMode = useViewMode() as ViewMode;
const router = useRouter();

const issuesGroupedState = useHookstate<DetailedViewGroup[]>([]);
const groupedIssuesId = getUniqIdForGroupedIssues(issuesGroupedState.value)
const groupedIssuesIdPrev = usePrevious(groupedIssuesId);
const globalLoadingState = useGlobalLoadingState();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moving global loading state up to the top

const query = router.query
const showTodayMarker = useShowTodayMarker();

const setIssuesGroupedState = issuesGroupedState.set
useEffect(() => {
if (viewMode && groupedIssuesIdPrev !== groupedIssuesId) {
setIssuesGroupedState(() => convertIssueDataStateToDetailedViewGroupOld(issueDataState, viewMode, query))
}
}, [viewMode, query, setIssuesGroupedState, issueDataState, groupedIssuesIdPrev, groupedIssuesId]);
const memoGrouped = useHookstateMemo(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

useHookstateMemo properly recognizes uniqueness of hookState objects.

() => convertIssueDataStateToDetailedViewGroupOld(issueDataState, viewMode, query),
[viewMode, query, issueDataState]
)
const issuesGroupedState = hookstate<DetailedViewGroup[]>(memoGrouped)

/**
* Magic numbers that just seem to work are:
Expand All @@ -63,15 +56,13 @@ export function RoadmapDetailed({
*/
const [numHeaderTicks, setNumHeaderTicks] = useState(5);
const [numGridCols, setNumGridCols] = useState(45);
const globalLoadingState = useGlobalLoadingState();


// for preventing dayjsDates from being recalculated if it doesn't need to be
const issuesGroupedId = issuesGroupedState.value.map((g) => g.groupName).join(',');
/**
* Collect all due dates from all issues, as DayJS dates.
*/
const dayjsDates: Dayjs[] = useMemo(() => {
const dayjsDates: Dayjs[] = useHookstateMemo(() => {
const today = dayjs();
let innerDayjsDates: Dayjs[] = []
try {
Expand Down Expand Up @@ -114,7 +105,7 @@ export function RoadmapDetailed({

return innerDayjsDates;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [issuesGroupedState.length, issuesGroupedId]);
}, [issuesGroupedState, issuesGroupedId]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

useHookstateMemo properly recognizes uniqueness of hookState objects.


/**
* * Ensure that the dates are
Expand All @@ -129,15 +120,6 @@ export function RoadmapDetailed({
globalTimeScaler.setScale(dates, numGridCols * 1.09);
}, [dates, numGridCols]);

const invalidGroups = issuesGroupedState.filter((group) => group.ornull == null || group.items.ornull == null)
if (issuesGroupedState.value.length === 0 || invalidGroups.length > 0) {
if (invalidGroups.length > 0) {
invalidGroups.forEach((g) => {
console.warn('Found an invalid group: ', g.value);
});
}
return <Spinner />;
}

// return early while loading.
if (globalLoadingState.get()) {
Expand All @@ -151,6 +133,15 @@ export function RoadmapDetailed({
</Stack>
)
}
const invalidGroups = issuesGroupedState.filter((group) => group.ornull == null || group.items.ornull == null)
if (issuesGroupedState.value.length === 0 || invalidGroups.length > 0) {
if (invalidGroups.length > 0) {
invalidGroups.forEach((g) => {
console.warn('Found an invalid group: ', g.value);
});
Comment on lines +139 to +141
Copy link
Collaborator

Choose a reason for hiding this comment

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

optionally

Suggested change
invalidGroups.forEach((g) => {
console.warn('Found an invalid group: ', g.value);
});
invalidGroups.forEach(({value}) => console.warn('Found an invalid group: ', value));

}
return <Spinner />;
}
Comment on lines +136 to +144
Copy link
Contributor Author

Choose a reason for hiding this comment

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

only processing invalidGroups after globalLoadingState check


/**
* Current getTicks function returns 1 less than the number of ticks we want.
Expand Down
3 changes: 1 addition & 2 deletions components/roadmap-grid/RoadmapTabbedView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ export function RoadmapTabbedView({
}

return (
<Skeleton isLoaded={!globalLoadingState.get()}>
<Skeleton isLoaded={!globalLoadingState.get()} key={index}>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix react warning

<Tab
className={styles.gridViewTab}
key={index}
>
<Center>
<TabIcon />
Expand Down
11 changes: 5 additions & 6 deletions components/roadmap-grid/grid-row.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import NextLink from 'next/link';
import { Flex, Text, Box, Center } from '@chakra-ui/react';
import React, { useEffect, useMemo, useState } from 'react';
import type { State } from '@hookstate/core'
import React, { useState } from 'react';
import { State, useHookstateEffect, useHookstateMemo } from '@hookstate/core'

import { dayjs } from '../../lib/client/dayjs';
import { IssueData, IssueDataViewInput } from '../../lib/types';
Expand Down Expand Up @@ -33,13 +33,12 @@ export function GridRow({
const viewMode = useViewMode();
const routerQuery = useRouter().query;
const [closestDateIdx, setClosestDateIdx] = useState(Math.round(globalTimeScaler.getColumn(dayjs.utc(milestone.due_date.get()).toDate())));
useEffect(() => {
useHookstateEffect(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note: useHookstateEffect is supposed to recognize hookstate objects, but doesn't seem to work properly, unlike useHookstateMemo (which does work properly, not rerunning the hook callback if hookstate object wasn't changed)

setClosestDateIdx(Math.round(globalTimeScaler.getColumn(dayjs.utc(milestone.due_date.get()).toDate())));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [milestone.due_date, globalTimeScaler.getDomain()]);
}, [milestone.due_date, ...globalTimeScaler.getDomain()]);
const span = Math.max(4, numGridCols / timelineTicks.length);
const closest = span * (closestDateIdx - 1);
const childLink = useMemo(() => getLinkForRoadmapChild({ viewMode, issueData: milestone.get(), query: routerQuery, currentRoadmapRoot: issueDataState.value }), [issueDataState.value, milestone, routerQuery, viewMode]);
const childLink = useHookstateMemo(() => getLinkForRoadmapChild({ viewMode, issueData: milestone.get(), query: routerQuery, currentRoadmapRoot: issueDataState.value }), [issueDataState, milestone, routerQuery, viewMode]);
const clickable = milestone.children.length > 0;

if (milestone == null || milestone.ornull == null) {
Expand Down