diff --git a/src/routes/repo/[projectId]/BranchLane.svelte b/src/routes/repo/[projectId]/BranchLane.svelte index f3b9c7682..2a1b1e954 100644 --- a/src/routes/repo/[projectId]/BranchLane.svelte +++ b/src/routes/repo/[projectId]/BranchLane.svelte @@ -13,7 +13,6 @@ import IconGithub from '$lib/icons/IconGithub.svelte'; import { getExpandedWithCacheFallback, setExpandedWithCache } from './cache'; import type { VirtualBranchOperations } from './vbranches'; - import Modal from '$lib/components/Modal/Modal.svelte'; const dispatch = createEventDispatcher(); @@ -36,11 +35,7 @@ const newItems = e.detail.items; const fileItems = newItems.filter((item) => item instanceof File) as File[]; - let hunkIdsToMove = []; - - if (e.type == 'finalize') { - hunkIdsToMove.push(...fileItems.flatMap((item) => item.hunks.map((h) => h.id))); - } + console.log('handleDndEvent', e.type, e.detail.items); const hunkItems = newItems.filter((item) => item instanceof Hunk) as Hunk[]; hunkItems.forEach((hunk) => { @@ -50,24 +45,35 @@ } else { 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); if (e.type == 'finalize' && files.length == 0) dispatch('empty'); + if (e.type === 'finalize') updateBranchOwnership(); } - function handleEmpty() { - const emptyIndex = files.findIndex((item) => !item.hunks || item.hunks.length == 0); - if (emptyIndex != -1) { - files.splice(emptyIndex, 1); + function updateBranchOwnership() { + const ownership = files + .map((file) => file.id + ':' + file.hunks.map((hunk) => hunk.id.split(':')[1]).join(',')) + .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 { @@ -187,7 +193,9 @@ filepath={file.path} bind:expanded={file.expanded} bind:hunks={file.hunks} - on:empty={handleEmpty} + on:update={(e) => { + handleFileUpdate(file.id, e.detail); + }} on:expanded={(e) => { setExpandedWithCache(file, e.detail); expandFromCache(); diff --git a/src/routes/repo/[projectId]/FileCard.svelte b/src/routes/repo/[projectId]/FileCard.svelte index 771c116db..6c44f7674 100644 --- a/src/routes/repo/[projectId]/FileCard.svelte +++ b/src/routes/repo/[projectId]/FileCard.svelte @@ -12,13 +12,15 @@ export let hunks: Hunk[]; let zoneEl: HTMLElement; - const dispatch = createEventDispatcher(); + const dispatch = createEventDispatcher<{ + expanded: boolean; + update: Hunk[]; + }>(); export let expanded: boolean | undefined; function handleDndEvent(e: CustomEvent>) { hunks = e.detail.items; - hunks.sort((itemA, itemB) => compareDesc(itemA.modifiedAt, itemB.modifiedAt)); - if (e.type == 'finalize' && hunks.length == 0) dispatch('empty'); + if (e.type == 'finalize') dispatch('update', e.detail.items); } function hunkSize(hunk: string): number[] { diff --git a/src/routes/repo/[projectId]/vbranches.ts b/src/routes/repo/[projectId]/vbranches.ts index d54e387e2..d8c4f9d08 100644 --- a/src/routes/repo/[projectId]/vbranches.ts +++ b/src/routes/repo/[projectId]/vbranches.ts @@ -17,6 +17,7 @@ export interface VirtualBranchOperations { applyBranch(branchId: string): Promise; unapplyBranch(branchId: string): Promise; moveFiles(branchId: string, paths: Array): Promise; + updateBranchOwnership(branchId: string, ownership: string): Promise; } export function getVirtualBranches( @@ -36,7 +37,9 @@ export function getVirtualBranches( updateBranchName: (branchId, name) => updateBranchName(writeable, projectId, branchId, name), applyBranch: (branchId) => applyBranch(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); return store; @@ -55,7 +58,7 @@ function createWriteable(projectId: string) { .deltas({ projectId, sessionId: lastSession.id }) .subscribe(() => { list(projectId).then((newBranches) => { - set(sort(plainToInstance(Branch, newBranches))); + set(plainToInstance(Branch, newBranches)); }); return () => deltasUnsubscribe(); }); @@ -68,18 +71,9 @@ function refresh(projectId: string, store: Writable>) { 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 { return invoke>('list_virtual_branches', { projectId }).then((result) => - sort(plainToInstance(Branch, result)) + plainToInstance(Branch, result) ); } @@ -170,6 +164,26 @@ function unapplyBranch( }); } +function updateBranchOwnership( + writable: Writable>, + projectId: string, + branchId: string, + ownership: string +) { + return invoke('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( writable: Writable>, projectId: string,