ChannelItem.svelte 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. export let onUpdate: Function = () => {};
  11. export let className = '';
  12. export let channel;
  13. let showEditChannelModal = false;
  14. let itemElement;
  15. </script>
  16. <ChannelModal
  17. bind:show={showEditChannelModal}
  18. {channel}
  19. edit={true}
  20. {onUpdate}
  21. onSubmit={async ({ name, access_control }) => {
  22. const res = await updateChannelById(localStorage.token, channel.id, {
  23. name,
  24. access_control
  25. }).catch((error) => {
  26. toast.error(error.message);
  27. });
  28. if (res) {
  29. toast.success('Channel updated successfully');
  30. }
  31. onUpdate();
  32. }}
  33. />
  34. <div
  35. bind:this={itemElement}
  36. class=" w-full {className} rounded-lg flex relative group hover:bg-gray-100 dark:hover:bg-gray-900 {$page
  37. .url.pathname === `/channels/${channel.id}`
  38. ? 'bg-gray-100 dark:bg-gray-900'
  39. : ''} px-2.5 py-1"
  40. >
  41. <a
  42. class=" w-full flex justify-between"
  43. href="/channels/{channel.id}"
  44. on:click={() => {
  45. if ($mobile) {
  46. showSidebar.set(false);
  47. }
  48. }}
  49. draggable="false"
  50. >
  51. <div class="flex items-center gap-1">
  52. <svg
  53. xmlns="http://www.w3.org/2000/svg"
  54. viewBox="0 0 16 16"
  55. fill="currentColor"
  56. class="size-5"
  57. >
  58. <path
  59. fill-rule="evenodd"
  60. d="M7.487 2.89a.75.75 0 1 0-1.474-.28l-.455 2.388H3.61a.75.75 0 0 0 0 1.5h1.663l-.571 2.998H2.75a.75.75 0 0 0 0 1.5h1.666l-.403 2.114a.75.75 0 0 0 1.474.28l.456-2.394h2.973l-.403 2.114a.75.75 0 0 0 1.474.28l.456-2.394h1.947a.75.75 0 0 0 0-1.5h-1.661l.57-2.998h1.95a.75.75 0 0 0 0-1.5h-1.664l.402-2.108a.75.75 0 0 0-1.474-.28l-.455 2.388H7.085l.402-2.108ZM6.8 6.498l-.571 2.998h2.973l.57-2.998H6.8Z"
  61. clip-rule="evenodd"
  62. />
  63. </svg>
  64. <div class=" text-left self-center overflow-hidden w-full line-clamp-1">
  65. {channel.name}
  66. </div>
  67. </div>
  68. </a>
  69. {#if $user?.role === 'admin'}
  70. <button
  71. class="absolute z-10 right-2 invisible group-hover:visible self-center flex items-center dark:text-gray-300"
  72. on:pointerup={(e) => {
  73. e.stopPropagation();
  74. showEditChannelModal = true;
  75. }}
  76. >
  77. <button class="p-0.5 dark:hover:bg-gray-850 rounded-lg touch-auto" on:click={(e) => {}}>
  78. <Cog6 className="size-3.5" />
  79. </button>
  80. </button>
  81. {/if}
  82. </div>