ChannelItem.svelte 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { onMount, getContext, tick, onDestroy } from 'svelte';
  4. const i18n = getContext('i18n');
  5. import { page } from '$app/stores';
  6. import { mobile, showSidebar, user } from '$lib/stores';
  7. import { updateChannelById } from '$lib/apis/channels';
  8. import Cog6 from '$lib/components/icons/Cog6.svelte';
  9. import ChannelModal from './ChannelModal.svelte';
  10. import Lock from '$lib/components/icons/Lock.svelte';
  11. import Hashtag from '$lib/components/icons/Hashtag.svelte';
  12. export let onUpdate: Function = () => {};
  13. export let className = '';
  14. export let channel;
  15. let showEditChannelModal = false;
  16. let itemElement;
  17. </script>
  18. <ChannelModal
  19. bind:show={showEditChannelModal}
  20. {channel}
  21. edit={true}
  22. {onUpdate}
  23. onSubmit={async ({ name, access_control }) => {
  24. const res = await updateChannelById(localStorage.token, channel.id, {
  25. name,
  26. access_control
  27. }).catch((error) => {
  28. toast.error(error.message);
  29. });
  30. if (res) {
  31. toast.success($i18n.t('Channel updated successfully'));
  32. }
  33. onUpdate();
  34. }}
  35. />
  36. <div
  37. bind:this={itemElement}
  38. class=" w-full {className} rounded-lg flex relative group hover:bg-gray-100 dark:hover:bg-gray-900 {$page
  39. .url.pathname === `/channels/${channel.id}`
  40. ? 'bg-gray-100 dark:bg-gray-900'
  41. : ''} px-2.5 py-1"
  42. >
  43. <a
  44. class=" w-full flex justify-between"
  45. href="/channels/{channel.id}"
  46. on:click={() => {
  47. console.log(channel);
  48. if ($mobile) {
  49. showSidebar.set(false);
  50. }
  51. }}
  52. draggable="false"
  53. >
  54. <div class="flex items-center gap-1 shrink-0">
  55. <div class=" size-4 justify-center flex items-center">
  56. {#if channel?.access_control === null}
  57. <Hashtag className="size-3" strokeWidth="2.5" />
  58. {:else}
  59. <Lock className="size-[15px]" strokeWidth="2" />
  60. {/if}
  61. </div>
  62. <div class=" text-left self-center overflow-hidden w-full line-clamp-1 flex-1">
  63. {channel.name}
  64. </div>
  65. </div>
  66. </a>
  67. {#if $user?.role === 'admin'}
  68. <button
  69. class="absolute z-10 right-2 invisible group-hover:visible self-center flex items-center dark:text-gray-300"
  70. on:click={(e) => {
  71. e.stopPropagation();
  72. showEditChannelModal = true;
  73. }}
  74. >
  75. <button class="p-0.5 dark:hover:bg-gray-850 rounded-lg touch-auto" on:click={(e) => {}}>
  76. <Cog6 className="size-3.5" />
  77. </button>
  78. </button>
  79. {/if}
  80. </div>