Skip to content

Commit

Permalink
refactor: no explicit any
Browse files Browse the repository at this point in the history
enabled eslint rule no-explicit-any and fixed reported errors
  • Loading branch information
mathiazom committed Sep 17, 2024
1 parent b17baf5 commit 33935ef
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 133 deletions.
1 change: 0 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module.exports = {
},
},
rules: {
"@typescript-eslint/no-explicit-any": "off", // TODO
"unused-imports/no-unused-imports": "error",
"import/no-named-as-default": "off",
"import/no-unresolved": "error",
Expand Down
2 changes: 1 addition & 1 deletion src/app/(main)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { loadQuery } from "studio/lib/store";

import styles from "./layout.module.css";

const hasValidData = (data: any) => data && Object.keys(data).length > 0;
const hasValidData = (data: unknown) => data && Object.keys(data).length > 0;

export default async function Layout({
children,
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/fetchData/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const clientWithToken = client.withConfig({ token });

interface FetchRequestBody {
query: string;
params?: Record<string, any>;
params?: Record<string, unknown>;
}

export async function POST(req: Request) {
Expand Down
35 changes: 17 additions & 18 deletions src/components/richText/RichText.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
"use client";

import { PortableText } from "@portabletext/react";
import { PortableText, PortableTextReactComponents } from "@portabletext/react";
import { PortableTextObject } from "@sanity/types";
import { ReactNode } from "react";

import Text from "src/components/text/Text";
import textStyles from "src/components/text/text.module.css";
import { useConvertSanityImageToNextImage } from "src/utils/hooks/useConvertImage";
import { getReactNodeTextContent } from "src/utils/reactNode";

import styles from "./richText.module.css";

type Children = {
_type: string;
marks: any[];
marks: string[];
text: string;
_key: string;
};
Expand All @@ -27,11 +30,11 @@ export type PortableTextBlock = {
_type: string;
};
alt?: string;
markDefs?: any[];
markDefs?: PortableTextObject[];
};

const formatId = (children: any): string => {
const text = children.join(" ");
const formatId = (children: ReactNode): string => {
const text = getReactNodeTextContent(children);

return text
.toLowerCase()
Expand All @@ -44,36 +47,32 @@ const SanityImage = ({ value }: { value: PortableTextBlock }) => {
return <div className={styles.image}>{ImageElement}</div>;
};

const myPortableTextComponents = {
const myPortableTextComponents: Partial<PortableTextReactComponents> = {
block: {
h2: ({ children }: any) => (
h2: ({ children }) => (
<Text type="h2" id={formatId(children)}>
{children}
</Text>
),
h3: ({ children }: any) => (
h3: ({ children }) => (
<Text type="h3" id={formatId(children)}>
{children}
</Text>
),
normal: ({ children }: any) => <Text type="body">{children}</Text>,
blockquote: ({ children }: any) => (
normal: ({ children }) => <Text type="body">{children}</Text>,
blockquote: ({ children }) => (
<blockquote className={`${styles.blockquote} ${textStyles.body}`}>
{children}
</blockquote>
),
},
list: {
bullet: ({ children }: any) => <ul className={styles.list}>{children}</ul>,
number: ({ children }: any) => <ol className={styles.list}>{children}</ol>,
bullet: ({ children }) => <ul className={styles.list}>{children}</ul>,
number: ({ children }) => <ol className={styles.list}>{children}</ol>,
},
listItem: {
bullet: ({ children }: any) => (
<li className={textStyles.body}>{children}</li>
),
number: ({ children }: any) => (
<li className={textStyles.body}>{children}</li>
),
bullet: ({ children }) => <li className={textStyles.body}>{children}</li>,
number: ({ children }) => <li className={textStyles.body}>{children}</li>,
},
types: {
image: SanityImage,
Expand Down
6 changes: 3 additions & 3 deletions src/components/sections/callout/Callout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PortableText } from "@portabletext/react";
import { PortableText, PortableTextReactComponents } from "@portabletext/react";

import CustomLink from "src/components/link/CustomLink";
import Text from "src/components/text/Text";
Expand All @@ -10,8 +10,8 @@ interface CalloutProps {
callout: CalloutSection;
}

const myPortableTextComponents = {
block: ({ children }: any) => <Text type="bodySuperLarge">{children}</Text>,
const myPortableTextComponents: Partial<PortableTextReactComponents> = {
block: ({ children }) => <Text type="bodySuperLarge">{children}</Text>,
};

const Callout = ({ callout }: CalloutProps) => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/sections/grid/Grid.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import { PortableText } from "@portabletext/react";
import { PortableText, PortableTextReactComponents } from "@portabletext/react";

import { PortableTextBlock } from "src/components/richText/RichText";
import Text from "src/components/text/Text";
Expand Down Expand Up @@ -54,6 +54,6 @@ const Element = ({
);
};

const myPortableTextComponents = {
block: ({ children }: any) => <Text type="small">{children}</Text>,
const myPortableTextComponents: Partial<PortableTextReactComponents> = {
block: ({ children }) => <Text type="small">{children}</Text>,
};
6 changes: 3 additions & 3 deletions src/components/sections/logoSalad/LogoSalad.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PortableText } from "@portabletext/react";
import { PortableText, PortableTextReactComponents } from "@portabletext/react";

import Text from "src/components/text/Text";
import { LogoSaladSection } from "studio/lib/interfaces/pages";
Expand All @@ -10,8 +10,8 @@ interface LogoSaladProps {
logoSalad: LogoSaladSection;
}

const myPortableTextComponents = {
block: ({ children }: any) => <Text type="bodySuperLarge">{children}</Text>,
const myPortableTextComponents: Partial<PortableTextReactComponents> = {
block: ({ children }) => <Text type="bodySuperLarge">{children}</Text>,
};

export const LogoSalad = ({ logoSalad }: LogoSaladProps) => {
Expand Down
6 changes: 3 additions & 3 deletions src/post/lead/Lead.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import { PortableText } from "@portabletext/react";
import { PortableText, PortableTextReactComponents } from "@portabletext/react";

import { PortableTextBlock } from "src/components/richText/RichText";
import Text from "src/components/text/Text";
Expand All @@ -8,8 +8,8 @@ import { IImage } from "studio/lib/interfaces/media";

import styles from "./lead.module.css";

const myPortableTextComponents = {
block: ({ children }: any) => <Text type="bodyLarge">{children}</Text>,
const myPortableTextComponents: Partial<PortableTextReactComponents> = {
block: ({ children }) => <Text type="bodyLarge">{children}</Text>,
};

const Lead = ({
Expand Down
20 changes: 20 additions & 0 deletions src/utils/reactNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ReactNode, isValidElement } from "react";

export function getReactNodeTextContent(node: ReactNode): string {
if (typeof node === "string" || typeof node === "number") {
return node.toString();
}

if (Array.isArray(node)) {
return node.map(getReactNodeTextContent).join("");
}

if (isValidElement(node)) {
const children = node.props.children;
if (children) {
return getReactNodeTextContent(children);
}
}

return "";
}
5 changes: 3 additions & 2 deletions src/utils/seo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { toPlainText } from "@portabletext/toolkit";
import type { QueryParams } from "@sanity/client";
import { Metadata } from "next";

import { PortableTextBlock } from "src/components/richText/RichText";
Expand Down Expand Up @@ -32,7 +33,7 @@ export const OPEN_GRAPH_IMAGE_DIMENSIONS = {

export async function fetchSeoData(
query: string,
variables?: any,
variables?: QueryParams | undefined,
): Promise<SeoData | null> {
try {
const { data } = await loadQuery<SeoData>(query, variables);
Expand All @@ -45,7 +46,7 @@ export async function fetchSeoData(

export async function fetchPostSeoData(
query: string,
variables?: any,
variables?: QueryParams | undefined,
): Promise<SeoData | null> {
try {
const { data } = await loadQuery<PostSeoData>(query, variables);
Expand Down
12 changes: 2 additions & 10 deletions studio/components/AnchorSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { Button, Select, Stack } from "@sanity/ui";
import React, { useEffect, useState } from "react";
import { PatchEvent, set, unset, useFormValue } from "sanity";
import { PatchEvent, StringInputProps, set, unset, useFormValue } from "sanity";

import { fetchWithToken } from "studio/lib/fetchWithToken";

interface AnchorSelectProps {
value?: string;
type: any;
onChange: (event: PatchEvent) => void;
path: any[];
schemaType: any;
}

interface AnchorItem {
basicTitle?: string;
value: string;
Expand All @@ -30,7 +22,7 @@ function fromCamelCase(value?: string) {
}); // Capitalize the first letter
}

const AnchorSelect = ({ value, onChange, path }: AnchorSelectProps) => {
const AnchorSelect = ({ value, onChange, path }: StringInputProps) => {
const [listItems, setListItems] = useState<AnchorItem[]>([]);

// Extract the internal link reference from the form value
Expand Down
26 changes: 23 additions & 3 deletions studio/components/SoMeInputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Box, Label, Select, Stack, TextInput } from "@sanity/ui";
import React from "react";
import { ObjectInputProps, set } from "sanity";

import { SocialMediaLink } from "studio/lib/interfaces/socialMedia";

export const SoMePlatforms: { [key: string]: string } = {
facebook: "Facebook",
x: "X",
Expand All @@ -23,10 +25,28 @@ const detectPlatformFromUrl = (url: string): string | null => {
return null;
};

const SoMeInputs: React.FC<ObjectInputProps<Record<string, any>>> = ({
function isSocialMediaLinkType(value: unknown): value is SocialMediaLink {
return (
typeof value === "object" &&
value !== null &&
"_type" in value &&
typeof value._type === "string" &&
"url" in value &&
typeof value.url === "string" &&
"platform" in value &&
typeof value.platform === "string"
);
}

const SoMeInputs: React.FC<ObjectInputProps<Record<string, unknown>>> = ({
value = {},
onChange,
}) => {
if (!isSocialMediaLinkType(value)) {
console.error("Unexpected value type for SoMeInputs");
return;
}

const handleUrlChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (!onChange) return;
const newUrl = event.target.value;
Expand Down Expand Up @@ -58,7 +78,7 @@ const SoMeInputs: React.FC<ObjectInputProps<Record<string, any>>> = ({
<TextInput
id={urlInputName}
name={urlInputName}
value={value.url}
value={value && value.url}
onChange={handleUrlChange}
placeholder="Enter URL"
/>
Expand All @@ -72,7 +92,7 @@ const SoMeInputs: React.FC<ObjectInputProps<Record<string, any>>> = ({
<Select
id={platformInputName}
name={platformInputName}
value={value.platform}
value={value && value.platform}
onChange={handlePlatformChange}
>
<option value="">Select Platform</option>
Expand Down
70 changes: 0 additions & 70 deletions studio/components/slug/ReferenceSlugInput.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion studio/lib/fetchWithToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
export const fetchWithToken = async <T>(
query: string,
params?: Record<string, any>,
params?: Record<string, unknown>,
): Promise<T> => {
const response = await fetch("/api/fetchData", {
method: "POST",
Expand Down
2 changes: 1 addition & 1 deletion studio/schemas/objects/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const link = defineField({
{ type: lazyCompensationsID() },
],
validation: (rule) =>
rule.custom((value: any, context: any) => {
rule.custom((value, context) => {
const parent = context.parent as Parent;
if (
parent?.linkTitle &&
Expand Down
Loading

0 comments on commit 33935ef

Please sign in to comment.