ChatList.svelte 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <script lang="ts">
  2. import { getContext, onMount } from 'svelte';
  3. const i18n = getContext('i18n');
  4. import dayjs from 'dayjs';
  5. import localizedFormat from 'dayjs/plugin/localizedFormat';
  6. import { getTimeRange } from '$lib/utils';
  7. dayjs.extend(localizedFormat);
  8. export let chats = [];
  9. let chatList = null;
  10. const init = async () => {
  11. if (chats.length === 0) {
  12. chatList = [];
  13. } else {
  14. chatList = chats.map((chat) => ({
  15. ...chat,
  16. time_range: getTimeRange(chat.updated_at)
  17. }));
  18. }
  19. };
  20. $: if (chats) {
  21. init();
  22. }
  23. </script>
  24. {#if chatList}
  25. <div class="text-left text-sm w-full mb-3">
  26. {#if chatList.length === 0}
  27. <div
  28. class="text-xs text-gray-500 dark:text-gray-400 text-center px-5 min-h-20 w-full h-full flex justify-center items-center"
  29. >
  30. {$i18n.t('No chats found')}
  31. </div>
  32. {/if}
  33. {#each chatList as chat, idx (chat.id)}
  34. {#if (idx === 0 || (idx > 0 && chat.time_range !== chatList[idx - 1].time_range)) && chat?.time_range}
  35. <div
  36. class="w-full text-xs text-gray-500 dark:text-gray-500 font-medium {idx === 0
  37. ? ''
  38. : 'pt-5'} pb-2 px-2"
  39. >
  40. {$i18n.t(chat.time_range)}
  41. <!-- localisation keys for time_range to be recognized from the i18next parser (so they don't get automatically removed):
  42. {$i18n.t('Today')}
  43. {$i18n.t('Yesterday')}
  44. {$i18n.t('Previous 7 days')}
  45. {$i18n.t('Previous 30 days')}
  46. {$i18n.t('January')}
  47. {$i18n.t('February')}
  48. {$i18n.t('March')}
  49. {$i18n.t('April')}
  50. {$i18n.t('May')}
  51. {$i18n.t('June')}
  52. {$i18n.t('July')}
  53. {$i18n.t('August')}
  54. {$i18n.t('September')}
  55. {$i18n.t('October')}
  56. {$i18n.t('November')}
  57. {$i18n.t('December')}
  58. -->
  59. </div>
  60. {/if}
  61. <a
  62. class=" w-full flex justify-between items-center rounded-lg text-sm py-2 px-3 hover:bg-gray-50 dark:hover:bg-gray-850"
  63. draggable="false"
  64. href={`/c/${chat.id}`}
  65. on:click={() => (show = false)}
  66. >
  67. <div class="text-ellipsis line-clamp-1 w-full sm:basis-3/5">
  68. {chat?.title}
  69. </div>
  70. <div class="hidden sm:flex sm:basis-2/5 items-center justify-end">
  71. <div class=" text-gray-500 dark:text-gray-400 text-xs">
  72. {dayjs(chat?.updated_at * 1000).calendar()}
  73. </div>
  74. </div>
  75. </a>
  76. {/each}
  77. <!-- {#if !allChatsLoaded && loadHandler}
  78. <Loader
  79. on:visible={(e) => {
  80. if (!chatListLoading) {
  81. loadHandler();
  82. }
  83. }}
  84. >
  85. <div class="w-full flex justify-center py-1 text-xs animate-pulse items-center gap-2">
  86. <Spinner className=" size-4" />
  87. <div class=" ">Loading...</div>
  88. </div>
  89. </Loader>
  90. {/if} -->
  91. </div>
  92. {/if}