123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <script lang="ts">
- import { DropdownMenu } from 'bits-ui';
- import { onMount, getContext, createEventDispatcher } from 'svelte';
- import { flyAndScale } from '$lib/utils/transitions';
- import { knowledge } from '$lib/stores';
- import Dropdown from '$lib/components/common/Dropdown.svelte';
- const i18n = getContext('i18n');
- const dispatch = createEventDispatcher();
- export let onClose: Function = () => {};
- let items = [];
- onMount(() => {
- let legacy_documents = $knowledge.filter((item) => item?.meta?.document);
- let legacy_collections =
- legacy_documents.length > 0
- ? [
- {
- name: 'All Documents',
- legacy: true,
- type: 'collection',
- description: 'Deprecated (legacy collection), please create a new knowledge base.',
- title: $i18n.t('All Documents'),
- collection_names: legacy_documents.map((item) => item.id)
- },
- ...legacy_documents
- .reduce((a, item) => {
- return [...new Set([...a, ...(item?.meta?.tags ?? []).map((tag) => tag.name)])];
- }, [])
- .map((tag) => ({
- name: tag,
- legacy: true,
- type: 'collection',
- description: 'Deprecated (legacy collection), please create a new knowledge base.',
- collection_names: legacy_documents
- .filter((item) => (item?.meta?.tags ?? []).map((tag) => tag.name).includes(tag))
- .map((item) => item.id)
- }))
- ]
- : [];
- items = [...$knowledge, ...legacy_collections].map((item) => {
- return {
- ...item,
- ...(item?.legacy || item?.meta?.legacy || item?.meta?.document ? { legacy: true } : {})
- };
- });
- });
- </script>
- <Dropdown
- on:change={(e) => {
- if (e.detail === false) {
- onClose();
- }
- }}
- >
- <slot />
- <div slot="content">
- <DropdownMenu.Content
- class="w-full max-w-80 rounded-lg px-1 py-1.5 border border-gray-300/30 dark:border-gray-700/50 z-50 bg-white dark:bg-gray-850 dark:text-white shadow-lg"
- sideOffset={8}
- side="bottom"
- align="start"
- transition={flyAndScale}
- >
- <div class="max-h-[10rem] overflow-y-scroll">
- {#if items.length === 0}
- <div class="text-center text-sm text-gray-500 dark:text-gray-400">
- {$i18n.t('No knowledge found')}
- </div>
- {:else}
- {#each items as item}
- <DropdownMenu.Item
- class="flex gap-2.5 items-center px-3 py-2 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
- on:click={() => {
- dispatch('select', item);
- }}
- >
- <div class="flex items-center">
- <div class="flex flex-col">
- <div class=" w-fit mb-0.5">
- {#if item.legacy}
- <div
- class="bg-gray-500/20 text-gray-700 dark:text-gray-200 rounded uppercase text-xs font-bold px-1"
- >
- Legacy
- </div>
- {:else if item?.meta?.document}
- <div
- class="bg-gray-500/20 text-gray-700 dark:text-gray-200 rounded uppercase text-xs font-bold px-1"
- >
- Document
- </div>
- {:else}
- <div
- class="bg-green-500/20 text-green-700 dark:text-green-200 rounded uppercase text-xs font-bold px-1"
- >
- Collection
- </div>
- {/if}
- </div>
- <div class="line-clamp-1 font-medium pr-0.5">
- {item.name}
- </div>
- </div>
- </div>
- </DropdownMenu.Item>
- {/each}
- {/if}
- </div>
- </DropdownMenu.Content>
- </div>
- </Dropdown>
|