feat: rename a chat from the menu next to its title (#33177)

This commit is contained in:
G30
2026-10-26 04:02:00 +09:00
committed by GitHub
parent af6c382d3a
commit 551b03ff90
3 changed files with 88 additions and 7 deletions
+70 -6
View File
@@ -1,5 +1,5 @@
<script lang="ts">
import { getContext } from 'svelte';
import { getContext, tick } from 'svelte';
import { toast } from 'svelte-sonner';
import {
@@ -7,6 +7,7 @@
artifactContents,
banners,
chatId,
chatTitle as _chatTitle,
config,
mobile,
settings,
@@ -37,6 +38,8 @@
import ChatCheck from '../icons/ChatCheck.svelte';
import Knobs from '../icons/Knobs.svelte';
import { isTemporaryChatId } from '$lib/utils/chatId';
import { updateChatById } from '$lib/apis/chats';
import { refreshChatList, refreshFolderChatLists } from '$lib/stores/chatList';
import ChatParticipants from '$lib/components/common/ChatParticipants.svelte';
const i18n: any = getContext('i18n');
@@ -68,6 +71,45 @@
let showShareChatModal = false;
let showDownloadChatModal = false;
let confirmEdit = false;
let chatTitle = '';
let chatTitleInput: HTMLInputElement;
const renameHandler = async () => {
chatTitle = title || chat?.chat?.title || '';
confirmEdit = true;
await tick();
setTimeout(() => {
chatTitleInput?.focus();
chatTitleInput?.select();
}, 0);
};
const editChatTitle = async () => {
if (!confirmEdit) return;
confirmEdit = false;
const newTitle = chatTitle.trim();
if (newTitle === (title || chat?.chat?.title)) return;
if (!newTitle) {
toast.error($i18n.t('Title cannot be an empty string.'));
return;
}
const res = await updateChatById(localStorage.token, chat.id, { title: newTitle }).catch(
(error) => {
toast.error(`${error}`);
return null;
}
);
if (!res) return;
_chatTitle.set(newTitle);
await refreshChatList(localStorage.token, { refreshPinned: true });
await refreshFolderChatLists();
};
</script>
<ShareChatModal bind:show={showShareChatModal} chatId={$chatId} />
@@ -122,11 +164,32 @@
>
{#if chat?.id}
<div class="flex max-w-full min-w-0 items-center gap-2 mr-2">
<h1
class="min-w-0 truncate py-1 text-left text-[0.9375rem] font-normal text-gray-700 dark:text-gray-300"
>
{title || chat?.chat?.title || $i18n.t('New Chat')}
</h1>
{#if confirmEdit}
<input
bind:this={chatTitleInput}
bind:value={chatTitle}
class="min-w-0 flex-1 bg-transparent py-1 text-[0.9375rem] font-normal text-gray-700 outline-hidden dark:text-gray-300"
aria-label={$i18n.t('Title')}
on:keydown={(e) => {
if (e.isComposing || e.keyCode === 229) return;
if (e.key === 'Enter') {
e.preventDefault();
editChatTitle();
} else if (e.key === 'Escape') {
e.preventDefault();
confirmEdit = false;
}
}}
on:blur={editChatTitle}
/>
{:else}
<h1
class="min-w-0 truncate py-1 text-left text-[0.9375rem] font-normal text-gray-700 dark:text-gray-300"
>
{title || chat?.chat?.title || $i18n.t('New Chat')}
</h1>
{/if}
{#if shareEnabled && chat && (chat.id || $temporaryChatEnabled) && (scrollToTop || ($artifactContents ?? []).length > 0 || (!readOnly && !$temporaryChatEnabled) || $user?.role === 'admin' || ($user?.permissions?.chat?.export ?? true))}
<Menu
@@ -137,6 +200,7 @@
shareHandler={() => {
showShareChatModal = !showShareChatModal;
}}
{renameHandler}
archiveChatHandler={() => {
archiveChatHandler(chat.id);
}}
@@ -37,6 +37,7 @@
import GarbageBin from '$lib/components/icons/GarbageBin.svelte';
import Messages from '$lib/components/chat/Messages.svelte';
import Download from '$lib/components/icons/Download.svelte';
import EditPencilIcon from '$lib/components/layout/Sidebar/icons/EditPencil.svelte';
const i18n = getContext('i18n');
@@ -44,6 +45,7 @@
export let readOnly: boolean = false;
export let shareHandler: Function;
export let renameHandler: Function;
export let moveChatHandler: Function;
export let archiveChatHandler: Function;
@@ -464,7 +466,22 @@
<div class="flex items-center line-clamp-1">{$i18n.t('PDF document (.pdf)')}</div>
</button>
</DropdownSub>
{/if}
{#if !readOnly && !$temporaryChatEnabled && chat?.id}
<button
draggable="false"
class="flex h-[1.6875rem] w-full items-center gap-2 rounded-xl px-2 text-[0.8125rem] cursor-pointer select-none hover:bg-gray-50/40 dark:hover:bg-gray-800/40"
on:click={() => {
renameHandler();
}}
>
<EditPencilIcon className="size-3.5" strokeWidth="1.5" />
<div class="flex items-center">{$i18n.t('Rename')}</div>
</button>
{/if}
{#if $user?.role === 'admin' || ($user.permissions?.chat?.export ?? true)}
<button
draggable="false"
class="flex h-[1.6875rem] w-full items-center gap-2 rounded-xl px-2 text-[0.8125rem] cursor-pointer select-none hover:bg-gray-50/40 dark:hover:bg-gray-800/40"
@@ -246,7 +246,7 @@
let showShareChatModal = false;
let confirmEdit = false;
let chatTitle = title;
let chatTitle = '';
const editChatTitle = async (id, title) => {
title = title?.trim();