RateComment.svelte 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { createEventDispatcher, onMount, getContext } from 'svelte';
  4. const i18n = getContext('i18n');
  5. const dispatch = createEventDispatcher();
  6. export let show = false;
  7. export let message;
  8. let LIKE_REASONS = [];
  9. let DISLIKE_REASONS = [];
  10. function loadReasons() {
  11. LIKE_REASONS = [
  12. $i18n.t("Accurate information"),
  13. $i18n.t("Followed instructions perfectly"),
  14. $i18n.t("Showcased creativity"),
  15. $i18n.t("Positive attitude"),
  16. $i18n.t("Attention to detail"),
  17. $i18n.t("Thorough explanation"),
  18. $i18n.t("Other")
  19. ];
  20. DISLIKE_REASONS = [
  21. $i18n.t("Don't like the style"),
  22. $i18n.t("Not factually correct"),
  23. $i18n.t("Didn't fully follow instructions"),
  24. $i18n.t("Refused when it shouldn't have"),
  25. $i18n.t("Being lazy"),
  26. $i18n.t("Other")
  27. ];
  28. }
  29. let reasons = [];
  30. let selectedReason = null;
  31. let comment = '';
  32. $: if (message.annotation.rating === 1) {
  33. reasons = LIKE_REASONS;
  34. } else if (message.annotation.rating === -1) {
  35. reasons = DISLIKE_REASONS;
  36. }
  37. onMount(() => {
  38. selectedReason = message.annotation.reason;
  39. comment = message.annotation.comment;
  40. loadReasons();
  41. });
  42. const submitHandler = () => {
  43. console.log('submitHandler');
  44. message.annotation.reason = selectedReason;
  45. message.annotation.comment = comment;
  46. dispatch('submit');
  47. toast.success($i18n.t('Thanks for your feedback!'));
  48. show = false;
  49. };
  50. </script>
  51. <div class=" my-2.5 rounded-xl px-4 py-3 border dark:border-gray-850">
  52. <div class="flex justify-between items-center">
  53. <div class=" text-sm">{$i18n.t('Tell us more:')}</div>
  54. <button
  55. on:click={() => {
  56. show = false;
  57. }}
  58. >
  59. <svg
  60. xmlns="http://www.w3.org/2000/svg"
  61. fill="none"
  62. viewBox="0 0 24 24"
  63. stroke-width="1.5"
  64. stroke="currentColor"
  65. class="size-4"
  66. >
  67. <path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
  68. </svg>
  69. </button>
  70. </div>
  71. {#if reasons.length > 0}
  72. <div class="flex flex-wrap gap-2 text-sm mt-2.5">
  73. {#each reasons as reason}
  74. <button
  75. class="px-3.5 py-1 border dark:border-gray-850 dark:hover:bg-gray-850 {selectedReason ===
  76. reason
  77. ? 'dark:bg-gray-800'
  78. : ''} transition rounded-lg"
  79. on:click={() => {
  80. selectedReason = reason;
  81. }}
  82. >
  83. {reason}
  84. </button>
  85. {/each}
  86. </div>
  87. {/if}
  88. <div class="mt-2">
  89. <textarea
  90. bind:value={comment}
  91. class="w-full text-sm px-1 py-2 bg-transparent outline-none resize-none rounded-xl"
  92. placeholder="{$i18n.t('Feel free to add specific details')}"
  93. rows="2"
  94. />
  95. </div>
  96. <div class="mt-2 flex justify-end">
  97. <button
  98. class=" bg-emerald-700 text-white text-sm font-medium rounded-lg px-3.5 py-1.5"
  99. on:click={() => {
  100. submitHandler();
  101. }}
  102. >
  103. Submit
  104. </button>
  105. </div>
  106. </div>