send full branch updates instead of atomic moves

This commit is contained in:
Nikita Galaiko 2023-06-29 11:22:51 +02:00
parent df935248c0
commit 4120ea4e56
3 changed files with 57 additions and 33 deletions

View File

@ -13,7 +13,6 @@
import IconGithub from '$lib/icons/IconGithub.svelte'; import IconGithub from '$lib/icons/IconGithub.svelte';
import { getExpandedWithCacheFallback, setExpandedWithCache } from './cache'; import { getExpandedWithCacheFallback, setExpandedWithCache } from './cache';
import type { VirtualBranchOperations } from './vbranches'; import type { VirtualBranchOperations } from './vbranches';
import Modal from '$lib/components/Modal/Modal.svelte';
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
@ -36,11 +35,7 @@
const newItems = e.detail.items; const newItems = e.detail.items;
const fileItems = newItems.filter((item) => item instanceof File) as File[]; const fileItems = newItems.filter((item) => item instanceof File) as File[];
let hunkIdsToMove = []; console.log('handleDndEvent', e.type, e.detail.items);
if (e.type == 'finalize') {
hunkIdsToMove.push(...fileItems.flatMap((item) => item.hunks.map((h) => h.id)));
}
const hunkItems = newItems.filter((item) => item instanceof Hunk) as Hunk[]; const hunkItems = newItems.filter((item) => item instanceof Hunk) as Hunk[];
hunkItems.forEach((hunk) => { hunkItems.forEach((hunk) => {
@ -50,24 +45,35 @@
} else { } else {
fileItems.push(createFile(hunk.filePath, hunk)); fileItems.push(createFile(hunk.filePath, hunk));
} }
hunkIdsToMove.push(hunk.id);
}); });
if (hunkIdsToMove.length > 0) virtualBranches.moveFiles(branchId, hunkIdsToMove);
files = fileItems.filter((file) => file.hunks && file.hunks.length > 0); files = fileItems.filter((file) => file.hunks && file.hunks.length > 0);
if (e.type == 'finalize' && files.length == 0) dispatch('empty'); if (e.type == 'finalize' && files.length == 0) dispatch('empty');
if (e.type === 'finalize') updateBranchOwnership();
} }
function handleEmpty() { function updateBranchOwnership() {
const emptyIndex = files.findIndex((item) => !item.hunks || item.hunks.length == 0); const ownership = files
if (emptyIndex != -1) { .map((file) => file.id + ':' + file.hunks.map((hunk) => hunk.id.split(':')[1]).join(','))
files.splice(emptyIndex, 1); .join('\n');
virtualBranches.updateBranchOwnership(branchId, ownership);
}
function handleFileUpdate(fileId: string, hunks: Hunk[]) {
const fileIndex = files.findIndex((f) => f.id == fileId);
console.log(fileIndex, fileId, hunks);
if (fileIndex == -1) {
return;
} else {
if (hunks.length === 0) {
files.splice(fileIndex, 1);
if (files.length === 0) dispatch('empty');
} else {
files[fileIndex].hunks = hunks;
}
files = files;
updateBranchOwnership();
} }
if (files.length == 0) {
dispatch('empty');
}
files = files;
} }
function updateTextArea(): void { function updateTextArea(): void {
@ -187,7 +193,9 @@
filepath={file.path} filepath={file.path}
bind:expanded={file.expanded} bind:expanded={file.expanded}
bind:hunks={file.hunks} bind:hunks={file.hunks}
on:empty={handleEmpty} on:update={(e) => {
handleFileUpdate(file.id, e.detail);
}}
on:expanded={(e) => { on:expanded={(e) => {
setExpandedWithCache(file, e.detail); setExpandedWithCache(file, e.detail);
expandFromCache(); expandFromCache();

View File

@ -12,13 +12,15 @@
export let hunks: Hunk[]; export let hunks: Hunk[];
let zoneEl: HTMLElement; let zoneEl: HTMLElement;
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher<{
expanded: boolean;
update: Hunk[];
}>();
export let expanded: boolean | undefined; export let expanded: boolean | undefined;
function handleDndEvent(e: CustomEvent<DndEvent<Hunk>>) { function handleDndEvent(e: CustomEvent<DndEvent<Hunk>>) {
hunks = e.detail.items; hunks = e.detail.items;
hunks.sort((itemA, itemB) => compareDesc(itemA.modifiedAt, itemB.modifiedAt)); if (e.type == 'finalize') dispatch('update', e.detail.items);
if (e.type == 'finalize' && hunks.length == 0) dispatch('empty');
} }
function hunkSize(hunk: string): number[] { function hunkSize(hunk: string): number[] {

View File

@ -17,6 +17,7 @@ export interface VirtualBranchOperations {
applyBranch(branchId: string): Promise<void | object>; applyBranch(branchId: string): Promise<void | object>;
unapplyBranch(branchId: string): Promise<void | object>; unapplyBranch(branchId: string): Promise<void | object>;
moveFiles(branchId: string, paths: Array<string>): Promise<void | object>; moveFiles(branchId: string, paths: Array<string>): Promise<void | object>;
updateBranchOwnership(branchId: string, ownership: string): Promise<void | object>;
} }
export function getVirtualBranches( export function getVirtualBranches(
@ -36,7 +37,9 @@ export function getVirtualBranches(
updateBranchName: (branchId, name) => updateBranchName(writeable, projectId, branchId, name), updateBranchName: (branchId, name) => updateBranchName(writeable, projectId, branchId, name),
applyBranch: (branchId) => applyBranch(writeable, projectId, branchId), applyBranch: (branchId) => applyBranch(writeable, projectId, branchId),
unapplyBranch: (branchId) => unapplyBranch(writeable, projectId, branchId), unapplyBranch: (branchId) => unapplyBranch(writeable, projectId, branchId),
moveFiles: (branchId, paths) => moveFiles(writeable, projectId, branchId, paths) moveFiles: (branchId, paths) => moveFiles(writeable, projectId, branchId, paths),
updateBranchOwnership: (branchId, ownership) =>
updateBranchOwnership(writeable, projectId, branchId, ownership)
}; };
cache.set(projectId, store); cache.set(projectId, store);
return store; return store;
@ -55,7 +58,7 @@ function createWriteable(projectId: string) {
.deltas({ projectId, sessionId: lastSession.id }) .deltas({ projectId, sessionId: lastSession.id })
.subscribe(() => { .subscribe(() => {
list(projectId).then((newBranches) => { list(projectId).then((newBranches) => {
set(sort(plainToInstance(Branch, newBranches))); set(plainToInstance(Branch, newBranches));
}); });
return () => deltasUnsubscribe(); return () => deltasUnsubscribe();
}); });
@ -68,18 +71,9 @@ function refresh(projectId: string, store: Writable<Loadable<Branch[]>>) {
list(projectId).then((newBranches) => store.set({ isLoading: false, value: newBranches })); list(projectId).then((newBranches) => store.set({ isLoading: false, value: newBranches }));
} }
function sort(branches: Branch[]): Branch[] {
for (const branch of branches) {
for (const file of branch.files) {
file.hunks.sort((a, b) => b.modifiedAt.getTime() - a.modifiedAt.getTime());
}
}
return branches;
}
async function list(projectId: string): Promise<Branch[]> { async function list(projectId: string): Promise<Branch[]> {
return invoke<Array<Branch>>('list_virtual_branches', { projectId }).then((result) => return invoke<Array<Branch>>('list_virtual_branches', { projectId }).then((result) =>
sort(plainToInstance(Branch, result)) plainToInstance(Branch, result)
); );
} }
@ -170,6 +164,26 @@ function unapplyBranch(
}); });
} }
function updateBranchOwnership(
writable: Writable<Loadable<Branch[]>>,
projectId: string,
branchId: string,
ownership: string
) {
return invoke<object>('update_virtual_branch', {
projectId: projectId,
branch: { id: branchId, ownership }
})
.then((res) => {
console.log(res);
refresh(projectId, writable);
})
.catch((err) => {
console.log(err);
error('Unable to update branch!');
});
}
function updateBranchName( function updateBranchName(
writable: Writable<Loadable<Branch[]>>, writable: Writable<Loadable<Branch[]>>,
projectId: string, projectId: string,