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: UTC time to active importers #70

Merged
merged 8 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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: 12 additions & 0 deletions client/src/app/dayjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import dayjs from "dayjs";
import isSameOrBefore from "dayjs/plugin/isSameOrBefore";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
import customParseFormat from "dayjs/plugin/customParseFormat";
import arraySupport from "dayjs/plugin/arraySupport";

dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(customParseFormat);
dayjs.extend(isSameOrBefore);
dayjs.extend(arraySupport);
17 changes: 7 additions & 10 deletions client/src/app/pages/advisory-details/vulnerabilities.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ import { NavLink } from "react-router-dom";
import dayjs from "dayjs";

import { Toolbar, ToolbarContent, ToolbarItem } from "@patternfly/react-core";
import {
Table,
Tbody,
Td,
Th,
Thead,
Tr
} from "@patternfly/react-table";
import { Table, Tbody, Td, Th, Thead, Tr } from "@patternfly/react-table";

import { VulnerabilityWithinAdvisory } from "@app/api/models";
import { FilterToolbar, FilterType } from "@app/components/FilterToolbar";
Expand Down Expand Up @@ -137,10 +130,14 @@ export const Vulnerabilities: React.FC<VulnerabilitiesProps> = ({
{item.non_normative.title}
</Td>
<Td width={10} {...getTdProps({ columnKey: "discovered" })}>
{formatDate(item.non_normative.discovered)}
{!!item.non_normative.discovered
? formatDate(item.non_normative.discovered)
: null}
Copy link
Member

Choose a reason for hiding this comment

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

why the unnecessary change of this block of code? what problem are we solving with this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@carlosthe19916, It's because I've made changes to https://github.com/trustification/trustify-ui/pull/70/files#diff-93d1e679f3a44569699332c49a4e0cf4e30ada958d50d897b3764cf2e20dd6f7R37-R39 where it makes more sense to not return undefined and therefore check for such case before calling formatDate.

Copy link
Member

Choose a reason for hiding this comment

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

  • Sorry but this change is replacing a simple line of code with a complex conditional longer code. This is is a step back in my opinion.
  • What specific problem is this change solving that the previous code was not covering?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@carlosthe19916,

formatDate(item.non_normative.discovered) what happens when formatDate returns undefined ?
React might break in such case and what should be done is to trap such case and return null.

Copy link
Member

Choose a reason for hiding this comment

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

  • formatDate is already returning undefined in multiple cases.
const myVar = formatDate(undefined); // Then myVar will be undefined

<>
{myVar}
</>

How is the previous code breaking reactjs? Can you give me a real concrete example and not suppositions?

  • I think the case is simple: Case1 vs Case2, let's explain why:
{!!item.non_normative.discovered
                        ? formatDate(item.non_normative.discovered)
                        : null}

is better than

{formatDate(item.non_normative.released)}

Why if item.non_normative.released is undefined it will break the code?

  • Couldn't we just do the following?:
export const formatDate = (value?: string) => {
  return value ? dayjs(value).format(RENDER_DATE_FORMAT) : null;
};

we are returning null as you want.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@carlosthe19916 , sorry for the confusion, what I mean by breaking the code is not reactjs friendly when the code renders {undefined}.
Well it seems that React 18 handles undefined better. But that shouldn't really happen anyway :
https://blog.saeloun.com/2022/04/14/react-18-allows-components-to-render-undfined/

Let's use your latest example and make sure formatDate and formatDateTime return null if undefined.

</Td>
<Td width={10} {...getTdProps({ columnKey: "released" })}>
{formatDate(item.non_normative.released)}
{!!item.non_normative.released
? formatDate(item.non_normative.released)
: null}
</Td>
<Td width={15} {...getTdProps({ columnKey: "severity" })}>
<SeverityShieldAndText value={item.severity} />
Expand Down
15 changes: 12 additions & 3 deletions client/src/app/pages/importer-list/importer-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
Thead,
Tr,
} from "@patternfly/react-table";
import dayjs from "dayjs";

import { Importer, ImporterType } from "@app/api/models";
import { ConfirmDialog } from "@app/components/ConfirmDialog";
Expand All @@ -38,7 +39,11 @@ import {
useDeleteiIporterMutation as useDeleteIporterMutation,
useFetchImporters,
} from "@app/queries/importers";
import { formatDate, getAxiosErrorMessage } from "@app/utils/utils";
import {
formatDate,
formatDateTime,
getAxiosErrorMessage,
} from "@app/utils/utils";

import { FilterToolbar, FilterType } from "@app/components/FilterToolbar";
import { SimplePagination } from "@app/components/SimplePagination";
Expand Down Expand Up @@ -250,14 +255,18 @@ export const ImporterList: React.FC = () => {
modifier="truncate"
{...getTdProps({ columnKey: "start" })}
>
{formatDate(item.report?.startDate)}
{!configValues?.disabled && !!item.report?.startDate
? formatDateTime(item.report?.startDate)
: null}
</Td>
<Td
width={15}
modifier="truncate"
{...getTdProps({ columnKey: "end" })}
>
{formatDate(item.report?.endDate)}
{!configValues?.disabled && !!item.report?.endDate
? formatDateTime(item.report?.endDate)
: null}
</Td>
<Td
width={10}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,18 @@ export const CveDetails: React.FC = () => {
<DescriptionListGroup>
<DescriptionListTerm>Published</DescriptionListTerm>
<DescriptionListDescription>
{formatDate(vulnerability?.published)}
{!!vulnerability?.published
? formatDate(vulnerability?.published)
: null}
<p style={{ color: "red" }}>issue-410</p>
</DescriptionListDescription>
</DescriptionListGroup>
<DescriptionListGroup>
<DescriptionListTerm>Modified</DescriptionListTerm>
<DescriptionListDescription>
{formatDate(vulnerability?.modified)}
{!!vulnerability?.modified
? formatDate(vulnerability?.modified)
: null}
<p style={{ color: "red" }}>issue-410</p>
</DescriptionListDescription>
</DescriptionListGroup>
Expand Down
10 changes: 6 additions & 4 deletions client/src/app/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AxiosError } from "axios";
import dayjs from "dayjs";
import { PackageURL } from "packageurl-js";

import { RENDER_DATE_FORMAT } from "@app/Constants";
import { RENDER_DATETIME_FORMAT, RENDER_DATE_FORMAT } from "@app/Constants";
import { DecomposedPurl } from "@app/api/models";
import { ToolbarChip } from "@patternfly/react-core";

Expand Down Expand Up @@ -34,9 +34,11 @@ export const getToolbarChipKey = (value: string | ToolbarChip) => {

// Dates

export const formatDate = (value?: string) => {
return value ? dayjs(value).format(RENDER_DATE_FORMAT) : undefined;
};
export const formatDate = (value: string) =>
dayjs(value).utc().format(RENDER_DATE_FORMAT);

export const formatDateTime = (value: string) =>
dayjs(value).utc().format(RENDER_DATETIME_FORMAT);

export const duplicateFieldCheck = <T>(
fieldKey: keyof T,
Expand Down
21 changes: 4 additions & 17 deletions client/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
import React from "react";

import { createRoot } from 'react-dom/client';
import { createRoot } from "react-dom/client";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";

import ENV from "@app/env";
import App from "@app/App";
import reportWebVitals from "@app/reportWebVitals";

import dayjs from "dayjs";
import isSameOrBefore from "dayjs/plugin/isSameOrBefore";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
import customParseFormat from "dayjs/plugin/customParseFormat";
import arraySupport from "dayjs/plugin/arraySupport";

import "@app/dayjs";
import { OidcProvider } from "@app/components/OidcProvider";

dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(customParseFormat);
dayjs.extend(isSameOrBefore);
dayjs.extend(arraySupport);

const queryClient = new QueryClient();

const container = document.getElementById('root');
const root = createRoot(container!)
const container = document.getElementById("root");
const root = createRoot(container!);

const renderApp = () => {
return root.render(
Expand Down