Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 14 additions & 26 deletions lib/src/Components/Item/PopupView.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
/* eslint-disable @typescript-eslint/restrict-plus-operands */
import { useContext, useMemo, useState } from 'react'
import { useContext, useEffect, useMemo } from 'react'
import { Marker, Tooltip } from 'react-leaflet'

import { useAppState } from '#components/AppShell/hooks/useAppState'
Expand All @@ -13,18 +13,19 @@ import {
import { useItems, useAllItemsLoaded } from '#components/Map/hooks/useItems'
import { useAddMarker, useAddPopup, useLeafletRefs } from '#components/Map/hooks/useLeafletRefs'
import { useSetMarkerClicked, useSelectPosition } from '#components/Map/hooks/useSelectPosition'
import { useGetItemTags, useAllTagsLoaded, useTags } from '#components/Map/hooks/useTags'
import {
useGetItemTags,
useAllTagsLoaded,
useProcessItemsTags,
} from '#components/Map/hooks/useTags'
import LayerContext from '#components/Map/LayerContext'
import { ItemViewPopup } from '#components/Map/Subcomponents/ItemViewPopup'
import { encodeTag } from '#utils/FormatTags'
import { hashTagRegex } from '#utils/HashTagRegex'
import MarkerIconFactory from '#utils/MarkerIconFactory'
import { randomColor } from '#utils/RandomColor'

import TemplateItemContext from './TemplateItemContext'

import type { Item } from '#types/Item'
import type { Tag } from '#types/Tag'
import type { Popup } from 'leaflet'

/**
Expand All @@ -51,9 +52,14 @@ export const PopupView = ({ children }: { children?: React.ReactNode }) => {
const setMarkerClicked = useSetMarkerClicked()
const selectPosition = useSelectPosition()

const tags = useTags()
const [newTagsToAdd, setNewTagsToAdd] = useState<Tag[]>([])
const [tagsReady, setTagsReady] = useState<boolean>(false)
const processItemsTags = useProcessItemsTags()

// Process items to create missing tags when items are loaded
useEffect(() => {
if (allTagsLoaded && allItemsLoaded && items.length > 0) {
processItemsTags(items)
}
}, [allTagsLoaded, allItemsLoaded, items, processItemsTags])

const isLayerVisible = useIsLayerVisible()

Expand Down Expand Up @@ -105,24 +111,6 @@ export const PopupView = ({ children }: { children?: React.ReactNode }) => {
})
}

if (allTagsLoaded && allItemsLoaded) {
item.text?.match(hashTagRegex)?.map((tag) => {
if (
!tags.find((t) => t.name.toLocaleLowerCase() === tag.slice(1).toLocaleLowerCase()) &&
!newTagsToAdd.find((t) => t.name.toLocaleLowerCase() === tag.slice(1).toLocaleLowerCase())
) {
const newTag = {
id: crypto.randomUUID(),
name: tag.slice(1),
color: randomColor(),
}
setNewTagsToAdd((current) => [...current, newTag])
}
return null
})
!tagsReady && setTagsReady(true)
}

const itemTags = getItemTags(item)

const latitude = item.position.coordinates[1]
Expand Down
Loading