chore(client/ts): port services/branches

This commit is contained in:
Elian Doran 2024-12-21 15:34:07 +02:00
parent cc8f927718
commit efb17c9010
No known key found for this signature in database
3 changed files with 53 additions and 27 deletions

View File

@ -15,6 +15,7 @@ import toast from "../services/toast.js";
import ShortcutComponent from "./shortcut_component.js"; import ShortcutComponent from "./shortcut_component.js";
import { t, initLocale } from "../services/i18n.js"; import { t, initLocale } from "../services/i18n.js";
import NoteDetailWidget from "../widgets/note_detail.js"; import NoteDetailWidget from "../widgets/note_detail.js";
import { ResolveOptions } from "../widgets/dialogs/delete_notes.js";
interface Layout { interface Layout {
getRootWidget: (appContext: AppContext) => RootWidget; getRootWidget: (appContext: AppContext) => RootWidget;
@ -43,6 +44,11 @@ export type TriggerData = {
} | { } | {
// For "searchNotes" // For "searchNotes"
searchString: string | undefined; searchString: string | undefined;
} | {
// For "showDeleteNotesDialog"
branchIdsToDelete: string[];
callback: (value: ResolveOptions) => void;
forceDeleteAllClones: boolean;
} }
class AppContext extends Component { class AppContext extends Component {

View File

@ -1,17 +1,28 @@
import utils from './utils.js'; import utils from './utils.js';
import server from './server.js'; import server from './server.js';
import toastService from "./toast.js"; import toastService, { ToastOptions } from "./toast.js";
import froca from "./froca.js"; import froca from "./froca.js";
import hoistedNoteService from "./hoisted_note.js"; import hoistedNoteService from "./hoisted_note.js";
import ws from "./ws.js"; import ws from "./ws.js";
import appContext from "../components/app_context.js"; import appContext from "../components/app_context.js";
import { t } from './i18n.js'; import { t } from './i18n.js';
import { Node } from './tree.js';
import { ResolveOptions } from '../widgets/dialogs/delete_notes.js';
async function moveBeforeBranch(branchIdsToMove, beforeBranchId) { // TODO: Deduplicate type with server
interface Response {
success: boolean;
message: string;
}
async function moveBeforeBranch(branchIdsToMove: string[], beforeBranchId: string) {
branchIdsToMove = filterRootNote(branchIdsToMove); branchIdsToMove = filterRootNote(branchIdsToMove);
branchIdsToMove = filterSearchBranches(branchIdsToMove); branchIdsToMove = filterSearchBranches(branchIdsToMove);
const beforeBranch = froca.getBranch(beforeBranchId); const beforeBranch = froca.getBranch(beforeBranchId);
if (!beforeBranch) {
return;
}
if (['root', '_lbRoot', '_lbAvailableLaunchers', '_lbVisibleLaunchers'].includes(beforeBranch.noteId)) { if (['root', '_lbRoot', '_lbAvailableLaunchers', '_lbVisibleLaunchers'].includes(beforeBranch.noteId)) {
toastService.showError(t("branches.cannot-move-notes-here")); toastService.showError(t("branches.cannot-move-notes-here"));
@ -19,7 +30,7 @@ async function moveBeforeBranch(branchIdsToMove, beforeBranchId) {
} }
for (const branchIdToMove of branchIdsToMove) { for (const branchIdToMove of branchIdsToMove) {
const resp = await server.put(`branches/${branchIdToMove}/move-before/${beforeBranchId}`); const resp = await server.put<Response>(`branches/${branchIdToMove}/move-before/${beforeBranchId}`);
if (!resp.success) { if (!resp.success) {
toastService.showError(resp.message); toastService.showError(resp.message);
@ -28,11 +39,14 @@ async function moveBeforeBranch(branchIdsToMove, beforeBranchId) {
} }
} }
async function moveAfterBranch(branchIdsToMove, afterBranchId) { async function moveAfterBranch(branchIdsToMove: string[], afterBranchId: string) {
branchIdsToMove = filterRootNote(branchIdsToMove); branchIdsToMove = filterRootNote(branchIdsToMove);
branchIdsToMove = filterSearchBranches(branchIdsToMove); branchIdsToMove = filterSearchBranches(branchIdsToMove);
const afterNote = froca.getBranch(afterBranchId).getNote(); const afterNote = await froca.getBranch(afterBranchId)?.getNote();
if (!afterNote) {
return;
}
const forbiddenNoteIds = [ const forbiddenNoteIds = [
'root', 'root',
@ -50,7 +64,7 @@ async function moveAfterBranch(branchIdsToMove, afterBranchId) {
branchIdsToMove.reverse(); // need to reverse to keep the note order branchIdsToMove.reverse(); // need to reverse to keep the note order
for (const branchIdToMove of branchIdsToMove) { for (const branchIdToMove of branchIdsToMove) {
const resp = await server.put(`branches/${branchIdToMove}/move-after/${afterBranchId}`); const resp = await server.put<Response>(`branches/${branchIdToMove}/move-after/${afterBranchId}`);
if (!resp.success) { if (!resp.success) {
toastService.showError(resp.message); toastService.showError(resp.message);
@ -59,8 +73,11 @@ async function moveAfterBranch(branchIdsToMove, afterBranchId) {
} }
} }
async function moveToParentNote(branchIdsToMove, newParentBranchId) { async function moveToParentNote(branchIdsToMove: string[], newParentBranchId: string) {
const newParentBranch = froca.getBranch(newParentBranchId); const newParentBranch = froca.getBranch(newParentBranchId);
if (!newParentBranch) {
return;
}
if (newParentBranch.noteId === '_lbRoot') { if (newParentBranch.noteId === '_lbRoot') {
toastService.showError(t("branches.cannot-move-notes-here")); toastService.showError(t("branches.cannot-move-notes-here"));
@ -72,12 +89,13 @@ async function moveToParentNote(branchIdsToMove, newParentBranchId) {
for (const branchIdToMove of branchIdsToMove) { for (const branchIdToMove of branchIdsToMove) {
const branchToMove = froca.getBranch(branchIdToMove); const branchToMove = froca.getBranch(branchIdToMove);
if (branchToMove.noteId === hoistedNoteService.getHoistedNoteId() if (!branchToMove
|| (await branchToMove.getParentNote()).type === 'search') { || branchToMove.noteId === hoistedNoteService.getHoistedNoteId()
|| (await branchToMove.getParentNote())?.type === 'search') {
continue; continue;
} }
const resp = await server.put(`branches/${branchIdToMove}/move-to/${newParentBranchId}`); const resp = await server.put<Response>(`branches/${branchIdToMove}/move-to/${newParentBranchId}`);
if (!resp.success) { if (!resp.success) {
toastService.showError(resp.message); toastService.showError(resp.message);
@ -86,7 +104,7 @@ async function moveToParentNote(branchIdsToMove, newParentBranchId) {
} }
} }
async function deleteNotes(branchIdsToDelete, forceDeleteAllClones = false) { async function deleteNotes(branchIdsToDelete: string[], forceDeleteAllClones = false) {
branchIdsToDelete = filterRootNote(branchIdsToDelete); branchIdsToDelete = filterRootNote(branchIdsToDelete);
if (branchIdsToDelete.length === 0) { if (branchIdsToDelete.length === 0) {
@ -100,7 +118,7 @@ async function deleteNotes(branchIdsToDelete, forceDeleteAllClones = false) {
deleteAllClones = false; deleteAllClones = false;
} }
else { else {
({proceed, deleteAllClones, eraseNotes} = await new Promise(res => ({proceed, deleteAllClones, eraseNotes} = await new Promise<ResolveOptions>(res =>
appContext.triggerCommand('showDeleteNotesDialog', {branchIdsToDelete, callback: res, forceDeleteAllClones}))); appContext.triggerCommand('showDeleteNotesDialog', {branchIdsToDelete, callback: res, forceDeleteAllClones})));
} }
@ -127,10 +145,9 @@ async function deleteNotes(branchIdsToDelete, forceDeleteAllClones = false) {
const branch = froca.getBranch(branchIdToDelete); const branch = froca.getBranch(branchIdToDelete);
if (deleteAllClones) { if (deleteAllClones && branch) {
await server.remove(`notes/${branch.noteId}${query}`); await server.remove(`notes/${branch.noteId}${query}`);
} } else {
else {
await server.remove(`branches/${branchIdToDelete}${query}`); await server.remove(`branches/${branchIdToDelete}${query}`);
} }
} }
@ -152,7 +169,7 @@ async function activateParentNotePath() {
} }
} }
async function moveNodeUpInHierarchy(node) { async function moveNodeUpInHierarchy(node: Node) {
if (hoistedNoteService.isHoistedNode(node) if (hoistedNoteService.isHoistedNode(node)
|| hoistedNoteService.isTopLevelNode(node) || hoistedNoteService.isTopLevelNode(node)
|| node.getParent().data.noteType === 'search') { || node.getParent().data.noteType === 'search') {
@ -162,7 +179,7 @@ async function moveNodeUpInHierarchy(node) {
const targetBranchId = node.getParent().data.branchId; const targetBranchId = node.getParent().data.branchId;
const branchIdToMove = node.data.branchId; const branchIdToMove = node.data.branchId;
const resp = await server.put(`branches/${branchIdToMove}/move-after/${targetBranchId}`); const resp = await server.put<Response>(`branches/${branchIdToMove}/move-after/${targetBranchId}`);
if (!resp.success) { if (!resp.success) {
toastService.showError(resp.message); toastService.showError(resp.message);
@ -175,22 +192,25 @@ async function moveNodeUpInHierarchy(node) {
} }
} }
function filterSearchBranches(branchIds) { function filterSearchBranches(branchIds: string[]) {
return branchIds.filter(branchId => !branchId.startsWith('virt-')); return branchIds.filter(branchId => !branchId.startsWith('virt-'));
} }
function filterRootNote(branchIds) { function filterRootNote(branchIds: string[]) {
const hoistedNoteId = hoistedNoteService.getHoistedNoteId(); const hoistedNoteId = hoistedNoteService.getHoistedNoteId();
return branchIds.filter(branchId => { return branchIds.filter(branchId => {
const branch = froca.getBranch(branchId); const branch = froca.getBranch(branchId);
if (!branch) {
return false;
}
return branch.noteId !== 'root' return branch.noteId !== 'root'
&& branch.noteId !== hoistedNoteId; && branch.noteId !== hoistedNoteId;
}); });
} }
function makeToast(id, message) { function makeToast(id: string, message: string): ToastOptions {
return { return {
id: id, id: id,
title: t("branches.delete-status"), title: t("branches.delete-status"),
@ -235,8 +255,8 @@ ws.subscribeToMessages(async message => {
} }
}); });
async function cloneNoteToBranch(childNoteId, parentBranchId, prefix) { async function cloneNoteToBranch(childNoteId: string, parentBranchId: string, prefix: string) {
const resp = await server.put(`notes/${childNoteId}/clone-to-branch/${parentBranchId}`, { const resp = await server.put<Response>(`notes/${childNoteId}/clone-to-branch/${parentBranchId}`, {
prefix: prefix prefix: prefix
}); });
@ -245,8 +265,8 @@ async function cloneNoteToBranch(childNoteId, parentBranchId, prefix) {
} }
} }
async function cloneNoteToParentNote(childNoteId, parentNoteId, prefix) { async function cloneNoteToParentNote(childNoteId: string, parentNoteId: string, prefix: string) {
const resp = await server.put(`notes/${childNoteId}/clone-to-note/${parentNoteId}`, { const resp = await server.put<Response>(`notes/${childNoteId}/clone-to-note/${parentNoteId}`, {
prefix: prefix prefix: prefix
}); });
@ -256,8 +276,8 @@ async function cloneNoteToParentNote(childNoteId, parentNoteId, prefix) {
} }
// beware that the first arg is noteId and the second is branchId! // beware that the first arg is noteId and the second is branchId!
async function cloneNoteAfter(noteId, afterBranchId) { async function cloneNoteAfter(noteId: string, afterBranchId: string) {
const resp = await server.put(`notes/${noteId}/clone-after/${afterBranchId}`); const resp = await server.put<Response>(`notes/${noteId}/clone-after/${afterBranchId}`);
if (!resp.success) { if (!resp.success) {
toastService.showError(resp.message); toastService.showError(resp.message);

View File

@ -12,7 +12,7 @@ interface Response {
brokenRelations: FAttributeRow[]; brokenRelations: FAttributeRow[];
} }
interface ResolveOptions { export interface ResolveOptions {
proceed: boolean; proceed: boolean;
deleteAllClones?: boolean; deleteAllClones?: boolean;
eraseNotes?: boolean; eraseNotes?: boolean;