ChannelItem.svelte 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. onSubmit={async ({ name, access_control }) => {
  21. const res = await updateChannelById(localStorage.token, channel.id, {
  22. name,
  23. access_control
  24. }).catch((error) => {
  25. toast.error(error.message);
  26. });
  27. if (res) {
  28. toast.success('Channel updated successfully');
  29. }
  30. onUpdate();
  31. }}
  32. />
  33. <div
  34. bind:this={itemElement}
  35. class=" w-full {className} rounded-lg flex relative group hover:bg-gray-100 dark:hover:bg-gray-900 {$page
  36. .url.pathname === `/channels/${channel.id}`
  37. ? 'bg-gray-100 dark:bg-gray-900'
  38. : ''} px-2.5 py-1"
  39. >
  40. <a
  41. class=" w-full flex justify-between"
  42. href="/channels/{channel.id}"
  43. on:click={() => {
  44. if ($mobile) {
  45. showSidebar.set(false);
  46. }
  47. }}
  48. draggable="false"
  49. >
  50. <div class="flex items-center gap-1">
  51. <svg
  52. xmlns="http://www.w3.org/2000/svg"
  53. viewBox="0 0 16 16"
  54. fill="currentColor"
  55. class="size-5"
  56. >
  57. <path
  58. fill-rule="evenodd"
  59. 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"
  60. clip-rule="evenodd"
  61. />
  62. </svg>
  63. <div class=" text-left self-center overflow-hidden w-full line-clamp-1">
  64. {channel.name}
  65. </div>
  66. </div>
  67. </a>
  68. {#if $user?.role === 'admin'}
  69. <button
  70. class="absolute z-10 right-2 invisible group-hover:visible self-center flex items-center dark:text-gray-300"
  71. on:pointerup={(e) => {
  72. e.stopPropagation();
  73. showEditChannelModal = true;
  74. }}
  75. >
  76. <button class="p-0.5 dark:hover:bg-gray-850 rounded-lg touch-auto" on:click={(e) => {}}>
  77. <Cog6 className="size-3.5" />
  78. </button>
  79. </button>
  80. {/if}
  81. </div>