1
0

ImportModal.svelte 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.description = frontmatter.description;
  34. }
  35. onImport(func);
  36. show = false;
  37. }
  38. };
  39. </script>
  40. <Modal size="sm" bind:show>
  41. <div>
  42. <div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-2">
  43. <div class=" text-lg font-medium self-center">{$i18n.t('Import')}</div>
  44. <button
  45. class="self-center"
  46. on:click={() => {
  47. show = false;
  48. }}
  49. >
  50. <svg
  51. xmlns="http://www.w3.org/2000/svg"
  52. viewBox="0 0 20 20"
  53. fill="currentColor"
  54. class="w-5 h-5"
  55. >
  56. <path
  57. 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"
  58. />
  59. </svg>
  60. </button>
  61. </div>
  62. <div class="flex flex-col md:flex-row w-full px-4 pb-3 md:space-x-4 dark:text-gray-200">
  63. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  64. <form
  65. class="flex flex-col w-full"
  66. on:submit|preventDefault={() => {
  67. submitHandler();
  68. }}
  69. >
  70. <div class="px-1">
  71. <div class="flex flex-col w-full">
  72. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('URL')}</div>
  73. <div class="flex-1">
  74. <input
  75. class="w-full text-sm bg-transparent disabled:text-gray-500 dark:disabled:text-gray-500 outline-hidden"
  76. type="url"
  77. bind:value={url}
  78. placeholder={$i18n.t('Enter the URL of the function to import')}
  79. required
  80. />
  81. </div>
  82. </div>
  83. </div>
  84. <div class="flex justify-end pt-3 text-sm font-medium">
  85. <button
  86. 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
  87. ? ' cursor-not-allowed'
  88. : ''}"
  89. type="submit"
  90. disabled={loading}
  91. >
  92. {$i18n.t('Import')}
  93. {#if loading}
  94. <div class="ml-2 self-center">
  95. <svg
  96. class=" w-4 h-4"
  97. viewBox="0 0 24 24"
  98. fill="currentColor"
  99. xmlns="http://www.w3.org/2000/svg"
  100. ><style>
  101. .spinner_ajPY {
  102. transform-origin: center;
  103. animation: spinner_AtaB 0.75s infinite linear;
  104. }
  105. @keyframes spinner_AtaB {
  106. 100% {
  107. transform: rotate(360deg);
  108. }
  109. }
  110. </style><path
  111. 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"
  112. opacity=".25"
  113. /><path
  114. 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"
  115. class="spinner_ajPY"
  116. /></svg
  117. >
  118. </div>
  119. {/if}
  120. </button>
  121. </div>
  122. </form>
  123. </div>
  124. </div>
  125. </div>
  126. </Modal>