FileItem.svelte 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <script lang="ts">
  2. import { createEventDispatcher, getContext } from 'svelte';
  3. import { formatFileSize } from '$lib/utils';
  4. import FileItemModal from './FileItemModal.svelte';
  5. import GarbageBin from '../icons/GarbageBin.svelte';
  6. import Spinner from './Spinner.svelte';
  7. import Tooltip from './Tooltip.svelte';
  8. const i18n = getContext('i18n');
  9. const dispatch = createEventDispatcher();
  10. export let className = 'w-60';
  11. export let colorClassName = 'bg-white dark:bg-gray-850 border border-gray-50 dark:border-white/5';
  12. export let url: string | null = null;
  13. export let dismissible = false;
  14. export let loading = false;
  15. export let item = null;
  16. export let edit = false;
  17. export let small = false;
  18. export let name: string;
  19. export let type: string;
  20. export let size: number;
  21. let showModal = false;
  22. </script>
  23. {#if item}
  24. <FileItemModal bind:show={showModal} bind:item {edit} />
  25. {/if}
  26. <button
  27. class="relative group p-1.5 {className} flex items-center gap-1 {colorClassName} {small
  28. ? 'rounded-xl'
  29. : 'rounded-2xl'} text-left"
  30. type="button"
  31. on:click={async () => {
  32. if (item?.file?.data?.content) {
  33. showModal = !showModal;
  34. } else {
  35. if (url) {
  36. if (type === 'file') {
  37. window.open(`${url}/content`, '_blank').focus();
  38. } else {
  39. window.open(`${url}`, '_blank').focus();
  40. }
  41. }
  42. }
  43. dispatch('click');
  44. }}
  45. >
  46. {#if !small}
  47. <div class="p-3 bg-black/20 dark:bg-white/10 text-white rounded-xl">
  48. {#if !loading}
  49. <svg
  50. xmlns="http://www.w3.org/2000/svg"
  51. viewBox="0 0 24 24"
  52. fill="currentColor"
  53. class=" size-5"
  54. >
  55. <path
  56. fill-rule="evenodd"
  57. d="M5.625 1.5c-1.036 0-1.875.84-1.875 1.875v17.25c0 1.035.84 1.875 1.875 1.875h12.75c1.035 0 1.875-.84 1.875-1.875V12.75A3.75 3.75 0 0 0 16.5 9h-1.875a1.875 1.875 0 0 1-1.875-1.875V5.25A3.75 3.75 0 0 0 9 1.5H5.625ZM7.5 15a.75.75 0 0 1 .75-.75h7.5a.75.75 0 0 1 0 1.5h-7.5A.75.75 0 0 1 7.5 15Zm.75 2.25a.75.75 0 0 0 0 1.5H12a.75.75 0 0 0 0-1.5H8.25Z"
  58. clip-rule="evenodd"
  59. />
  60. <path
  61. d="M12.971 1.816A5.23 5.23 0 0 1 14.25 5.25v1.875c0 .207.168.375.375.375H16.5a5.23 5.23 0 0 1 3.434 1.279 9.768 9.768 0 0 0-6.963-6.963Z"
  62. />
  63. </svg>
  64. {:else}
  65. <Spinner />
  66. {/if}
  67. </div>
  68. {/if}
  69. {#if !small}
  70. <div class="flex flex-col justify-center -space-y-0.5 px-2.5 w-full">
  71. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1 mb-1">
  72. {name}
  73. </div>
  74. <div class=" flex justify-between text-gray-500 text-xs line-clamp-1">
  75. {#if type === 'file'}
  76. {$i18n.t('File')}
  77. {:else if type === 'doc'}
  78. {$i18n.t('Document')}
  79. {:else if type === 'collection'}
  80. {$i18n.t('Collection')}
  81. {:else}
  82. <span class=" capitalize line-clamp-1">{type}</span>
  83. {/if}
  84. {#if size}
  85. <span class="capitalize">{formatFileSize(size)}</span>
  86. {/if}
  87. </div>
  88. </div>
  89. {:else}
  90. <Tooltip content={name} className="flex flex-col w-full" placement="top-start">
  91. <div class="flex flex-col justify-center -space-y-0.5 px-2.5 w-full">
  92. <div class=" dark:text-gray-100 text-sm flex justify-between items-center">
  93. {#if loading}
  94. <div class=" shrink-0 mr-2">
  95. <Spinner className="size-4" />
  96. </div>
  97. {/if}
  98. <div class="font-medium line-clamp-1 flex-1">{name}</div>
  99. <div class="text-gray-500 text-xs capitalize shrink-0">{formatFileSize(size)}</div>
  100. </div>
  101. </div>
  102. </Tooltip>
  103. {/if}
  104. {#if dismissible}
  105. <div class=" absolute -top-1 -right-1">
  106. <button
  107. class=" bg-gray-400 text-white border border-white rounded-full group-hover:visible invisible transition"
  108. type="button"
  109. on:click|stopPropagation={() => {
  110. dispatch('dismiss');
  111. }}
  112. >
  113. <svg
  114. xmlns="http://www.w3.org/2000/svg"
  115. viewBox="0 0 20 20"
  116. fill="currentColor"
  117. class="w-4 h-4"
  118. >
  119. <path
  120. d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
  121. />
  122. </svg>
  123. </button>
  124. <!-- <button
  125. class=" p-1 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-full group-hover:visible invisible transition"
  126. type="button"
  127. on:click={() => {
  128. }}
  129. >
  130. <GarbageBin />
  131. </button> -->
  132. </div>
  133. {/if}
  134. </button>