Add generic item types depending on layer

This commit is contained in:
Maximilian Harz 2025-02-04 11:32:07 +01:00
parent 89b780eee2
commit 356238c10b
4 changed files with 57 additions and 36 deletions

View File

@ -2,7 +2,7 @@
/* eslint-disable @typescript-eslint/prefer-optional-chain */
/* eslint-disable @typescript-eslint/no-floating-promises */
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
/* eslint-disable @typescript-eslint/await-thenable */
/* eslint-disable @typescript-eslint/restrict-plus-operands */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */

41
types/Item.d.ts vendored
View File

@ -1,13 +1,13 @@
import type { ItemsApi } from './ItemsApi'
import type { ItemType } from './ItemType'
import type { LayerProps } from './LayerProps'
import type { LayerParameters, LayerProps } from './LayerProps'
import type { Relation } from './Relation'
import type { UserItem } from './UserItem'
import type { Point } from 'geojson'
type TagIds = { tags_id: string }[]
export interface Item {
interface BaseItem<L extends LayerParameters> {
id: string
name: string
text: string
@ -20,7 +20,7 @@ export interface Item {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
api?: ItemsApi<any>
tags?: string[]
layer?: LayerProps
layer?: LayerProps<L>
relations?: Relation[]
parent?: string
subname?: string
@ -32,30 +32,25 @@ export interface Item {
offers?: TagIds
needs?: TagIds
status?: string
color?: string
markerIcon?: string
avatar?: string
new?: boolean
contact?: string
telephone?: string
next_appointment?: string
type?: ItemType
// {
// coordinates: [number, number]
/* constructor(
id: string,
name: string,
text: string,
position: Geometry,
layer?: LayerProps,
api?: ItemsApi<any>,
) {
this.id = id
this.name = name
this.text = text
this.position = position
this.layer = layer
this.api = api
} */
}
export type Item<L extends LayerParameters> = BaseItem<L> &
(L extends { hasAvatar: true }
? {
avatar: string
}
: unknown) &
(L extends { hasColor: true }
? {
color: string
}
: unknown) &
Record<string, unknown>
type foo = Item<{ hasAvatar: false; hasColor: true }>

28
types/ItemType.d.ts vendored
View File

@ -1,11 +1,35 @@
import type { Key } from 'react'
interface TextParameters {
dataField: string
heading: string
hideWhenEmpty: boolean
}
interface GroupSubHeaderParameters {
shareBaseUrl: string
platforms: string[]
}
interface ContactInfoParameters {
heading: string
}
interface StartEndParemeters {
heading: string
}
export type Parameters =
| TextParameters
| GroupSubHeaderParameters
| ContactInfoParameters
| StartEndParemeters
export interface ItemType {
name: string
show_start_end: boolean
show_text: boolean
// eslint-disable-next-line @typescript-eslint/no-explicit-any
profileTemplate: { collection: string | number; id: Key | null | undefined; item: any }[]
profileTemplate: { collection: string | number; id: Key | null | undefined; item: Parameters }[]
offers_and_needs: boolean
icon_as_labels: unknown
relations: boolean

22
types/LayerProps.d.ts vendored
View File

@ -3,9 +3,17 @@ import type { ItemFormPopupProps } from './ItemFormPopupProps'
import type { ItemsApi } from './ItemsApi'
import type { ItemType } from './ItemType'
export interface LayerProps {
interface LayerParameters {
hasAvatar?: boolean
hasColor?: boolean
hasTags?: boolean
hasOffers?: boolean
hasNeeds?: boolean
}
export interface LayerProps<L extends LayerParameters> {
id?: string
data?: Item[]
data?: Item<L>[]
children?: React.ReactNode
name: string
menuIcon: string
@ -15,15 +23,9 @@ export interface LayerProps {
markerShape: string
markerDefaultColor: string
markerDefaultColor2?: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
api?: ItemsApi<any>
itemType: ItemType // How does this relate to Item type defined in Item.d.ts?
api?: ItemsApi<Item<L>>
itemType: ItemType
// TODO Conditionally type items with .avatar etc.?
hasAvatar?: boolean
hasColor?: boolean
hasTags?: boolean
hasOffers?: boolean
hasNeeds?: boolean
onlyOnePerOwner?: boolean
customEditLink?: string
customEditParameter?: string