Skip to content

Commit

Permalink
[CP-2995] extract headlineMessage logic into useTemporaryHeadlineMess…
Browse files Browse the repository at this point in the history
…age hook
  • Loading branch information
dkarski committed Oct 4, 2024
1 parent e1f15ac commit 9c0b1a4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Icon, {
import { IconType } from "Core/__deprecated__/renderer/components/core/icon/icon-type"
import { intl } from "Core/__deprecated__/renderer/utils/intl"
import { selectActiveDeviceMenuElements } from "generic-view/store"
import useTemporaryHeadlineMessage from "Core/__deprecated__/renderer/components/rest/header/use-temporary-headline-message"

const messages = defineMessages({
backButtonLabel: { id: "module.generic.viewBackButton" },
Expand Down Expand Up @@ -107,6 +108,9 @@ const Header: FunctionComponent<HeaderProps> = ({
}
}
}, [genericMenu, location, previousViewName])

const temporaryHeadlineMessage = useTemporaryHeadlineMessage(currentLocation)

return (
<HeaderWrapper>
{previousViewName ? (
Expand All @@ -123,7 +127,7 @@ const Header: FunctionComponent<HeaderProps> = ({
) : (
<HeaderText
displayStyle={TextDisplayStyle.Headline4}
message={currentLocation}
message={temporaryHeadlineMessage}
data-testid={"location"}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import { useState, useEffect } from "react"
import { useSelector } from "react-redux"
import { selectActiveApiDeviceId, selectEntitiesData } from "generic-view/store"
import { ReduxRootState } from "Core/__deprecated__/renderer/store"

function useTemporaryHeadlineMessage(
currentLocation: { id: string } | string | undefined
) {
const [headlineMessage, setHeadlineMessage] = useState("")
const deviceId = useSelector(selectActiveApiDeviceId)!
const entitiesData = useSelector((state: ReduxRootState) =>
selectEntitiesData(state, {
entitiesType: "contacts",
deviceId,
})
)

useEffect(() => {
if (typeof currentLocation === "string") {
setHeadlineMessage(currentLocation)
}

if (currentLocation === "Contacts") {
const ContactsLength = entitiesData?.length ?? 0
const message =
ContactsLength < 1 ? "Contacts" : `Contacts (${ContactsLength})`
setHeadlineMessage(message)
}
}, [currentLocation, entitiesData?.length])

return headlineMessage
}

export default useTemporaryHeadlineMessage

0 comments on commit 9c0b1a4

Please sign in to comment.