Files
CT/src/Views/Objects/objectDetailModal.tsx
T

146 lines
3.8 KiB
TypeScript
Raw Normal View History

import * as React from "bloatless-react";
2024-07-11 15:08:25 +02:00
import { Chat, MessageObject } from "../../Model/chatModel";
import { translation } from "../../translations";
2024-07-11 13:05:02 +02:00
export function ObjectDetailModal(
chat: Chat,
2024-07-11 13:05:02 +02:00
messageObject: MessageObject,
isPresented: React.State<boolean>
) {
2024-07-11 14:03:15 +02:00
// state
const editingTitle = new React.State(messageObject.title);
2024-07-11 15:08:25 +02:00
const selectedMessageObjectIndex = new React.State(0);
const selectedMessageObject = React.createProxyState(
[selectedMessageObjectIndex],
() =>
chat.getObjectContentFromIndex(
messageObject,
selectedMessageObjectIndex.value
)
);
const didEditContent = new React.State(false);
const editingNoteContent = React.createProxyState(
[selectedMessageObject],
() => selectedMessageObject.value.noteContent ?? ""
);
React.bulkSubscribe(
[editingNoteContent],
() => (didEditContent.value = true)
2024-07-11 14:03:15 +02:00
);
// methods
2024-07-11 15:08:25 +02:00
function handleKeyDown(e: KeyboardEvent) {
if (!e.metaKey && !e.ctrlKey) return;
switch (e.key) {
case "s":
e.preventDefault();
saveAndClose();
break;
}
}
function closeModal() {
isPresented.value = false;
}
function saveAndClose() {
2024-07-11 13:05:02 +02:00
messageObject.title = editingTitle.value;
2024-07-11 15:08:25 +02:00
chat.updateObject(
messageObject.id,
didEditContent.value == true
? {
isoDateVersionCreated: new Date().toISOString(),
id: React.UUID(),
noteContent: editingNoteContent.value,
}
: undefined
);
closeModal();
}
function deleteAndClose() {
2024-07-11 13:05:02 +02:00
chat.deleteObject(messageObject);
closeModal();
}
return (
2024-07-11 15:08:25 +02:00
<div class="modal" toggle:open={isPresented} on:keydown={handleKeyDown}>
<div>
<main>
2024-07-11 13:05:02 +02:00
<h2>{messageObject.title}</h2>
2024-07-11 14:03:15 +02:00
<span class="secondary">{messageObject.id}</span>
<hr></hr>
2024-07-11 16:00:38 +02:00
<div class="flex-column gap">
<label class="tile">
<span class="icon">label</span>
<div>
<span>{translation.objectTitle}</span>
<input
bind:value={editingTitle}
placeholder={translation.objectTitlePlaceholder}
></input>
</div>
</label>
2024-07-11 14:03:15 +02:00
2024-07-11 16:00:38 +02:00
<hr></hr>
2024-07-11 15:10:35 +02:00
<label class="tile">
<span class="icon">history</span>
<div>
<span>{translation.objectVersion}</span>
<select bind:value={selectedMessageObjectIndex}>
{...messageObject.contentVersions.map((content, index) => (
<option value={index}>
{new Date(content.isoDateVersionCreated).toLocaleString()}
</option>
))}
</select>
<span class="icon">arrow_drop_down</span>
</div>
</label>
<label class="tile">
<span class="icon">sticky_note_2</span>
<div>
<span>{translation.noteContent}</span>
<textarea
rows="5"
bind:value={editingNoteContent}
placeholder={translation.noteContentPlaceholder}
></textarea>
</div>
</label>
2024-07-11 15:08:25 +02:00
2024-07-11 16:00:38 +02:00
<hr></hr>
2024-07-11 16:00:38 +02:00
<button class="danger width-input" on:click={deleteAndClose}>
{translation.deleteObject}
<span class="icon">delete</span>
</button>
</div>
</main>
<div class="flex-row">
<button class="flex-1 width-100 danger" on:click={closeModal}>
{translation.discard}
</button>
<button class="flex-1 width-100 primary" on:click={saveAndClose}>
{translation.save}
<span class="icon">save</span>
</button>
</div>
</div>
</div>
);
}