Skip to content

Commit a9c6276

Browse files
committed
fix(types): use strict type checking and fix all typing issues
HCRC-178. Use strict type rules. Enable skipLibCheck. Render null when Head-component is not configured. Fix the nullpointer exceptions. The LoadingButton is a basic HDS-React Button, which analyses isLoading state to show a loading spinner as a start icon and changes styles according to state as well. It mimics the example given in the HDS-React Storybook documentation in https://hds.hel.fi/components/button/code/#loading-button. The `handleEscapeKeyPress` cannot be a native KeyboardEvent and React.KeyboardEvent at the same time. If a `onKeyDown`-attribute is used, it needs to use the React-KeyboardEvent, but it should be exactly the same as adding an eventListener to KeyboardEvent. Added `classnames`-package to dependencies as it was needed by Link-component. test: update snapshots HCRC-178 fix(types): all stories default export argTypes typing HCRC-178 fix(types): simplify and fix useEventsByIdsQuery type handling Remove makeQueryWithEventsApolloClientFromConfig.ts as unnecessary, it was used in a single place. Now the code is simpler and easier to understand. Using HelmetWrapper the @ts-ignore clauses become unnecessary and are removed.
1 parent 143cdf8 commit a9c6276

79 files changed

Lines changed: 745 additions & 575 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/apollo/apollo-custom-template.stories.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import React from 'react';
44
import type { StoryFn, Meta } from '@storybook/react';
5-
import { Helmet, HelmetProvider } from 'react-helmet-async';
5+
import { HelmetProvider } from 'react-helmet-async';
66

77
import { ConfigProvider } from '../core/configProvider/ConfigProvider';
88
import { defaultConfig } from '../core/configProvider/defaultConfig';
@@ -15,6 +15,7 @@ import { LanguageCodeEnum } from '../core';
1515
import { useCmsEndpointConfig } from '../storybook-common/useCmsEndpointConfig';
1616
import type { CmsEndpoint } from '../storybook-common/constants';
1717
import { cmsMenuName, cmsTestPage } from '../storybook-common/constants';
18+
import { HelmetWrapper } from '../storybook-common/HelmetWrapper';
1819

1920
const ExampleNavigation = ({
2021
menuName,
@@ -61,8 +62,11 @@ const CustomPageContentLayout: typeof PageContentLayout = ({
6162
const getTemplate =
6263
(dataSource: keyof typeof CmsEndpoint): StoryFn<typeof Page> =>
6364
(args) => {
64-
const { apolloClient, eventsApolloClient, internalHrefOrigins } =
65-
useCmsEndpointConfig(dataSource);
65+
const {
66+
apolloClient,
67+
eventsApolloClient,
68+
internalHrefOrigins = [],
69+
} = useCmsEndpointConfig(dataSource);
6670
return (
6771
<HelmetProvider>
6872
<ConfigProvider
@@ -76,16 +80,14 @@ const getTemplate =
7680
venuesApolloClient: 'disabled',
7781
components: {
7882
...defaultConfig.components,
79-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
80-
// @ts-ignore
81-
Head: Helmet,
83+
Head: HelmetWrapper,
8284
},
8385
utils: {
8486
...defaultConfig.utils,
8587
getRoutedInternalHref: (link) => {
8688
let uri = '';
8789
internalHrefOrigins.forEach((d) => {
88-
uri = link.replace(d, '');
90+
uri = link?.replace(d, '') ?? '';
8991
});
9092
return uri;
9193
},

src/apollo/apollo.stories.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import React from 'react';
44
import type { StoryFn, Meta } from '@storybook/react';
5-
import { Helmet, HelmetProvider } from 'react-helmet-async';
5+
import { HelmetProvider } from 'react-helmet-async';
66

77
import { ConfigProvider } from '../core/configProvider/ConfigProvider';
88
import { defaultConfig } from '../core/configProvider/defaultConfig';
@@ -16,6 +16,7 @@ import { LanguageCodeEnum, getBreadcrumbsFromPage } from '../core';
1616
import { useCmsEndpointConfig } from '../storybook-common/useCmsEndpointConfig';
1717
import type { CmsEndpoint } from '../storybook-common/constants';
1818
import { cmsMenuName, cmsTestPage } from '../storybook-common/constants';
19+
import { HelmetWrapper } from '../storybook-common/HelmetWrapper';
1920

2021
const ExampleNavigation = ({
2122
menuName,
@@ -38,8 +39,11 @@ const ExampleNavigation = ({
3839
const getTemplate =
3940
(datasource: keyof typeof CmsEndpoint): StoryFn<typeof Page> =>
4041
(args) => {
41-
const { apolloClient, eventsApolloClient, internalHrefOrigins } =
42-
useCmsEndpointConfig(datasource);
42+
const {
43+
apolloClient,
44+
eventsApolloClient,
45+
internalHrefOrigins = [],
46+
} = useCmsEndpointConfig(datasource);
4347
return (
4448
<HelmetProvider>
4549
<ConfigProvider
@@ -53,16 +57,14 @@ const getTemplate =
5357
venuesApolloClient: 'disabled',
5458
components: {
5559
...defaultConfig.components,
56-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
57-
// @ts-ignore
58-
Head: Helmet,
60+
Head: HelmetWrapper,
5961
},
6062
utils: {
6163
...defaultConfig.utils,
6264
getRoutedInternalHref: (link) => {
6365
let uri = '';
6466
internalHrefOrigins.forEach((d) => {
65-
uri = link.replace(d, '');
67+
uri = link?.replace(d, '') ?? '';
6668
});
6769
return uri;
6870
},

src/apollo/navigation/Navigation.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function Navigation({
3333
});
3434
const languages =
3535
(languagesQuery.data?.languages?.filter(
36-
(l) => !!l,
36+
(language) => !!language,
3737
) as LanguageFragment[]) ?? undefined;
3838
return (
3939
<NavigationWithoutData

src/apollo/pageContent/PageContent.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ export function PageContent({
3030
const { uri } = useApolloPageContext();
3131
const pageQuery = usePageQuery({
3232
variables: {
33-
id: uri,
33+
id: uri ?? '',
3434
},
35+
skip: !uri,
3536
});
3637

3738
if (pageQuery.loading) {

src/common/components/htmlToReact/HtmlToReact.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import React from 'react';
44
import type { StoryFn, Meta } from '@storybook/react';
55

66
import kukkuuTestPage from '../../../mocks/responses/cms/page/kukkuu-page-demosivu.json';
7-
import type { HtmlToReactProps } from './HtmlToReact';
7+
import type { HtmlToReactProps, TableVariant } from './HtmlToReact';
88
import { HtmlToReact, TABLE_VARIANTS } from './HtmlToReact';
99
import { StoryContainer } from '../../../core/storyContainer/StoryContainer';
1010

@@ -80,7 +80,7 @@ const Template: StoryFn<typeof HtmlToReact> = ({
8080
trustedOrigins,
8181
tableVariants,
8282
}: HtmlToReactProps & {
83-
tableVariants: HtmlToReactProps['components']['tableVariants'];
83+
tableVariants?: TableVariant[];
8484
}) => (
8585
<StoryContainer>
8686
<HtmlToReact

src/common/components/htmlToReact/HtmlToReact.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ export function HtmlToReact({
311311
[dirty, allowedUnsafeTags],
312312
);
313313
const htmlReactParserOptions = {
314-
replace: (domNode) =>
314+
replace: (domNode: DOMNode) =>
315315
replaceDomNodeWithReactComponent(
316316
domNode,
317317
htmlReactParserOptions,

src/common/components/htmlToReact/__tests__/HtmlToReact.tests.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('HtmlToReact', () => {
5959
expect(iframes.length).toBe(expectedIframesCount);
6060
// Each iframe src.attribute is listed in the trusted origins
6161
iframes.forEach((iframe) => {
62-
expect(trustedOrigins.includes(new URL(iframe.src).origin));
62+
expect((trustedOrigins || []).includes(new URL(iframe.src).origin));
6363
});
6464
},
6565
);

src/common/components/tag/Tag.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ import styles from './tag.module.scss';
88
import { theme1, theme2 } from './tagThemes';
99

1010
type Props = {
11-
/**
12-
* Additional children to render inside the tag.
13-
*/
14-
children: React.ReactNode;
15-
/**
16-
* Additional classname for the tag.
17-
*/
18-
className?: string;
1911
/**
2012
* Boolean indicating whether the tag has a featured style variant.
2113
*/
@@ -32,7 +24,7 @@ type Props = {
3224
* onClick handler.
3325
*/
3426
onClick?: () => void;
35-
} & Pick<HDSTagProps, 'id' | 'role' | 'size'>;
27+
} & Pick<HDSTagProps, 'children' | 'className' | 'id' | 'role' | 'size'>;
3628

3729
export function Tag({
3830
className,
@@ -70,6 +62,7 @@ export function Tag({
7062
!onClick && styles.noOutline,
7163
className,
7264
)}
65+
placeholder={undefined}
7366
{...hdsTagProps}
7467
// TODO: don't use the generatedId and allow an undefined id.
7568
id={id ?? `tag${generatedId}`}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
export const theme1 = {
2-
'--tag-background': 'var(--color-black-10)',
3-
'--tag-color': 'var(--color-black)',
4-
'--tag-focus-outline-color': 'var(--color-black-70)',
1+
import type { TagTheme } from 'hds-react';
2+
3+
export const theme1: TagTheme = {
4+
'--background-color': 'var(--color-black-10)',
5+
'--color': 'var(--color-black)',
6+
'--outline-color': 'var(--color-black-70)',
57
};
68

7-
export const theme2 = {
8-
'--tag-background': 'var(--color-tram-light)',
9-
'--tag-color': 'var(--color-black-90)',
10-
'--tag-focus-outline-color': 'var(--color-black-70)',
9+
export const theme2: TagTheme = {
10+
'--background-color': 'var(--color-tram-light)',
11+
'--color': 'var(--color-black-90)',
12+
'--outline-color': 'var(--color-black-70)',
1113
};

src/common/components/text/Text.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,15 @@ function getElement(variant: TextVariant) {
5252
}
5353

5454
function Text({
55-
as,
55+
as: elementType,
5656
variant = 'body-l',
5757
children,
5858
className,
5959
...rest
6060
}: TextVariantProps) {
6161
return React.createElement(
62-
as || getElement(variant),
62+
elementType || getElement(variant),
6363
{
64-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
65-
// @ts-ignore: For some reason TS considers text to be undefined
6664
className: [styles.text, styles[variant], className].join(' '),
6765
...rest,
6866
},

0 commit comments

Comments
 (0)