|
| 1 | +<!-- |
| 2 | +// Copyright © 2025 Hardcore Engineering Inc. |
| 3 | +// |
| 4 | +// Licensed under the Eclipse Public License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. You may |
| 6 | +// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +--> |
| 15 | +<script lang="ts"> |
| 16 | + import { onMount } from 'svelte' |
| 17 | + import { getName, Person, getCurrentEmployee } from '@hcengineering/contact' |
| 18 | + import { personByIdStore } from '@hcengineering/contact-resources' |
| 19 | + import { IdMap, notEmpty } from '@hcengineering/core' |
| 20 | + import { getClient } from '@hcengineering/presentation' |
| 21 | + import { Label } from '@hcengineering/ui' |
| 22 | + import { presenceByObjectId } from '@hcengineering/presence-resources' |
| 23 | + import { CardID } from '@hcengineering/communication-types' |
| 24 | +
|
| 25 | + import uiNext from '../plugin' |
| 26 | + import { type PresenceTyping } from '../types' |
| 27 | +
|
| 28 | + export let cardId: CardID |
| 29 | + const typingDelay = 2000 |
| 30 | + const maxTypingPersons = 3 |
| 31 | + const me = getCurrentEmployee() |
| 32 | + const hierarchy = getClient().getHierarchy() |
| 33 | +
|
| 34 | + let typingPersonsLabel: string = '' |
| 35 | + let typingPersonsCount = 0 |
| 36 | + let moreCount: number = 0 |
| 37 | +
|
| 38 | + let typing: PresenceTyping[] = [] |
| 39 | + $: presence = $presenceByObjectId.get(cardId) ?? [] |
| 40 | + $: typing = presence.map((p) => p.presence.typing).filter(notEmpty) |
| 41 | +
|
| 42 | + $: updateTypingPersons($personByIdStore, typing) |
| 43 | +
|
| 44 | + function updateTypingPersons (personById: IdMap<Person>, typingInfo: PresenceTyping[]): void { |
| 45 | + const now = Date.now() |
| 46 | + const personIds = new Set( |
| 47 | + typingInfo.filter((info) => info.person !== me && now - info.lastTyping < typingDelay).map((info) => info.person) |
| 48 | + ) |
| 49 | + const names = Array.from(personIds) |
| 50 | + .map((personId) => personById.get(personId)) |
| 51 | + .filter((person): person is Person => person !== undefined) |
| 52 | + .map((person) => getName(hierarchy, person)) |
| 53 | + .sort((name1, name2) => name1.localeCompare(name2)) |
| 54 | +
|
| 55 | + typingPersonsCount = names.length |
| 56 | + typingPersonsLabel = names.slice(0, maxTypingPersons).join(', ') |
| 57 | + moreCount = Math.max(names.length - maxTypingPersons, 0) |
| 58 | + } |
| 59 | +
|
| 60 | + onMount(() => { |
| 61 | + const interval = setInterval(() => { |
| 62 | + updateTypingPersons($personByIdStore, typing) |
| 63 | + }, typingDelay) |
| 64 | + return () => { |
| 65 | + clearInterval(interval) |
| 66 | + } |
| 67 | + }) |
| 68 | +</script> |
| 69 | + |
| 70 | +<span class="root h-4 mt-1 mb-1 ml-0-5 overflow-label"> |
| 71 | + {#if typingPersonsLabel !== ''} |
| 72 | + <span class="fs-bold"> |
| 73 | + {typingPersonsLabel} |
| 74 | + </span> |
| 75 | + {#if moreCount > 0} |
| 76 | + <span class="ml-1"><Label label={uiNext.string.AndMore} params={{ count: moreCount }} /></span> |
| 77 | + {/if} |
| 78 | + <span class="ml-1"><Label label={uiNext.string.IsTyping} params={{ count: typingPersonsCount }} /></span> |
| 79 | + {/if} |
| 80 | +</span> |
| 81 | + |
| 82 | +<style> |
| 83 | + .root { |
| 84 | + display: flex; |
| 85 | + align-items: center; |
| 86 | + font-size: 0.75rem; |
| 87 | + } |
| 88 | +</style> |
0 commit comments