Files
classroomio/apps/dashboard/src/lib/components/AI/AIButton/CustomPromptBtn.svelte
T

152 lines
4.6 KiB
Svelte
Raw Normal View History

<script lang="ts">
2025-11-01 15:49:29 +01:00
// import { preventDefault } from '$lib/utils/functions/svelte';
// import { writable } from 'svelte/store';
// // import { useCompletion } from 'ai/svelte';
// import WandSparklesIcon from '@lucide/svelte/icons/wand-sparkles';
// import { IconButton } from '$lib/components/IconButton';
2026-03-09 11:18:22 +01:00
// import { HTMLRender } from '$features/ui';
2025-11-01 15:49:29 +01:00
// import { t } from '$lib/utils/functions/translations';
2025-11-01 15:49:29 +01:00
// interface Props {
// className?: string;
// handleInsert?: any;
// defaultPrompt?: string;
// alignPopover?: PopoverProps['align'];
// isHTML?: boolean;
// }
2025-11-01 15:49:29 +01:00
// let {
// className = '',
// handleInsert = (_v: string) => {},
// defaultPrompt = '',
// alignPopover = 'left',
// isHTML = false
// }: Props = $props();
2025-11-01 15:49:29 +01:00
// let openPopover = $state(false);
// let plainText = $state('');
// let textRef: HTMLParagraphElement | undefined = $state();
// let prompt: string = $state(defaultPrompt);
2025-11-01 15:49:29 +01:00
// let isSubmitted = $state(false);
2025-11-01 15:49:29 +01:00
// let isLoading = writable(false);
// // const { input, handleSubmit, completion, isLoading } = useCompletion({
// // api: '/api/completion/customprompt'
// // });
2025-11-01 15:49:29 +01:00
// function restart() {
// isSubmitted = false;
// plainText = '';
// }
2025-11-01 15:49:29 +01:00
// function onCompletion(completion: string) {
// plainText = completion;
// if (textRef) {
// textRef.scrollTop = textRef.scrollHeight;
// }
// }
2025-11-01 15:49:29 +01:00
// // function callAI(prompt: string, rephrase: boolean) {
// // isSubmitted = true;
2025-11-01 15:49:29 +01:00
// // $input = rephrase ? `${$t('ai.can_you')}: ${plainText}` : prompt;
2025-11-01 15:49:29 +01:00
// // setTimeout(() => {
// // handleSubmit({ preventDefault: () => {} });
// // }, 500);
// // }
// $effect(() => {
// onCompletion($completion);
// });
// $effect(() => {
// prompt = defaultPrompt;
// });
</script>
2025-11-01 15:49:29 +01:00
<div></div>
<!--
<div class="{className} relative z-[100]">
<IconButton
contained={true}
color="ui:text-primary dark:bg-neutral-500 dark:text-white"
size="large"
onClick={() => (openPopover = !openPopover)}
>
2025-11-01 15:49:29 +01:00
<MagicWandFilled />
<Popover caret align={alignPopover} bind:open={openPopover}>
2025-11-01 15:49:29 +01:00
<div class="h-[220px] w-[300px] p-2 text-start">
{#if isSubmitted}
<div class="h-[200px]">
<div
bind:this={textRef}
2025-11-01 15:49:29 +01:00
class="my-1 h-[82%] w-full overflow-y-auto bg-white p-2 text-start text-sm dark:bg-neutral-700"
>
{#if isHTML}
2026-03-09 11:18:22 +01:00
<HTMLRender>{plainText}</HTMLRender>
{:else}
<p class="font-normal">
{plainText}
</p>
{/if}
</div>
2025-11-01 15:49:29 +01:00
<div class="ml-2 flex gap-5">
<button
2025-11-01 15:49:29 +01:00
class="cursor-pointer rounded-md border-[1px] px-4 py-2 text-xs font-medium"
onclick={() => {
handleInsert(plainText);
openPopover = false;
}}
type="button"
disabled={$isLoading}
>
{#if $isLoading}
<Loading withOverlay={false} small />
{:else}
2024-04-26 17:54:11 +01:00
{$t('ai.insert')}
{/if}
</button>
<button
2025-11-01 15:49:29 +01:00
class="cursor-pointer rounded-md border-[1px] px-4 py-2 text-xs font-medium {$isLoading &&
'cursor-not-allowed opacity-25'}"
onclick={preventDefault(() => callAI(prompt, true))}
type="button"
disabled={$isLoading}
>
2024-04-26 17:54:11 +01:00
{$t('ai.rephrase')}
</button>
{#if !$isLoading}
<button
2025-11-01 15:49:29 +01:00
class="cursor-pointer rounded-md border-[1px] px-4 py-2 text-xs font-medium"
onclick={preventDefault(restart)}
type="button"
>
2024-04-26 17:54:11 +01:00
{$t('ai.reset')}
</button>
{/if}
</div>
</div>
{:else}
<div class="h-full">
2025-11-01 15:49:29 +01:00
<p class="mb-2 ml-1 text-sm font-medium">{$t('ai.help_me')}</p>
<textarea
bind:value={prompt}
2025-11-01 15:49:29 +01:00
class="m-0 h-[65%] w-full rounded-lg border-0 text-sm dark:text-black"
2024-04-26 17:54:11 +01:00
placeholder={$t('ai.placeholder')}
2025-11-01 15:49:29 +01:00
></textarea>
<button
2025-11-01 15:49:29 +01:00
class="m-0 flex rounded-md border px-4 py-2 text-xs font-medium"
onclick={preventDefault(() => callAI(prompt, false))}
type="button"
>
2024-04-26 17:54:11 +01:00
{$t('ai.text')}
</button>
</div>
{/if}
2025-11-01 15:49:29 +01:00
</div>
</Popover>
</IconButton>
2025-11-01 15:49:29 +01:00
</div> -->