Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions apps/web/src/components/settings/PersonalizationSection.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useEffect, useState } from 'react';
import { useState } from 'react';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { toast } from 'sonner';

Expand Down Expand Up @@ -28,13 +28,14 @@ export function PersonalizationSection() {
const personalization = useQuery(
trpc.preferences.getPersonalization.queryOptions(),
);
const [instructions, setInstructions] = useState('');
const [draft, setDraft] = useState<string | null>(null);
const settings = personalization.data;
const instructions = draft ?? settings?.instructions ?? '';

useEffect(() => {
if (personalization.data) {
setInstructions(personalization.data.instructions);
}
}, [personalization.data]);
// Once saved data matches the draft, follow subsequent clean refreshes again.
if (draft !== null && draft === settings?.instructions) {
setDraft(null);
}

const update = useMutation(
trpc.preferences.updatePersonalization.mutationOptions({
Expand All @@ -43,7 +44,7 @@ export function PersonalizationSection() {
queryKey,
settings,
);
setInstructions(settings.instructions);
setDraft(null);
},
onError: (error) => {
toast.error(error.message);
Expand All @@ -52,7 +53,6 @@ export function PersonalizationSection() {
}),
);

const settings = personalization.data;
const isBusy = personalization.isPending || update.isPending;
const hasChanges = settings?.instructions !== instructions;

Expand Down Expand Up @@ -80,7 +80,7 @@ export function PersonalizationSection() {
rows={7}
className="md:min-h-48"
placeholder="For example: Keep answers concise, lead with a recommendation, and use examples when explaining unfamiliar concepts."
onChange={(event) => setInstructions(event.target.value)}
onChange={(event) => setDraft(event.target.value)}
/>
{instructions.length > MAX_INSTRUCTIONS_LENGTH * 0.8 && (
<div className="flex justify-end text-xs text-muted-foreground">
Expand Down
Loading