diff --git a/src/hooks/makeSearch.ts b/src/hooks/makeSearch.ts index 514e8d8f9..e0e4800e9 100644 --- a/src/hooks/makeSearch.ts +++ b/src/hooks/makeSearch.ts @@ -6,7 +6,7 @@ import makeQuery, { UseQueryResult } from "./makeQuery"; import useDebounce from "./useDebounce"; export interface SearchVariables { - after?: string; + after?: string | null; first: number; query: string; } diff --git a/src/siteSettings/components/SiteSettingsPage/SiteSettingsPage.stories.tsx b/src/siteSettings/components/SiteSettingsPage/SiteSettingsPage.stories.tsx index 15160f2fd..b1839fd04 100644 --- a/src/siteSettings/components/SiteSettingsPage/SiteSettingsPage.stories.tsx +++ b/src/siteSettings/components/SiteSettingsPage/SiteSettingsPage.stories.tsx @@ -9,7 +9,7 @@ import SiteSettingsPage, { SiteSettingsPageProps } from "./SiteSettingsPage"; const props: Omit = { disabled: false, errors: [], - onSubmit: () => undefined, + onSubmit: async () => undefined, saveButtonBarState: "default", shop, }; diff --git a/src/utils/api.ts b/src/utils/api.ts index eb9e82560..361f5eb9c 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -7,6 +7,6 @@ export enum GqlErrors { export function hasError(err: ApolloError, ...errorCodes: string[]): boolean { return err.graphQLErrors.some(gqlError => - errorCodes.includes(gqlError.extensions.exception.code), + errorCodes.includes(gqlError.extensions?.exception.code), ); } diff --git a/src/utils/credentialsManagement.ts b/src/utils/credentialsManagement.ts index 5fcf0712f..88151fe8c 100644 --- a/src/utils/credentialsManagement.ts +++ b/src/utils/credentialsManagement.ts @@ -1,31 +1,30 @@ -import { LoginData } from "@saleor/sdk"; +import { UserFragment } from "@dashboard/graphql"; +import { UserDetailsFragment } from "@saleor/sdk/dist/apollo/types"; -export const isSupported = !!( - navigator?.credentials?.preventSilentAccess && window.PasswordCredential -); +export const isSupported = !!window.PasswordCredential; export async function login( loginFn: (id: string, password: string) => Promise, ): Promise { - let result: T; + let result: T | null; try { const credential = await navigator.credentials.get({ password: true }); if (credential instanceof PasswordCredential) { - result = await loginFn(credential.id, credential.password); + result = await loginFn(credential.id, credential.password ?? ""); } } catch { result = null; } - return result; + return result!; } export function saveCredentials( - user: LoginData["user"], + user: UserFragment | UserDetailsFragment, password: string, -): Promise { - let result: Promise; +): Promise | null { + let result: Promise | null; if (isSupported) { const cred = new PasswordCredential({ diff --git a/src/utils/data/getPublicationData.ts b/src/utils/data/getPublicationData.ts index 1bfac52ce..dd8139a87 100644 --- a/src/utils/data/getPublicationData.ts +++ b/src/utils/data/getPublicationData.ts @@ -1,5 +1,5 @@ interface PublicationData { - publicationDate: string; + publicationDate: string | null; isPublished: boolean; } diff --git a/src/utils/errors/account.ts b/src/utils/errors/account.ts index 1cffaaa0b..d6dad684c 100644 --- a/src/utils/errors/account.ts +++ b/src/utils/errors/account.ts @@ -1,5 +1,8 @@ import { AccountErrorCode } from "@dashboard/graphql"; -import { SetPasswordData } from "@saleor/sdk"; +import { + AccountError, + AccountErrorCode as SdkAccountErrorCode, +} from "@saleor/sdk/dist/apollo/types"; import { defineMessages, IntlShape } from "react-intl"; import { getCommonFormFieldErrorMessage } from "./common"; @@ -48,11 +51,14 @@ const messages = defineMessages({ }); interface ErrorFragment { - code: AccountErrorCode | SetPasswordData["errors"][number]["code"]; + code: AccountErrorCode; field: string | null; } -function getAccountErrorMessage(err: ErrorFragment, intl: IntlShape): string { +function getAccountErrorMessage( + err: ErrorFragment | Omit, + intl: IntlShape, +): string | undefined { if (err) { switch (err.code) { case AccountErrorCode.INVALID_PASSWORD: @@ -78,7 +84,10 @@ function getAccountErrorMessage(err: ErrorFragment, intl: IntlShape): string { } } - return getCommonFormFieldErrorMessage(err, intl); + return getCommonFormFieldErrorMessage( + err, + intl, + ); } export default getAccountErrorMessage; diff --git a/src/utils/errors/app.ts b/src/utils/errors/app.ts index 86c8f7f0b..d5e46777c 100644 --- a/src/utils/errors/app.ts +++ b/src/utils/errors/app.ts @@ -38,7 +38,10 @@ const messages = defineMessages({ }, }); -function getAppErrorMessage(err: AppErrorFragment, intl: IntlShape): string { +function getAppErrorMessage( + err: AppErrorFragment, + intl: IntlShape, +): string | undefined { if (err) { switch (err.code) { case AppErrorCode.INVALID_MANIFEST_FORMAT: diff --git a/src/utils/errors/attribute.ts b/src/utils/errors/attribute.ts index 910c836a2..db6a6ed28 100644 --- a/src/utils/errors/attribute.ts +++ b/src/utils/errors/attribute.ts @@ -21,7 +21,7 @@ const messages = defineMessages({ function getAttributeErrorMessage( err: Omit | undefined, intl: IntlShape, -): string { +): string | undefined { if (err) { switch (err.code) { case AttributeErrorCode.ALREADY_EXISTS: diff --git a/src/utils/errors/channels.ts b/src/utils/errors/channels.ts index 558047bfb..21353d826 100644 --- a/src/utils/errors/channels.ts +++ b/src/utils/errors/channels.ts @@ -21,7 +21,7 @@ const messages = defineMessages({ function getChannelsErrorMessage( err: Omit | undefined, intl: IntlShape, -): string { +): string | undefined { if (err) { switch (err.code) { case ChannelErrorCode.ALREADY_EXISTS: diff --git a/src/utils/errors/common.ts b/src/utils/errors/common.ts index 68c8afbd4..b257a741a 100644 --- a/src/utils/errors/common.ts +++ b/src/utils/errors/common.ts @@ -33,7 +33,7 @@ export interface CommonError { export function getCommonFormFieldErrorMessage( error: CommonError | undefined, intl: IntlShape, -): string { +): string | undefined { if (error) { switch (error.code) { case "GRAPHQL_ERROR": diff --git a/src/utils/errors/discounts.ts b/src/utils/errors/discounts.ts index 850515383..32a300b38 100644 --- a/src/utils/errors/discounts.ts +++ b/src/utils/errors/discounts.ts @@ -14,7 +14,7 @@ const messages = defineMessages({ function getDiscountErrorMessage( err: Omit | undefined, intl: IntlShape, -): string { +): string | undefined { if (err) { switch (err.code) { case DiscountErrorCode.ALREADY_EXISTS: diff --git a/src/utils/errors/export.ts b/src/utils/errors/export.ts index 73d7f43fb..dcdf43793 100644 --- a/src/utils/errors/export.ts +++ b/src/utils/errors/export.ts @@ -6,7 +6,7 @@ import { getCommonFormFieldErrorMessage } from "./common"; function getExportErrorMessage( err: Omit | undefined, intl: IntlShape, -): string { +): string | undefined { return getCommonFormFieldErrorMessage(err, intl); } diff --git a/src/utils/errors/index.ts b/src/utils/errors/index.ts index 1a511f3ef..5cfaa7886 100644 --- a/src/utils/errors/index.ts +++ b/src/utils/errors/index.ts @@ -3,20 +3,14 @@ import { UserError } from "@dashboard/types"; export function getFieldError( errors: T[], field: string, -): T { +): T | undefined { return errors.find(err => err.field === field); } -export function getErrors(errors: UserError[]): string[] { - return errors - .filter(err => ["", null].includes(err.field)) - .map(err => err.message); -} - export type FormErrors< TField extends string, TError extends UserError -> = Record; +> = Record; export function getFormErrors( fields: TField[], @@ -25,7 +19,7 @@ export function getFormErrors( return fields.reduce((errs, field) => { errs[field] = getFieldError(errors, field); return errs; - }, ({} as unknown) as Record); + }, ({} as unknown) as Record); } export interface ChannelError { diff --git a/src/utils/errors/invoice.ts b/src/utils/errors/invoice.ts index 60e53c710..710ea3974 100644 --- a/src/utils/errors/invoice.ts +++ b/src/utils/errors/invoice.ts @@ -40,7 +40,7 @@ const messages = defineMessages({ function getInvoiceErrorMessage( err: InvoiceErrorFragment, intl: IntlShape, -): string { +): string | undefined { if (err) { switch (err.code) { case InvoiceErrorCode.EMAIL_NOT_SET: diff --git a/src/utils/errors/menu.ts b/src/utils/errors/menu.ts index 1673ebb50..9482bef77 100644 --- a/src/utils/errors/menu.ts +++ b/src/utils/errors/menu.ts @@ -6,7 +6,7 @@ import { getCommonFormFieldErrorMessage } from "./common"; function getMenuErrorMessage( err: Omit | undefined, intl: IntlShape, -): string { +): string | undefined { return getCommonFormFieldErrorMessage(err, intl); } diff --git a/src/utils/errors/order.ts b/src/utils/errors/order.ts index 87e787417..93b404f43 100644 --- a/src/utils/errors/order.ts +++ b/src/utils/errors/order.ts @@ -75,7 +75,7 @@ const messages = defineMessages({ function getOrderErrorMessage( err: OrderErrorFragment, intl: IntlShape, -): string { +): string | undefined { if (err) { switch (err.code) { case OrderErrorCode.BILLING_ADDRESS_NOT_SET: diff --git a/src/utils/errors/page.ts b/src/utils/errors/page.ts index ca3c0176c..2d5c24e4f 100644 --- a/src/utils/errors/page.ts +++ b/src/utils/errors/page.ts @@ -29,7 +29,7 @@ const messages = defineMessages({ function getPageErrorMessage( err: Omit | undefined, intl: IntlShape, -): string { +): string | undefined { if (err) { switch (err.code) { case PageErrorCode.UNIQUE: diff --git a/src/utils/errors/permissionGroups.ts b/src/utils/errors/permissionGroups.ts index 43d4ddb6b..59d248dc4 100644 --- a/src/utils/errors/permissionGroups.ts +++ b/src/utils/errors/permissionGroups.ts @@ -32,7 +32,7 @@ const messages = defineMessages({ function getPermissionGroupErrorMessage( err: PermissionGroupErrorFragment, intl: IntlShape, -): string { +): string | undefined { if (err) { switch (err.code) { case PermissionGroupErrorCode.ASSIGN_NON_STAFF_MEMBER: diff --git a/src/utils/errors/plugins.ts b/src/utils/errors/plugins.ts index 3282122a5..a54568038 100644 --- a/src/utils/errors/plugins.ts +++ b/src/utils/errors/plugins.ts @@ -17,7 +17,7 @@ const messages = defineMessages({ function getPluginErrorMessage( err: PluginErrorFragment, intl: IntlShape, -): string { +): string | undefined { if (err) { switch (err.code) { case PluginErrorCode.PLUGIN_MISCONFIGURED: diff --git a/src/utils/errors/product.ts b/src/utils/errors/product.ts index 9c2999935..bc361afbc 100644 --- a/src/utils/errors/product.ts +++ b/src/utils/errors/product.ts @@ -83,7 +83,7 @@ function getProductErrorMessage( > | undefined, intl: IntlShape, -): string { +): string | undefined { if (err) { switch (err.code) { case ProductErrorCode.ATTRIBUTE_ALREADY_ASSIGNED: @@ -119,7 +119,7 @@ function getProductErrorMessage( export function getProductVariantAttributeErrorMessage( err: Omit | undefined, intl: IntlShape, -): string { +): string | undefined { if (err) { switch (err.code) { case ProductErrorCode.UNIQUE: @@ -135,7 +135,7 @@ export function getProductVariantAttributeErrorMessage( export function getBulkProductErrorMessage( err: BulkProductErrorFragment | undefined, intl: IntlShape, -): string { +): string | undefined { if (err?.code === ProductErrorCode.UNIQUE && err.field === "sku") { return intl.formatMessage(messages.skuUnique); } diff --git a/src/utils/errors/shipping.ts b/src/utils/errors/shipping.ts index b8cdec643..c9d7edbf8 100644 --- a/src/utils/errors/shipping.ts +++ b/src/utils/errors/shipping.ts @@ -19,7 +19,7 @@ const messages = defineMessages({ function getShippingErrorMessage( err: Omit | undefined, intl: IntlShape, -): string { +): string | undefined { if (err) { switch (err.code) { case ShippingErrorCode.ALREADY_EXISTS: diff --git a/src/utils/errors/shop.ts b/src/utils/errors/shop.ts index 19cb03d0f..c96fc7cdf 100644 --- a/src/utils/errors/shop.ts +++ b/src/utils/errors/shop.ts @@ -14,7 +14,7 @@ const messages = defineMessages({ function getShopErrorMessage( err: Omit | undefined, intl: IntlShape, -): string { +): string | undefined { if (err) { switch (err.code) { case ShopErrorCode.ALREADY_EXISTS: diff --git a/src/utils/errors/staff.ts b/src/utils/errors/staff.ts index a9affb4fb..4debcbb49 100644 --- a/src/utils/errors/staff.ts +++ b/src/utils/errors/staff.ts @@ -6,7 +6,7 @@ import getAccountErrorMessage from "./account"; function getStaffErrorMessage( err: StaffErrorFragment, intl: IntlShape, -): string { +): string | undefined { return getAccountErrorMessage(err, intl); } diff --git a/src/utils/errors/stock.ts b/src/utils/errors/stock.ts index 349d2deb8..a2ab76c2a 100644 --- a/src/utils/errors/stock.ts +++ b/src/utils/errors/stock.ts @@ -20,7 +20,7 @@ const messages = defineMessages({ function getStockErrorMessage( err: Omit | undefined, intl: IntlShape, -): string { +): string | undefined { if (err) { switch (err.code) { case StockErrorCode.UNIQUE: @@ -34,7 +34,7 @@ function getStockErrorMessage( export function getBulkStockErrorMessage( err: Omit | undefined, intl: IntlShape, -): string { +): string | undefined { return getProductErrorMessage(err, intl); } diff --git a/src/utils/errors/taxes.ts b/src/utils/errors/taxes.ts index 18912ca19..9af669391 100644 --- a/src/utils/errors/taxes.ts +++ b/src/utils/errors/taxes.ts @@ -20,7 +20,7 @@ export type TaxClassError = function getTaxesErrorMessage( err: Omit | undefined, intl: IntlShape, -): string { +): string | undefined { return getCommonFormFieldErrorMessage(err, intl); } diff --git a/src/utils/errors/warehouse.ts b/src/utils/errors/warehouse.ts index 0d14a49f2..a2ff78902 100644 --- a/src/utils/errors/warehouse.ts +++ b/src/utils/errors/warehouse.ts @@ -14,14 +14,14 @@ const messages = defineMessages({ function getWarehouseErrorMessage( err: Omit | undefined, intl: IntlShape, -): string { +): string | undefined { return getCommonFormFieldErrorMessage(err, intl); } export function getWarehouseSlugErrorMessage( err: Omit | undefined, intl: IntlShape, -): string { +): string | undefined { if (err) { switch (err.code) { case WarehouseErrorCode.UNIQUE: diff --git a/src/utils/errors/webhooks.ts b/src/utils/errors/webhooks.ts index cc1a1c079..05e0994e0 100644 --- a/src/utils/errors/webhooks.ts +++ b/src/utils/errors/webhooks.ts @@ -6,7 +6,7 @@ import { getCommonFormFieldErrorMessage } from "./common"; function getWebhookErrorMessage( err: Omit | undefined, intl: IntlShape, -): string { +): string | undefined { return getCommonFormFieldErrorMessage(err, intl); } diff --git a/src/utils/filters/fields.ts b/src/utils/filters/fields.ts index 5847d85f2..d0e9a6a93 100644 --- a/src/utils/filters/fields.ts +++ b/src/utils/filters/fields.ts @@ -161,6 +161,6 @@ export function createBooleanField( }, ], type: FieldType.boolean, - value: [defaultValue?.toString()], + value: [defaultValue?.toString() ?? ""], }; } diff --git a/src/utils/filters/filters.ts b/src/utils/filters/filters.ts index 2da40d964..9a2216a8f 100644 --- a/src/utils/filters/filters.ts +++ b/src/utils/filters/filters.ts @@ -91,7 +91,7 @@ export function getSingleValueQueryParam< >(param: FilterElement, key: TUrlKey) { const { active, value } = param; - if (!active) { + if (!active || !value) { return { [key]: undefined, }; @@ -161,7 +161,7 @@ export function getMinMaxQueryParam< >(param: FilterElement, keyFrom: TUrlKey, keyTo: TUrlKey) { const { active, multiple, value } = param; - if (!active) { + if (!active || !value) { return { [keyFrom]: undefined, [keyTo]: undefined, diff --git a/src/utils/filters/storage.ts b/src/utils/filters/storage.ts index 652988f9c..e379337ec 100644 --- a/src/utils/filters/storage.ts +++ b/src/utils/filters/storage.ts @@ -8,7 +8,8 @@ export type GetFilterTabsOutput = Array>; function getFilterTabs( key: string, ): GetFilterTabsOutput { - return JSON.parse(localStorage.getItem(key)) || []; + const filterTabs = localStorage.getItem(key); + return filterTabs ? JSON.parse(filterTabs) : []; } function saveFilterTab( diff --git a/src/utils/handlers/metadataCreateHandler.ts b/src/utils/handlers/metadataCreateHandler.ts index 70c68e3d0..c0ff0656a 100644 --- a/src/utils/handlers/metadataCreateHandler.ts +++ b/src/utils/handlers/metadataCreateHandler.ts @@ -20,7 +20,7 @@ function createMetadataCreateHandler( return async (data: T) => { const { id, errors } = await create(data); - if (id === null || !!errors?.length) { + if (!id || !!errors?.length) { return errors; } @@ -33,8 +33,8 @@ function createMetadataCreateHandler( }, }); const updateMetaErrors = [ - ...(updateMetaResult.data.deleteMetadata.errors || []), - ...(updateMetaResult.data.updateMetadata.errors || []), + ...(updateMetaResult.data?.deleteMetadata?.errors || []), + ...(updateMetaResult.data?.updateMetadata?.errors || []), ]; if (updateMetaErrors.length > 0) { @@ -52,8 +52,8 @@ function createMetadataCreateHandler( }); const updatePrivateMetaErrors = [ - ...(updatePrivateMetaResult.data.deletePrivateMetadata.errors || []), - ...(updatePrivateMetaResult.data.updatePrivateMetadata.errors || []), + ...(updatePrivateMetaResult.data?.deletePrivateMetadata?.errors || []), + ...(updatePrivateMetaResult.data?.updatePrivateMetadata?.errors || []), ]; if (updatePrivateMetaErrors.length > 0) { diff --git a/src/utils/handlers/metadataUpdateHandler.ts b/src/utils/handlers/metadataUpdateHandler.ts index e28b0c05a..fb80a1d12 100644 --- a/src/utils/handlers/metadataUpdateHandler.ts +++ b/src/utils/handlers/metadataUpdateHandler.ts @@ -62,8 +62,8 @@ function createMetadataUpdateHandler( }); const updateMetaErrors = [ - ...(updateMetaResult.data.deleteMetadata.errors || []), - ...(updateMetaResult.data.updateMetadata.errors || []), + ...(updateMetaResult.data?.deleteMetadata?.errors || []), + ...(updateMetaResult.data?.updateMetadata?.errors || []), ]; if (updateMetaErrors.length > 0) { @@ -84,8 +84,10 @@ function createMetadataUpdateHandler( }); const updatePrivateMetaErrors = [ - ...(updatePrivateMetaResult.data.deletePrivateMetadata.errors || []), - ...(updatePrivateMetaResult.data.updatePrivateMetadata.errors || []), + ...(updatePrivateMetaResult.data?.deletePrivateMetadata?.errors || + []), + ...(updatePrivateMetaResult.data?.updatePrivateMetadata?.errors || + []), ]; if (updatePrivateMetaErrors.length > 0) { diff --git a/src/utils/handlers/multiAutocompleteSelectChangeHandler.ts b/src/utils/handlers/multiAutocompleteSelectChangeHandler.ts index 36bec8216..7b98b1861 100644 --- a/src/utils/handlers/multiAutocompleteSelectChangeHandler.ts +++ b/src/utils/handlers/multiAutocompleteSelectChangeHandler.ts @@ -20,6 +20,10 @@ function createMultiAutocompleteSelectHandler( const id = event.target.value; const choice = combinedChoices.find(choice => choice.value === id); + if (!choice) { + return; + } + setSelected(toggle(choice, selected, (a, b) => a.value === b.value)); }; } diff --git a/src/utils/maps.ts b/src/utils/maps.ts index 198bbf11b..5c729096b 100644 --- a/src/utils/maps.ts +++ b/src/utils/maps.ts @@ -8,10 +8,11 @@ import { CountryWithCodeFragment, MetadataInput, MetadataItemFragment, - SearchPagesQuery, + PageFragment, } from "@dashboard/graphql"; import { getFullName } from "@dashboard/misc"; -import { Node, RelayToFlat, SlugNode, TagNode } from "@dashboard/types"; +import { Node, SlugNode, TagNode } from "@dashboard/types"; +import { Choice } from "@saleor/macaw-ui"; interface Edge { node: T; @@ -40,8 +41,8 @@ export function mapCountriesToChoices(countries: CountryWithCodeFragment[]) { } export function mapPagesToChoices( - pages: RelayToFlat, -) { + pages: Array>, +): Choice[] { return pages.map(page => ({ label: page.title, value: page.id, @@ -105,6 +106,10 @@ export function mapMultiValueNodeToChoice>( return (nodes as string[]).map(node => ({ label: node, value: node })); } + if (!key) { + return []; + } + return (nodes as T[]).map(node => ({ label: node[key], value: node[key] })); } @@ -120,6 +125,10 @@ export function mapSingleValueNodeToChoice>( return (nodes as string[]).map(node => ({ label: node, value: node })); } + if (!key) { + return []; + } + return (nodes as T[]).map(node => ({ label: node[key], value: node[key] })); } diff --git a/src/utils/menu/menu.ts b/src/utils/menu/menu.ts index 02f13fa92..f0435291b 100644 --- a/src/utils/menu/menu.ts +++ b/src/utils/menu/menu.ts @@ -27,9 +27,11 @@ export type IFlatMenu = Array< export function validateMenuOptions( menu: IMenu, ): boolean { + const isValue = (val: TValue | undefined): val is TValue => val !== undefined; + const values: TValue[] = toFlat(menu) .map(menuItem => menuItem.value) - .filter(value => value !== undefined); + .filter(isValue); const uniqueValues = Array.from(new Set(values)); return uniqueValues.length === values.length; } @@ -56,9 +58,9 @@ export function getMenuItemByValue( value: TValue, ): IMenuItem { const flatMenu = toFlat(menu); - const flatMenuItem: IFlatMenuItem = flatMenu.find( - menuItem => menuItem.value === value, - ); + const flatMenuItem: + | IFlatMenuItem + | undefined = flatMenu.find(menuItem => menuItem.value === value); if (flatMenuItem === undefined) { throw new Error(`Value ${value} does not exist in menu`); @@ -94,6 +96,10 @@ function _walkToRoot( ): IFlatMenu { const menuItem = flatMenu.find(menuItem => menuItem.id === parent); + if (menuItem === undefined) { + throw new Error(`Value ${parent} does not exist in menu`); + } + if (menuItem.parent === null) { return [menuItem]; } @@ -107,6 +113,10 @@ export function walkToRoot( const flatMenu = toFlat(menu); const menuItem = flatMenu.find(menuItem => menuItem.value === value); + if (menuItem === undefined) { + throw new Error(`Value ${value} does not exist in menu`); + } + return (menuItem.parent === null ? [menuItem] : [menuItem, ..._walkToRoot(flatMenu, menuItem.parent)] @@ -116,7 +126,7 @@ export function walkToRoot( function _toFlat( menuItem: IMenuItem, sort: number, - parent: string, + parent: string | null, ): IFlatMenu { const id = parent ? [parent, sort].join(":") : sort.toString(); const flatMenuItem: IFlatMenuItem = { diff --git a/src/utils/richText/useRichText.ts b/src/utils/richText/useRichText.ts index 44abde192..c71a186dc 100644 --- a/src/utils/richText/useRichText.ts +++ b/src/utils/richText/useRichText.ts @@ -1,19 +1,27 @@ import { EditorCore } from "@dashboard/components/RichTextEditor"; import { OutputData } from "@editorjs/editorjs"; -import { useMemo, useRef, useState } from "react"; +import { MutableRefObject, useMemo, useRef, useState } from "react"; interface UseRichTextOptions { - initial: string | null; + initial: string | null | undefined; loading?: boolean; triggerChange: () => void; } +interface UseRichTextResult { + editorRef: MutableRefObject; + handleChange: () => void; + getValue: () => Promise; + defaultValue: OutputData | undefined; + isReadyForMount: boolean; +} + export function useRichText({ initial, loading, triggerChange, -}: UseRichTextOptions) { - const editorRef = useRef(null); +}: UseRichTextOptions): UseRichTextResult { + const editorRef = useRef(null); const [isReadyForMount, setIsReadyForMount] = useState(false); const handleChange = () => { @@ -33,7 +41,7 @@ export function useRichText({ return; } - if (initial === undefined) { + if (!initial) { setIsReadyForMount(true); return ""; } diff --git a/src/utils/sort.ts b/src/utils/sort.ts index 1cec2eade..05fef6c65 100644 --- a/src/utils/sort.ts +++ b/src/utils/sort.ts @@ -72,7 +72,7 @@ type GetSortQueryField = ( type GetSortQueryVariables< TSortField extends string, TParams extends Record -> = (params: TParams) => SortingInput; +> = (params: TParams) => SortingInput | undefined; export function createGetSortQueryVariables< TUrlField extends string, TSortField extends string, diff --git a/src/utils/tables.ts b/src/utils/tables.ts deleted file mode 100644 index 1dcb5f5d1..000000000 --- a/src/utils/tables.ts +++ /dev/null @@ -1,10 +0,0 @@ -export function getFooterColSpanWithBulkActions( - arr: any[], - numberOfColumns: number, -): number { - if (arr === undefined || arr.length > 0) { - return numberOfColumns + 1; - } - - return numberOfColumns; -} diff --git a/src/utils/urls.ts b/src/utils/urls.ts index 43d6bd1d2..f80bc11a6 100644 --- a/src/utils/urls.ts +++ b/src/utils/urls.ts @@ -8,7 +8,9 @@ export function stringifyQs(params: unknown, arrayFormat?: string): string { }); } -export function getArrayQueryParam(param: string | string[]): string[] { +export function getArrayQueryParam( + param: string | string[], +): string[] | undefined { if (!param) { return undefined; }