diff --git a/app/src/lib/components/History.svelte b/app/src/lib/components/History.svelte index 069b14be3..a41ed9762 100644 --- a/app/src/lib/components/History.svelte +++ b/app/src/lib/components/History.svelte @@ -7,23 +7,20 @@ import ScrollableContainer from './ScrollableContainer.svelte'; import SnapshotCard from './SnapshotCard.svelte'; import emptyFolderSvg from '$lib/assets/empty-state/empty-folder.svg?raw'; - import { listen } from '$lib/backend/ipc'; import { Project } from '$lib/backend/projects'; import { clickOutside } from '$lib/clickOutside'; import { HistoryService, createdOnDay } from '$lib/history/history'; - import { persisted } from '$lib/persisted/persisted'; import { getContext } from '$lib/utils/context'; - import * as hotkeys from '$lib/utils/hotkeys'; import { RemoteFile } from '$lib/vbranches/types'; import { plainToInstance } from 'class-transformer'; - import { onMount } from 'svelte'; + import { createEventDispatcher } from 'svelte'; import type { Snapshot, SnapshotDiff } from '$lib/history/types'; const project = getContext(Project); const historyService = getContext(HistoryService); const snapshots = historyService.snapshots; + const dispatch = createEventDispatcher<{ hide: any }>(); - const showHistoryView = persisted(false, 'showHistoryView'); const loading = historyService.loading; let currentFilePreview: RemoteFile | undefined = undefined; @@ -49,142 +46,122 @@ }); } - onMount(() => { - const unsubscribe = listen('menu://view/history/clicked', () => { - $showHistoryView = !$showHistoryView; - }); - - // TODO: Refactor somehow - const unsubscribeHotkeys = hotkeys.on('$mod+Shift+H', () => { - $showHistoryView = !$showHistoryView; - }); - - return async () => { - unsubscribe(); - unsubscribeHotkeys(); - }; - }); - let snapshotFilesTempStore: | { entryId: string; diffs: { [key: string]: SnapshotDiff } } | undefined = undefined; let selectedFile: { entryId: string; path: string } | undefined = undefined; -{#if $showHistoryView} - diff --git a/app/src/lib/history/history.ts b/app/src/lib/history/history.ts index 1f34a4608..3415ccce4 100644 --- a/app/src/lib/history/history.ts +++ b/app/src/lib/history/history.ts @@ -6,18 +6,33 @@ import { writable } from 'svelte/store'; export class HistoryService { cursor: string | undefined = undefined; - snapshots = writable([], (set) => { - this.loadSnapshots().then((x) => set(x)); + readonly loading = writable(false); + readonly snapshots = writable([], (set) => { + // Load snapshots when going from 0 -> 1 subscriber. + this.fetch().then((x) => set(x)); return () => { + // Clear store when component last subscriber unsubscribes. set([]); this.cursor = undefined; }; }); - loading = writable(false); constructor(private projectId: string) {} - async loadSnapshots(after?: string) { + async load() { + if (this.cursor) this.cursor = undefined; + this.snapshots.set(await this.fetch()); + this.loading.set(false); + } + + async loadMore() { + if (!this.cursor) throw new Error('Unable to load more without a cursor'); + const more = await this.fetch(this.cursor); + // TODO: Update API so we don't have to .slice() + this.snapshots.update((snapshots) => [...snapshots, ...more.slice(1)]); + } + + private async fetch(after?: string) { this.loading.set(true); const resp = await invoke('list_snapshots', { projectId: this.projectId, @@ -29,10 +44,8 @@ export class HistoryService { return plainToInstance(Snapshot, resp); } - async loadMore() { - if (!this.cursor) throw new Error('Unable to load more without a cursor'); - const more = await this.loadSnapshots(this.cursor); - this.snapshots.update((snapshots) => [...snapshots, ...more.slice(1)]); + clear() { + this.snapshots.set([]); } async getSnapshotDiff(projectId: string, sha: string) { diff --git a/app/src/routes/[projectId]/+layout.svelte b/app/src/routes/[projectId]/+layout.svelte index a3e91229d..b1e42a568 100644 --- a/app/src/routes/[projectId]/+layout.svelte +++ b/app/src/routes/[projectId]/+layout.svelte @@ -1,4 +1,5 @@ @@ -81,8 +102,10 @@ {:else if $baseBranch}
+ {#if $showHistoryView} + ($showHistoryView = false)} /> + {/if} -
{/if} {/key}