Skip to content

Commit 1c57ed9

Browse files
committed
Typescript 5 updates
1 parent b5f2001 commit 1c57ed9

12 files changed

+57
-50
lines changed

lib/src/core/FireCMS.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export function FireCMS<UserType extends User>(props: FireCMSProps<UserType>) {
7373
const usedBasePath = basePath ?? "/";
7474
const usedBasedCollectionPath = baseCollectionPath ?? DEFAULT_COLLECTION_PATH;
7575

76+
// @ts-ignore
7677
const dateUtilsLocale = locale ? locales[locale] : undefined;
7778

7879
const navigation = useBuildNavigationContext({

lib/src/core/util/icons.tsx

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import { CMSView, EntityCollection } from "../../types";
44
import { SvgIconTypeMap } from "@mui/material";
55
import { hashString } from "./hash";
66

7-
export function getIcon(iconKey: string) {
8-
return mui[iconKey];
7+
export function getIcon(iconKey?: keyof typeof mui) {
8+
if (!iconKey) return undefined;
9+
return iconKey in mui ? mui[iconKey] : undefined;
910
}
1011

1112
export function getIconForView(collectionOrView: EntityCollection | CMSView): React.ComponentType<SvgIconTypeMap["props"]> {
12-
if (collectionOrView?.icon && getIcon(collectionOrView.icon))
13-
return getIcon(collectionOrView.icon);
13+
const icon = getIcon(collectionOrView.icon);
14+
if (collectionOrView?.icon && icon)
15+
return icon;
1416
const iconsCount = collectionIconKeys.length;
1517
return mui[collectionIconKeys[hashString(collectionOrView.path) % iconsCount]];
1618
}
1719

18-
export const collectionIconKeys = ["AcUnit", "Adjust", "AlignHorizontalCenter", "Album", "AllInclusive", "AllOut", "Animation", "Assistant", "Attractions", "Audiotrack", "AutoAwesome", "AutoAwesomeMosaic", "BeachAccess", "Bolt", "Brightness1", "BreakfastDining", "BrokenImage", "Brightness5", "Cable", "CalendarViewMonth", "CatchingPokemon", "Casino", "Category", "Cloud", "ColorLens", "CreditCard", "Coronavirus", "Earbuds", "EggAlt", "FiberSmartRecord", "Flag", "Healing", "HeatPump", "Hive", "Hub", "LocalFireDepartment", "LocalPizza", "Memory", "Outlet", "Pages", "PanoramaPhotosphere", "SignalCellular0Bar", "SportsBaseball", "Storm", "Stairs"];
20+
export const collectionIconKeys:(keyof typeof mui)[] = ["AcUnit", "Adjust", "AlignHorizontalCenter", "Album", "AllInclusive", "AllOut", "Animation", "Assistant", "Attractions", "Audiotrack", "AutoAwesome", "AutoAwesomeMosaic", "BeachAccess", "Bolt", "Brightness1", "BreakfastDining", "BrokenImage", "Brightness5", "Cable", "CalendarViewMonth", "CatchingPokemon", "Casino", "Category", "Cloud", "ColorLens", "CreditCard", "Coronavirus", "Earbuds", "EggAlt", "FiberSmartRecord", "Flag", "Healing", "HeatPump", "Hive", "Hub", "LocalFireDepartment", "LocalPizza", "Memory", "Outlet", "Pages", "PanoramaPhotosphere", "SignalCellular0Bar", "SportsBaseball", "Storm", "Stairs"];

lib/src/core/util/objects.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ export const pick: <T>(obj: T, ...args: any[]) => T = (obj: any, ...args: any[])
99
});
1010

1111
export function isObject(item: any) {
12-
return (item && typeof item === "object" && !Array.isArray(item));
12+
return item && typeof item === "object" && !Array.isArray(item);
1313
}
1414

15-
export function mergeDeep<T extends {}>(target: T, source: any): T {
15+
export function mergeDeep<T extends object>(target: T, source: any): T {
1616
const targetIsObject = isObject(target);
17-
const output: T = targetIsObject ? Object.assign({}, target) : target;
17+
const output: T = targetIsObject ? { ...target } : target;
1818
if (targetIsObject && isObject(source)) {
1919
Object.keys(source).forEach(key => {
2020
if (isObject(source[key])) {
2121
if (!(key in target))
2222
Object.assign(output, { [key]: source[key] });
2323
else
24-
(output)[key] = mergeDeep((target)[key], source[key]);
24+
(output as any)[key] = mergeDeep((target as any)[key], source[key]);
2525
} else {
2626
Object.assign(output, { [key]: source[key] });
2727
}
@@ -34,11 +34,11 @@ export function getValueInPath(o: object | undefined, path: string): any {
3434
if (!o) return undefined;
3535
if (typeof o === "object") {
3636
if (path in o) {
37-
return (o)[path];
37+
return (o as any)[path];
3838
}
3939
if (path.includes(".")) {
4040
const pathSegments = path.split(".");
41-
return getValueInPath((o)[pathSegments[0]], pathSegments.slice(1).join("."))
41+
return getValueInPath((o as any)[pathSegments[0]], pathSegments.slice(1).join("."))
4242
}
4343
}
4444
return undefined;
@@ -49,10 +49,10 @@ export function removeInPath(o: object, path: string): object | undefined {
4949
const parts = path.split(".");
5050
const last = parts.pop();
5151
for (const part of parts) {
52-
currentObject = currentObject[part]
52+
currentObject = (currentObject as any)[part]
5353
}
5454
if (last)
55-
delete currentObject[last];
55+
delete (currentObject as any)[last];
5656
return currentObject;
5757
}
5858

@@ -98,7 +98,7 @@ export function removeUndefined(value: any): any {
9898
if (!isEmptyObject(value)) {
9999
const childRes = removeUndefined(value[key]);
100100
if (childRes !== undefined && !isEmptyObject(childRes))
101-
res[key] = childRes;
101+
(res as any)[key] = childRes;
102102
}
103103
});
104104
return res;

lib/src/core/util/useStorageUploadController.tsx

+27-23
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,27 @@ export interface StorageFieldItem {
3232
}
3333

3434
export function useStorageUploadController<M extends object>({
35-
entityId,
36-
entityValues,
37-
path,
38-
value,
39-
property,
40-
propertyKey,
41-
storageSource,
42-
disabled,
43-
onChange
44-
}:
45-
{
46-
entityId: string,
47-
entityValues: EntityValues<M>,
48-
value: string | string[] | null;
49-
path: string,
50-
propertyKey: string,
51-
property: ResolvedStringProperty | ResolvedArrayProperty<string[]>,
52-
storageSource: StorageSource,
53-
disabled: boolean,
54-
onChange: (value: string | string[] | null) => void
55-
}) {
35+
entityId,
36+
entityValues,
37+
path,
38+
value,
39+
property,
40+
propertyKey,
41+
storageSource,
42+
disabled,
43+
onChange
44+
}:
45+
{
46+
entityId: string,
47+
entityValues: EntityValues<M>,
48+
value: string | string[] | null;
49+
path: string,
50+
propertyKey: string,
51+
property: ResolvedStringProperty | ResolvedArrayProperty<string[]>,
52+
storageSource: StorageSource,
53+
disabled: boolean,
54+
onChange: (value: string | string[] | null) => void
55+
}) {
5656

5757
const storage: StorageConfig | undefined = property.dataType === "string"
5858
? property.storage
@@ -208,7 +208,7 @@ function getRandomId() {
208208
return Math.floor(Math.random() * Math.floor(Number.MAX_SAFE_INTEGER));
209209
}
210210

211-
const supportedTypes = {
211+
const supportedTypes: Record<string, string> = {
212212
"image/jpeg": "JPEG",
213213
"image/png": "PNG",
214214
"image/webp": "WEBP"
@@ -221,11 +221,15 @@ const resizeAndCompressImage = (file: File, compression: ImageCompression) => ne
221221
const inputQuality = compression.quality === undefined ? defaultQuality : compression.quality;
222222
const quality = inputQuality >= 0 ? inputQuality <= 100 ? inputQuality : 100 : 100;
223223

224+
const format = compressionFormat(file);
225+
if (!format) {
226+
throw Error("resizeAndCompressImage: Unsupported image format");
227+
}
224228
Resizer.imageFileResizer(
225229
file,
226230
compression.maxWidth || Number.MAX_VALUE,
227231
compression.maxHeight || Number.MAX_VALUE,
228-
compressionFormat(file),
232+
format,
229233
quality,
230234
0,
231235
(file: string | Blob | File | ProgressEvent<FileReader>) => resolve(file as File),

lib/src/firebase_app/hooks/useFirestoreDataSource.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -489,12 +489,13 @@ export function useFirestoreDataSource({
489489
* @param data
490490
* @category Firestore
491491
*/
492+
export function firestoreToCMSModel(data: any): any
492493
export function firestoreToCMSModel(data: any): any {
493494
if (data === null || data === undefined) return null;
494495
if (serverTimestamp().isEqual(data)) {
495496
return null;
496497
}
497-
if (data instanceof Timestamp || typeof data.toDate === "function") {
498+
if (data instanceof Timestamp || (typeof data.toDate === "function" && data.toDate() instanceof Date)) {
498499
return data.toDate();
499500
}
500501
if (data instanceof Date) {
@@ -510,15 +511,14 @@ export function firestoreToCMSModel(data: any): any {
510511
return data.map(firestoreToCMSModel);
511512
}
512513
if (typeof data === "object") {
513-
const result = {}
514+
const result: Record<string, any> = {};
514515
for (const key of Object.keys(data)) {
515516
result[key] = firestoreToCMSModel(data[key]);
516517
}
517518
return result;
518519
}
519520
return data;
520521
}
521-
522522
export function cmsToFirestoreModel(data: any, firestore: Firestore): any {
523523
if (Array.isArray(data)) {
524524
return data.map(v => cmsToFirestoreModel(v, firestore));

lib/src/preview/components/DatePreview.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function DatePreview({
1414
}: { date: Date }): React.ReactElement {
1515

1616
const appConfig: FireCMSContext<any> | undefined = useFireCMSContext();
17+
// @ts-ignore
1718
const dateUtilsLocale = appConfig?.locale ? locales[appConfig?.locale] : undefined;
1819
const dateFormat: string = appConfig?.dateTimeFormat ?? defaultDateFormat;
1920
const formattedDate = date ? format(date, dateFormat, { locale: dateUtilsLocale }) : "";

lib/src/preview/components/ReferencePreview.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ function ReferencePreviewInternal<M extends Record<string, any>>({
9494
});
9595

9696
if (entity) {
97-
referencesCache[reference.pathWithId] = entity;
97+
referencesCache.set(reference.pathWithId, entity);
9898
}
9999

100-
const usedEntity = entity ?? referencesCache[reference.pathWithId];
100+
const usedEntity = entity ?? referencesCache.get(reference.pathWithId);
101101

102102
const resolvedCollection = useMemo(() => resolveCollection({
103103
collection,

lib/src/types/collections.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { FireCMSContext } from "./firecms_context";
55
import { EntityCallbacks } from "./entity_callbacks";
66
import { Permissions, PermissionsBuilder } from "./permissions";
77
import { EnumValues, PropertiesOrBuilders } from "./properties";
8+
import * as mui from "@mui/icons-material";
89

910
/**
1011
* This interface represents a view that includes a collection of entities.
@@ -55,7 +56,7 @@ export interface EntityCollection<M extends Record<string, any> = any,
5556
* https://mui.com/material-ui/material-icons/
5657
* e.g. 'AccountTree' or 'Person'
5758
*/
58-
icon?: string;
59+
icon?: keyof typeof mui;
5960

6061
/**
6162
* Optional field used to group top level navigation entries under a~

lib/src/types/navigation.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as mui from "@mui/icons-material";
12
import { EntityCollection } from "./collections";
23

34
/**
@@ -50,8 +51,8 @@ export type NavigationContext = {
5051
* among the
5152
*/
5253
getCollection: <EC extends EntityCollection = EntityCollection<any>>(pathOrAlias: string,
53-
entityId?: string,
54-
includeUserOverride?: boolean) => EC | undefined;
54+
entityId?: string,
55+
includeUserOverride?: boolean) => EC | undefined;
5556

5657
/**
5758
* Default path under the navigation routes of the CMS will be created
@@ -78,7 +79,7 @@ export type NavigationContext = {
7879
*/
7980
buildCMSUrlPath: (path: string) => string;
8081

81-
buildUrlEditCollectionPath: (props: { path: string}) => string;
82+
buildUrlEditCollectionPath: (props: { path: string }) => string;
8283

8384
/**
8485
* Base url path for the home screen
@@ -146,7 +147,7 @@ export interface CMSView {
146147
* https://mui.com/material-ui/material-icons/
147148
* e.g. 'AccountTree' or 'Person'
148149
*/
149-
icon?:string;
150+
icon?: keyof typeof mui;
150151

151152
/**
152153
* Should this view be hidden from the main navigation panel.

lib/tsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"noImplicitThis": true,
1919
"noImplicitAny": true,
2020
"strictNullChecks": true,
21-
"suppressImplicitAnyIndexErrors": true,
2221
"allowSyntheticDefaultImports": true,
2322
"allowJs": true,
2423
"skipLibCheck": true,

tsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"noImplicitThis": true,
1919
"noImplicitAny": true,
2020
"strictNullChecks": true,
21-
"suppressImplicitAnyIndexErrors": true,
2221
"allowSyntheticDefaultImports": true,
2322
"allowJs": true,
2423
"skipLibCheck": true,

website/tsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"noImplicitReturns": true,
2828
"noImplicitThis": true,
2929
"strictNullChecks": true,
30-
"suppressImplicitAnyIndexErrors": true,
3130
"jsxImportSource": "@emotion/react"
3231
},
3332
"include": [

0 commit comments

Comments
 (0)