docs/.vitepress/theme/components/Feedback.vue

996d569334aead93e7388d2447be33599af3026c
wotaku/docs/.vitepress/theme/components/Feedback.vue
Duck feedback534f08cff2 2 months ago

413 lines · 12645 bytes

  1<!--
  2  All Rights Reserved
  3
  4  Copyright (c) 2025 taskylizard
  5
  6  All rights reserved. This code and its associated files may not be copied, modified, distributed, sublicensed, or used in any form, in whole or in part, without prior written permission from the copyright holder.
  7-->
  8<script setup lang="ts">
  9import { useRouter, withBase } from 'vitepress'
 10import { computed, reactive, ref } from 'vue'
 11import {
 12  feedbackOptions,
 13  type FeedbackType,
 14  getFeedbackOption
 15} from '../../types/Feedback'
 16import { resolvePrompts } from '../../utils/promptConfig'
 17
 18const props = defineProps<{
 19  heading?: string
 20}>()
 21
 22const sluggify = (text: string) =>
 23  text
 24    .toLowerCase()
 25    .replace(/[^a-z0-9_]+/g, '-')
 26    .replace(/--+/g, '-')
 27    .replace(/(^-+|-+$)/g, '')
 28    .substring(0, 60)
 29
 30const getURL = (heading: string) =>
 31  `https://wotaku.wiki${withBase(router.route.path)}#${sluggify(heading)}`
 32
 33const router = useRouter()
 34
 35/**
 36 * Resolve the prompt pool once on mount.
 37 * Re-evaluates reactively if the route changes (SPA navigation).
 38 */
 39const promptPool = computed(() => resolvePrompts(router.route.path))
 40
 41function getPrompt(): string {
 42  const pool = promptPool.value
 43  return pool[Math.floor(Math.random() * pool.length)]
 44}
 45
 46const loading = ref<boolean>(false)
 47const error = ref<unknown>(null)
 48const success = ref<boolean>(false)
 49
 50// Generate or retrieve stable visitor ID from localStorage
 51function getVisitorId(): string {
 52  const key = 'wotaku_visitor_id'
 53  let id = localStorage.getItem(key)
 54  if (!id || id.length !== 32) {
 55    id = crypto.randomUUID().replace(/-/g, '').slice(0, 32)
 56    localStorage.setItem(key, id)
 57  }
 58  return id
 59}
 60
 61const isDisabled = computed(() => {
 62  return (
 63    !feedback.content.length ||
 64    feedback.content.length < 5 ||
 65    feedback.content.length > 2000
 66  )
 67})
 68
 69// prettier-ignore
 70const feedback = reactive<
 71  Pick<FeedbackType, 'content' | 'page'> & Partial<Pick<FeedbackType, 'type' | 'name'>>
 72>({
 73  page: getURL(props.heading!),
 74  content: '',
 75  name: ''
 76})
 77
 78const selectedOption = ref(feedbackOptions[0])
 79
 80function selectType(type: FeedbackType['type']) {
 81  feedback.type = type
 82  selectedOption.value = getFeedbackOption(type)!
 83  error.value = null
 84}
 85
 86async function handleSubmit() {
 87  loading.value = true
 88  success.value = true
 89  error.value = null
 90
 91  const body = {
 92    content: feedback.content,
 93    type: feedback.type!,
 94    page: feedback.page,
 95    visitorId: getVisitorId(),
 96    ...(props.heading && { heading: props.heading }),
 97    ...(feedback.name && { name: feedback.name })
 98  }
 99
100  try {
101    const response = await fetch('https://wotaku-feedback.duckling.workers.dev/api/feedback', {
102      method: 'POST',
103      headers: {
104        'Content-Type': 'application/json'
105      },
106      body: JSON.stringify(body)
107    })
108
109    const data = await response.json()
110    if (data.error) {
111      success.value = false
112      error.value = data.error
113    }
114  } catch (err) {
115    success.value = false
116    error.value = err
117  } finally {
118    loading.value = false
119  }
120}
121
122const isCardShown = ref<boolean>(false)
123const helpfulText = props.heading
124  ? 'What do you think about this section?'
125  : 'What do you think about this page?'
126const prompt = computed(() => getPrompt())
127const toggleCard = () => (isCardShown.value = !isCardShown.value)
128</script>
129
130<template>
131  <template v-if="props.heading">
132    <button
133      @click="toggleCard()"
134      class="bg-$vp-c-default-soft text-primary border-$vp-c-default-soft hover:border-primary ml-3 inline-flex h-7 items-center justify-center whitespace-nowrap rounded-md border-2 border-solid px-1.5 py-3.5 text-sm font-medium transition-all duration-300 sm:h-6"
135      style="vertical-align: middle"
136    >
137      <span
138        :class="isCardShown === false ? `i-lucide:mail` : `i-lucide:mail-x`"
139      />
140    </button>
141  </template>
142  <template v-else>
143    <button
144      class="bg-$vp-c-default-soft text-primary px2 py1 border-$vp-c-default-soft hover:border-primary mt-2 select-none rounded border-2 border-solid font-bold transition-all duration-300"
145      @click="toggleCard()"
146    >
147      <span
148        :class="isCardShown === false
149        ? `i-lucide:mail mr-2`
150        : `i-lucide:mail-x mr-2`"
151      />
152      <span>Send Feedback</span>
153    </button>
154  </template>
155
156  <Transition name="fade" mode="out-in">
157    <div
158      v-if="isCardShown"
159      :class="['border-$vp-c-divider bg-$vp-c-bg-alt b-rd-4 m-[2rem 0] mt-4 border-2 border-solid px-6 pt-3', success ? 'pb-3' : 'pb-6']"
160    >
161      <Transition name="fade" mode="out-in">
162        <div v-if="!feedback.type">
163          <p class="heading">
164            {{ helpfulText }}
165          </p>
166          <div class="flex flex-wrap gap-2">
167            <button
168              v-for="item in feedbackOptions"
169              :key="item.value"
170              class="bg-bg border-$vp-c-default-soft hover:border-primary mt-2 select-none rounded border-2 border-solid font-bold transition-all duration-250 rounded-lg text-[14px] font-500 leading-normal m-0 px-3 py-1.5 text-center align-middle whitespace-nowrap"
171              @click="selectType(item.value)"
172            >
173              <span :class="[item.icon, 'mr-1 inline-block align-middle']" />
174              <span class="align-middle">{{ item.label }}</span>
175            </button>
176          </div>
177        </div>
178        <div v-else-if="feedback.type && !success" class="pt-2">
179          <div class="mb-3">
180            <span class="text-[22px] font-bold flex items-center gap-2">
181              <span :class="getFeedbackOption(feedback.type)?.icon" />
182              {{ getFeedbackOption(feedback.type)?.label }}
183            </span>
184          </div>
185
186          <div v-if="feedback.type === 'submit'" class="mb-2 text-sm">
187            <details>
188              <summary class="flex items-center gap-1">
189                <span class="i-iconoir-nav-arrow-right arrow-right" />
190                <span class="i-iconoir-nav-arrow-down arrow-down" />
191                <span class="i-lucide-shield-x bg-cerise-400" />
192                Things we won't add in the wiki
193              </summary>
194              <ol>
195                <li>
196                  Websites that are only or primarily for generative AI
197                  (Chatbot, roleplaying bot, ai art generator etc.)
198                </li>
199                <li>
200                  Sites that
201                  <ul>
202                    <li>imitate other websites</li>
203                    <li>have very small libraries</li>
204                    <li>only feature MTL'd works</li>
205                    <li>are mostly paywalled</li>
206                    <li>sell unofficial merch</li>
207                    <li>exlusively sell sex toys</li>
208                  </ul>
209                </li>
210                <li>Adult content sites similar to OnlyFans</li>
211                <li>
212                  Software that's too general (for example Windows OS related or
213                  Tweaking for privacy)
214                </li>
215                <li>Sites focused on lolicon or furry stuff</li>
216                <li>
217                  Sites scraping only a couple popular sources
218                </li>
219                <li>
220                  Sites scraping the servers of other scrapers
221                </li>
222                <li>
223                  Sites found in the Unsafe lists of
224                  <a
225                    href="https://privateers.wiki/unsafe"
226                    class="text-primary text-underline font-semibold"
227                  >
228                    Privateersclub
229                  </a>
230                  and/or
231                  <a
232                    href="https://rentry.org/pgames#untrusted-sites"
233                    class="text-primary text-underline font-semibold"
234                  >
235                    r/PiratedGames
236                  </a>
237                  .
238                </li>
239              </ol>
240            </details>
241            <details>
242              <summary class="flex items-center gap-1">
243                <span class="i-iconoir-nav-arrow-right arrow-right" />
244                <span class="i-iconoir-nav-arrow-down arrow-down" />
245                <span class="i-lucide-shield-alert bg-yellow-400" />
246                Things we will try to avoid
247              </summary>
248              <ol>
249                <li>Closed source software (with good FOSS alternatives)</li>
250                <li>
251                  Sites that only use hosters such Katfile, Nitroflare,
252                  DDownload and Rapidgator
253                </li>
254                <li>
255                  Sites that aren't primarily for anime but have it as a side
256                  product
257                </li>
258                <li>Things that are too niche and/or have a small userbase</li>
259              </ol>
260            </details>
261          </div>
262          <textarea
263            v-model="feedback.content"
264            autofocus
265            class="feedback-textarea bg-$vp-c-bg-alt text-$vp-c-text-2 w-full h-[100px] border border-$vp-c-divider rounded px-3 py-1.5 border-$vp-c-divider bg-$vp-c-bg-alt b-rd-4 border-2 border-solid"
266            :placeholder="prompt"
267          />
268          <input
269            v-model="feedback.name"
270            maxlength="50"
271            class="bg-$vp-c-bg-alt text-$vp-c-text-2 w-full border border-$vp-c-divider rounded px-3 py-1.5 mt-2 b-rd-4 border-2 border-solid text-sm"
272            placeholder="Name (optional)"
273          />
274          <p v-if="error" class="text-red-400 text-sm mt-1">
275            Failed to send: {{ String(error) }}
276          </p>
277          <p class="desc mb-2">
278            If you want a reply to your feedback, feel free to mention a contact
279            in the message or join our
280            <a
281              class="text-primary text-underline font-semibold"
282              href="https://discord.gg/wZMuSGpZ8s"
283            >
284              Discord.
285            </a>
286          </p>
287          <div class="flex flex-row gap-2">
288            <button
289              class="bg-$vp-c-default-soft text-primary border-$vp-c-default-soft inline-flex h-7 items-center justify-center whitespace-nowrap rounded-md border-2 border-solid px-1.5 py-3.5 text-sm font-medium transition-all duration-300 sm:h-6"
290              @click="feedback.type = undefined"
291            >
292              <span class="i-lucide:arrow-left-from-line">close</span>
293            </button>
294            <button
295              type="submit"
296              style="background-color: #438afe; border-color: #438afe;" class="border rounded-lg transition-colors duration-250 inline-flex items-center gap-1 text-14px font-500 leading-1.5 px-3 py-1.5 text-center align-middle whitespace-nowrap disabled:opacity-50 text-white hover:brightness-110 disabled:opacity-50"
297              :disabled="isDisabled"
298              @click="handleSubmit()"
299            >
300              Send Feedback <span class="i-lucide:mail-check" />
301            </button>
302          </div>
303        </div>
304        <div v-else>
305          <p class="heading">Thanks for your feedback!</p>
306        </div>
307      </Transition>
308    </div>
309  </Transition>
310</template>
311
312<style scoped lang="css">
313.btn {
314  border: 1px solid var(--vp-c-divider);
315  background-color: var(--vp-c-bg);
316  border-radius: 8px;
317  transition: border-color 0.25s, background-color 0.25s;
318  display: inline-block;
319  font-size: 14px;
320  font-weight: 500;
321  line-height: 1.5;
322  margin: 0;
323  padding: 0.375rem 0.75rem;
324  text-align: center;
325  vertical-align: middle;
326  white-space: nowrap;
327}
328
329.btn:disabled {
330  opacity: 0.5;
331}
332
333.btn:hover {
334  border-color: var(--vp-c-brand);
335}
336
337.btn-primary {
338  color: #fff;
339  background-color: var(--vp-c-brand);
340  border-color: var(--vp-c-brand);
341}
342
343.btn-primary:hover {
344  background-color: var(--vp-c-brand-darker);
345  border-color: var(--vp-c-brand-darker);
346}
347
348.heading {
349  font-size: 1.2rem;
350  font-weight: 700;
351}
352
353.desc {
354  display: block;
355  font-size: 12px;
356  color: var(--vp-c-text-2);
357}
358
359.prompt-text {
360  display: block;
361  font-size: 10px;
362  color: var(--vp-c-text-2);
363  opacity: 0.75;
364}
365
366.feedback-textarea {
367  font-size: 14px;
368}
369
370.feedback-textarea::placeholder {
371  font-size: 12px;
372}
373
374details summary {
375  display: flex;
376  align-items: center;
377  gap: 0.25rem;
378  cursor: pointer;
379  list-style: none;
380}
381
382details summary::-webkit-details-marker {
383  display: none;
384}
385
386details summary .arrow-right {
387  display: inline-block;
388  flex-shrink: 0;
389}
390
391details summary .arrow-down {
392  display: none;
393  flex-shrink: 0;
394}
395
396details[open] summary .arrow-right {
397  display: none;
398}
399
400details[open] summary .arrow-down {
401  display: inline-block;
402}
403
404.fade-enter-active,
405.fade-leave-active {
406  transition: opacity 0.25s ease;
407}
408
409.fade-enter-from,
410.fade-leave-to {
411  opacity: 0;
412}
413</style>
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form