ImportModal.svelte 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { getContext, onMount } from 'svelte';
  4. const i18n = getContext('i18n');
  5. import Modal from '$lib/components/common/Modal.svelte';
  6. import { loadFunctionByUrl } from '$lib/apis/functions';
  7. import { extractFrontmatter } from '$lib/utils';
  8. export let show = false;
  9. export let onImport = (e) => {};
  10. export let onClose = () => {};
  11. let loading = false;
  12. let url = '';
  13. const submitHandler = async () => {
  14. loading = true;
  15. if (!url) {
  16. toast.error($i18n.t('Please enter a valid URL'));
  17. loading = false;
  18. return;
  19. }
  20. const res = await loadFunctionByUrl(localStorage.token, url).catch((err) => {
  21. toast.error(`${err}`);
  22. return null;
  23. });
  24. if (res) {
  25. toast.success($i18n.t('Function loaded successfully'));
  26. let func = res;
  27. func.id = func.id || func.name.replace(/\s+/g, '_').toLowerCase();
  28. const frontmatter = extractFrontmatter(res.content); // Ensure frontmatter is extracted
  29. if (frontmatter?.title) {
  30. func.name = frontmatter.title;
  31. }
  32. if (frontmatter?.description) {
  33. func.meta = {
  34. ...func.meta,
  35. description: frontmatter.description
  36. };
  37. }
  38. onImport(func);
  39. show = false;
  40. }
  41. };
  42. </script>
  43. <Modal size="sm" bind:show>
  44. <div>
  45. <div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-2">
  46. <div class=" text-lg font-medium self-center">{$i18n.t('Import')}</div>
  47. <button
  48. class="self-center"
  49. on:click={() => {
  50. show = false;
  51. }}
  52. >
  53. <svg
  54. xmlns="http://www.w3.org/2000/svg"
  55. viewBox="0 0 20 20"
  56. fill="currentColor"
  57. class="w-5 h-5"
  58. >
  59. <path
  60. 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"
  61. />
  62. </svg>
  63. </button>
  64. </div>
  65. <div class="flex flex-col md:flex-row w-full px-4 pb-3 md:space-x-4 dark:text-gray-200">
  66. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  67. <form
  68. class="flex flex-col w-full"
  69. on:submit|preventDefault={() => {
  70. submitHandler();
  71. }}
  72. >
  73. <div class="px-1">
  74. <div class="flex flex-col w-full">
  75. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('URL')}</div>
  76. <div class="flex-1">
  77. <input
  78. class="w-full text-sm bg-transparent disabled:text-gray-500 dark:disabled:text-gray-500 outline-hidden"
  79. type="url"
  80. bind:value={url}
  81. placeholder={$i18n.t('Enter the URL of the function to import')}
  82. required
  83. />
  84. </div>
  85. </div>
  86. </div>
  87. <div class="flex justify-end pt-3 text-sm font-medium">
  88. <button
  89. class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center {loading
  90. ? ' cursor-not-allowed'
  91. : ''}"
  92. type="submit"
  93. disabled={loading}
  94. >
  95. {$i18n.t('Import')}
  96. {#if loading}
  97. <div class="ml-2 self-center">
  98. <svg
  99. class=" w-4 h-4"
  100. viewBox="0 0 24 24"
  101. fill="currentColor"
  102. xmlns="http://www.w3.org/2000/svg"
  103. ><style>
  104. .spinner_ajPY {
  105. transform-origin: center;
  106. animation: spinner_AtaB 0.75s infinite linear;
  107. }
  108. @keyframes spinner_AtaB {
  109. 100% {
  110. transform: rotate(360deg);
  111. }
  112. }
  113. </style><path
  114. d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
  115. opacity=".25"
  116. /><path
  117. d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"
  118. class="spinner_ajPY"
  119. /></svg
  120. >
  121. </div>
  122. {/if}
  123. </button>
  124. </div>
  125. </form>
  126. </div>
  127. </div>
  128. </div>
  129. </Modal>