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

223 lines
6.2 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";
2024-07-11 22:27:15 +02:00
import { icons } from "../../icons";
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
2024-07-11 16:45:45 +02:00
const selectedMessageObjectId = new React.State(
chat.getMostRecentContentId(messageObject)
);
2024-07-11 15:08:25 +02:00
const selectedMessageObject = React.createProxyState(
2024-07-11 16:45:45 +02:00
[selectedMessageObjectId],
2024-07-11 15:08:25 +02:00
() =>
2024-07-11 16:45:45 +02:00
chat.getObjectContentFromId(messageObject, selectedMessageObjectId.value)
2024-07-11 15:08:25 +02:00
);
2024-07-11 21:30:57 +02:00
// properties
2024-07-11 15:08:25 +02:00
const didEditContent = new React.State(false);
const editingNoteContent = React.createProxyState(
[selectedMessageObject],
() => selectedMessageObject.value.noteContent ?? ""
);
2024-07-11 21:30:57 +02:00
const editingCategory = React.createProxyState(
[selectedMessageObject],
() => selectedMessageObject.value.categoryName ?? ""
);
const editingDate = React.createProxyState(
[selectedMessageObject],
2024-07-11 22:27:15 +02:00
() => selectedMessageObject.value.date ?? ""
2024-07-11 21:30:57 +02:00
);
const editingTime = React.createProxyState(
[selectedMessageObject],
2024-07-11 22:27:15 +02:00
() => selectedMessageObject.value.time ?? ""
2024-07-11 21:30:57 +02:00
);
const editingPriority = React.createProxyState(
[selectedMessageObject],
() => selectedMessageObject.value.priority ?? 0
);
2024-07-11 15:08:25 +02:00
React.bulkSubscribe(
2024-07-11 22:27:15 +02:00
[
editingNoteContent,
editingCategory,
editingDate,
editingTime,
editingPriority,
],
2024-07-11 15:08:25 +02:00
() => (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
2024-07-11 16:45:45 +02:00
if (didEditContent.value == true) {
chat.addObjectContent(messageObject, {
isoDateVersionCreated: new Date().toISOString(),
id: React.UUID(),
2024-07-11 15:08:25 +02:00
2024-07-11 16:45:45 +02:00
noteContent: editingNoteContent.value,
2024-07-11 21:30:57 +02:00
priority: editingPriority.value,
categoryName: editingCategory.value,
2024-07-11 22:27:15 +02:00
date: editingDate.value,
time: editingTime.value,
2024-07-11 16:45:45 +02:00
});
}
2024-07-11 15:08:25 +02:00
2024-07-11 16:45:45 +02:00
chat.addObjectAndSend(messageObject);
closeModal();
}
function deleteAndClose() {
2024-07-11 13:05:02 +02:00
chat.deleteObject(messageObject);
closeModal();
}
const input = (
<input
bind:value={editingTitle}
placeholder={translation.objectTitlePlaceholder}
></input>
);
isPresented.subscribe(() => setTimeout(() => input.focus(), 100));
return (
2024-07-11 15:08:25 +02:00
<div class="modal" toggle:open={isPresented} on:keydown={handleKeyDown}>
<div>
<main>
<h2>{chat.getObjectTitle(messageObject)}</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">
2024-07-11 22:27:15 +02:00
<span class="icon">{icons.objectTitle}</span>
2024-07-11 16:00:38 +02:00
<div>
<span>{translation.objectTitle}</span>
{input}
2024-07-11 16:00:38 +02:00
</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">
2024-07-11 22:27:15 +02:00
<span class="icon">{icons.objectHistory}</span>
2024-07-11 15:10:35 +02:00
<div>
<span>{translation.objectVersion}</span>
2024-07-11 16:45:45 +02:00
<select bind:value={selectedMessageObjectId}>
{...chat
.getSortedContents(messageObject)
.map((content) => (
<option value={content.id}>
{new Date(
content.isoDateVersionCreated
).toLocaleString()}
</option>
))}
2024-07-11 15:10:35 +02:00
</select>
<span class="icon">arrow_drop_down</span>
</div>
</label>
2024-07-11 21:30:57 +02:00
<hr></hr>
2024-07-11 15:10:35 +02:00
<label class="tile">
2024-07-11 22:27:15 +02:00
<span class="icon">{icons.noteContent}</span>
2024-07-11 15:10:35 +02:00
<div>
2024-07-11 21:30:57 +02:00
<span>{translation.note}</span>
2024-07-11 15:10:35 +02:00
<textarea
rows="5"
bind:value={editingNoteContent}
placeholder={translation.noteContentPlaceholder}
></textarea>
</div>
</label>
2024-07-11 15:08:25 +02:00
2024-07-11 21:30:57 +02:00
<label class="tile">
2024-07-11 22:27:15 +02:00
<span class="icon">{icons.categoryName}</span>
2024-07-11 21:30:57 +02:00
<div>
<span>{translation.category}</span>
<input
bind:value={editingCategory}
placeholder={translation.categoryPlaceholder}
></input>
</div>
</label>
<label class="tile">
2024-07-11 22:27:15 +02:00
<span class="icon">{icons.priority}</span>
2024-07-11 21:30:57 +02:00
<div>
<span>{translation.priority}</span>
<input
type="number"
bind:value={editingPriority}
placeholder={translation.priorityPlaceholder}
></input>
</div>
</label>
<hr></hr>
<label class="tile">
2024-07-11 22:27:15 +02:00
<span class="icon">{icons.date}</span>
2024-07-11 21:30:57 +02:00
<div>
<span>{translation.date}</span>
<input type="date" bind:value={editingDate}></input>
</div>
</label>
<label class="tile">
2024-07-11 22:27:15 +02:00
<span class="icon">{icons.time}</span>
2024-07-11 21:30:57 +02:00
<div>
<span>{translation.time}</span>
<input type="time" bind:value={editingTime}></input>
</div>
</label>
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>
);
}