FileItem.svelte 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <script lang="ts">
  2. import { createEventDispatcher, getContext } from 'svelte';
  3. const i18n = getContext('i18n');
  4. const dispatch = createEventDispatcher();
  5. export let className = 'w-72';
  6. export let colorClassName = 'bg-white dark:bg-gray-800';
  7. export let url: string | null = null;
  8. export let clickHandler: Function | null = null;
  9. export let dismissible = false;
  10. export let status = 'processed';
  11. export let name: string;
  12. export let type: string;
  13. export let size: number;
  14. function formatSize(size) {
  15. if (size == null) return 'Unknown size';
  16. if (typeof size !== 'number' || size < 0) return 'Invalid size';
  17. if (size === 0) return '0 B';
  18. const units = ['B', 'KB', 'MB', 'GB', 'TB'];
  19. let unitIndex = 0;
  20. while (size >= 1024 && unitIndex < units.length - 1) {
  21. size /= 1024;
  22. unitIndex++;
  23. }
  24. return `${size.toFixed(1)} ${units[unitIndex]}`;
  25. }
  26. </script>
  27. <div class="relative group">
  28. <button
  29. class="h-14 {className} flex items-center space-x-3 {colorClassName} rounded-xl border border-gray-100 dark:border-gray-800 text-left"
  30. type="button"
  31. on:click={async () => {
  32. if (clickHandler === null) {
  33. if (url) {
  34. if (type === 'file') {
  35. window.open(`${url}/content`, '_blank').focus();
  36. } else {
  37. window.open(`${url}`, '_blank').focus();
  38. }
  39. }
  40. } else {
  41. clickHandler();
  42. }
  43. }}
  44. >
  45. <div class="p-4 py-[1.1rem] bg-red-400 text-white rounded-l-xl">
  46. {#if status === 'processed'}
  47. <svg
  48. xmlns="http://www.w3.org/2000/svg"
  49. viewBox="0 0 24 24"
  50. fill="currentColor"
  51. class=" size-5"
  52. >
  53. <path
  54. fill-rule="evenodd"
  55. 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"
  56. clip-rule="evenodd"
  57. />
  58. <path
  59. 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"
  60. />
  61. </svg>
  62. {:else}
  63. <svg
  64. class=" size-5 translate-y-[0.5px]"
  65. fill="currentColor"
  66. viewBox="0 0 24 24"
  67. xmlns="http://www.w3.org/2000/svg"
  68. ><style>
  69. .spinner_qM83 {
  70. animation: spinner_8HQG 1.05s infinite;
  71. }
  72. .spinner_oXPr {
  73. animation-delay: 0.1s;
  74. }
  75. .spinner_ZTLf {
  76. animation-delay: 0.2s;
  77. }
  78. @keyframes spinner_8HQG {
  79. 0%,
  80. 57.14% {
  81. animation-timing-function: cubic-bezier(0.33, 0.66, 0.66, 1);
  82. transform: translate(0);
  83. }
  84. 28.57% {
  85. animation-timing-function: cubic-bezier(0.33, 0, 0.66, 0.33);
  86. transform: translateY(-6px);
  87. }
  88. 100% {
  89. transform: translate(0);
  90. }
  91. }
  92. </style><circle class="spinner_qM83" cx="4" cy="12" r="2.5" /><circle
  93. class="spinner_qM83 spinner_oXPr"
  94. cx="12"
  95. cy="12"
  96. r="2.5"
  97. /><circle class="spinner_qM83 spinner_ZTLf" cx="20" cy="12" r="2.5" /></svg
  98. >
  99. {/if}
  100. </div>
  101. <div class="flex flex-col justify-center -space-y-0.5 pl-1.5 pr-4 w-full">
  102. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1 mb-1">
  103. {name}
  104. </div>
  105. <div class=" flex justify-between text-gray-500 text-xs">
  106. {#if type === 'file'}
  107. {$i18n.t('File')}
  108. {:else if type === 'doc'}
  109. {$i18n.t('Document')}
  110. {:else if type === 'collection'}
  111. {$i18n.t('Collection')}
  112. {:else}
  113. <span class=" capitalize">{type}</span>
  114. {/if}
  115. {#if size}
  116. <span class="capitalize">{formatSize(size)}</span>
  117. {/if}
  118. </div>
  119. </div>
  120. </button>
  121. {#if dismissible}
  122. <div class=" absolute -top-1 -right-1">
  123. <button
  124. class=" bg-gray-400 text-white border border-white rounded-full group-hover:visible invisible transition"
  125. type="button"
  126. on:click={() => {
  127. dispatch('dismiss');
  128. }}
  129. >
  130. <svg
  131. xmlns="http://www.w3.org/2000/svg"
  132. viewBox="0 0 20 20"
  133. fill="currentColor"
  134. class="w-4 h-4"
  135. >
  136. <path
  137. 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"
  138. />
  139. </svg>
  140. </button>
  141. </div>
  142. {/if}
  143. </div>