chore: added confirmation moda & handled multi selection of files

This commit is contained in:
Meesayen 2024-02-17 12:55:17 +01:00
parent 548bd2c3c0
commit 7023e43806
3 changed files with 44 additions and 5 deletions

View File

@ -1,4 +1,6 @@
<script lang="ts">
import Button from './Button.svelte';
import Modal from './Modal.svelte';
import PopupMenu from '$lib/components/PopupMenu.svelte';
import ContextMenu from '$lib/components/contextmenu/ContextMenu.svelte';
import ContextMenuItem from '$lib/components/contextmenu/ContextMenuItem.svelte';
@ -6,6 +8,7 @@
import type { BranchController } from '$lib/vbranches/branchController';
export let branchController: BranchController;
let confirmationModal: Modal;
let popupMenu: PopupMenu;
export function openByMouse(e: MouseEvent, item: any) {
@ -16,9 +19,43 @@
<PopupMenu bind:this={popupMenu} let:item>
<ContextMenu>
<ContextMenuSection>
{#if item.file !== undefined}
<ContextMenuItem label="Discard" on:click={() => branchController.unapplyFile(item.file)} />
{#if item.files !== undefined}
<ContextMenuItem label="Discard" on:click={() => confirmationModal.show(item)} />
{/if}
</ContextMenuSection>
</ContextMenu>
</PopupMenu>
<Modal width="small" title="Discard file" bind:this={confirmationModal} let:item>
<div>
Discarding changes to the following files:
<ul class="file-list">
{#each item.files as file}
<li><code>{file.path}</code></li>
{/each}
</ul>
</div>
<svelte:fragment slot="controls" let:close let:item>
<Button kind="outlined" color="neutral" on:click={close}>Cancel</Button>
<Button
color="error"
on:click={() => {
branchController.unapplyFiles(item.files);
confirmationModal.close();
}}
>
Confirm
</Button>
</svelte:fragment>
</Modal>
<style lang="postcss">
.file-list {
list-style: disc;
padding-left: var(--space-20);
padding-top: var(--space-6);
}
.file-list li {
padding: var(--space-2);
}
</style>

View File

@ -68,7 +68,7 @@
tabindex="0"
on:contextmenu={(e) =>
popupMenu.openByMouse(e, {
file
files: $selectedFiles.includes(file) ? $selectedFiles : [file]
})}
>
<div

View File

@ -150,11 +150,13 @@ export class BranchController {
}
}
async unapplyFile(file: LocalFile) {
async unapplyFiles(files: LocalFile[]) {
try {
await invoke<void>('unapply_ownership', {
projectId: this.projectId,
ownership: file.hunks.map((h) => `${file.path}:${h.id}`).join('\n')
ownership: files
.flatMap((f) => f.hunks.map((h) => `${f.path}:${h.id}`).join('\n'))
.join('\n')
});
} catch (err) {
toasts.error('Failed to unapply file changes');