Skip to content

Commit

Permalink
feat: allow conversations to be deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
nivthefox committed Nov 7, 2024
1 parent 08c6384 commit a152880
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
11 changes: 10 additions & 1 deletion src/conversation/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class Store {
async delete(userId, conversationId) {
const path = this.#getConversationPath(userId, conversationId);
const fileName = path.split('/').pop();
const file = new File([''], fileName, {type: 'application/json'});
const file = new File(['{}'], fileName, {type: 'application/json'});
const response = await this.#context.FilePicker.upload(this.#source, this.#storagePath, file);

if (!response.path) {
Expand Down Expand Up @@ -141,6 +141,15 @@ export class Store {
throw new Error('File not found');
}
const conversation = await response.json();
if (!conversation || conversation.id !== conversationId) {
// deleted
this.#conversations.delete(conversationId);
return;
}
if (conversation.format !== STORAGE_FORMAT_VERSION) {
// todo: migrate conversation
throw new Error('Invalid conversation format');
}
this.#conversations.set(conversationId, {...conversation, loaded: true});
}

Expand Down
32 changes: 22 additions & 10 deletions src/styles/components/_sidebar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,33 @@
padding: 0;

li {
display: flex; // Add this to make children align horizontally
align-items: center; // Vertically center the content
justify-content: space-between; // Space between link and icon

&.active {
background: hsla(var(--assistant-content) / .25);
}
}

a {
display: block;
width: 100%;
padding: 0.5rem;
border: 1px solid transparent;
color: inherit;
text-decoration: none;
a {
flex: 1; // Allow link to take up remaining space
padding: 0.5rem;
border: 1px solid transparent;
color: inherit;
text-decoration: none;

&:hover {
border: 1px solid hsla(var(--assistant-content) / .5);
}
}

i.delete {
padding: 0.5rem;
cursor: pointer;

&:hover {
border: 1px solid hsla(var(--assistant-content) / .5);
&:hover {
color: hsla(var(--assistant-content) / .8);
}
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/ui/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class Chat extends HandlebarsApplicationMixin(ApplicationV2) {
resizable: true,
},
actions: {
delete: Chat.deleteConversation,
load: Chat.loadConversation,
new: Chat.newConversation,
rename: Chat.rename,
Expand Down Expand Up @@ -211,9 +212,16 @@ export class Chat extends HandlebarsApplicationMixin(ApplicationV2) {
}

// Static Methods
static async deleteConversation(event, target) {
const userId = target.parentElement.getAttribute('data-user-id');
const id = target.parentElement.getAttribute('data-id');
await this.conversationStore.delete(userId, id);
await this.render(false);
}

static async loadConversation(event, target) {
const userId = target.getAttribute('data-user-id');
const id = target.getAttribute('data-id');
const userId = target.parentElement.getAttribute('data-user-id');
const id = target.parentElement.getAttribute('data-id');
this.#activeConversation = await this.conversationStore.get(userId, id);
await this.render(false);
}
Expand Down
5 changes: 3 additions & 2 deletions templates/applications/ChatSidebar.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
<header>{{username}}</header>
<ul>
{{#each conversations as |chat|}}
<li class="{{#if active}}active{{/if}}">
<a data-user-id="{{chat.userId}}" data-id="{{chat.id}}" data-action="load">
<li class="{{#if active}}active{{/if}}" data-id="{{chat.id}}" data-user-id="{{chat.userId}}">
<a data-action="load">
{{chat.title}}
<span>({{chat.messages}})</span>
</a>
<i class="delete fas fa-trash" data-action="delete"></i>
</li>
{{/each}}
</ul>
Expand Down

0 comments on commit a152880

Please sign in to comment.