message-timestamp.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <div
  3. v-if="timestampShowFlag"
  4. class="message-timestamp"
  5. >
  6. {{ timestampShowContent }}
  7. </div>
  8. </template>
  9. <script setup lang="ts">
  10. import { toRefs, ref, watch } from '../../../../adapter-vue';
  11. import { calculateTimestamp } from '../../utils/utils';
  12. const props = defineProps({
  13. currTime: {
  14. type: Number,
  15. default: 0,
  16. },
  17. prevTime: {
  18. type: Number,
  19. default: 0,
  20. },
  21. });
  22. const { currTime, prevTime } = toRefs(props);
  23. const timestampShowFlag = ref(false);
  24. const timestampShowContent = ref('');
  25. const handleItemTime = (currTime: number, prevTime: number) => {
  26. timestampShowFlag.value = false;
  27. if (currTime <= 0) {
  28. return '';
  29. } else if (!prevTime || prevTime <= 0) {
  30. timestampShowFlag.value = true;
  31. return calculateTimestamp(currTime * 1000);
  32. } else {
  33. const minDiffToShow = 10 * 60; // 10min 10*60s
  34. const diff = currTime - prevTime; // s
  35. if (diff >= minDiffToShow) {
  36. timestampShowFlag.value = true;
  37. return calculateTimestamp(currTime * 1000);
  38. }
  39. }
  40. return '';
  41. };
  42. watch(
  43. () => [currTime.value, prevTime.value],
  44. (newVal: any, oldVal: any) => {
  45. if (newVal?.toString() === oldVal?.toString()) {
  46. return;
  47. } else {
  48. timestampShowContent.value = handleItemTime(
  49. currTime.value,
  50. prevTime.value,
  51. );
  52. }
  53. },
  54. {
  55. immediate: true,
  56. },
  57. );
  58. </script>
  59. <style lang="scss" scoped>
  60. @import "../../../../assets/styles/common";
  61. .message-timestamp {
  62. margin: 10px auto;
  63. color: #999;
  64. font-size: 12px;
  65. overflow-wrap: anywhere;
  66. display: flex;
  67. align-items: center;
  68. text-align: center;
  69. }
  70. </style>