Fetch button (#610)

* when not behind, it fetches

* hitting refresh fetches from remote
This commit is contained in:
Scott Chacon 2023-07-04 07:15:44 +02:00 committed by GitHub
parent dab665531d
commit 0a3f9b6adc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 9 deletions

View File

@ -472,6 +472,16 @@ impl App {
Ok(())
}
pub fn fetch_from_target(&self, project_id: &str) -> Result<()> {
let project = self.gb_project(project_id)?;
let project_repository = project_repository::Repository::open(&project)?;
project_repository
.fetch()
.context("failed to fetch from target")?;
Ok(())
}
pub fn upsert_bookmark(&self, bookmark: &bookmarks::Bookmark) -> Result<()> {
let gb_repository = self.gb_repository(&bookmark.project_id)?;
let writer = bookmarks::Writer::new(&gb_repository).context("failed to open writer")?;

View File

@ -676,6 +676,14 @@ async fn push_virtual_branch(
Ok(())
}
#[timed(duration(printer = "debug!"))]
#[tauri::command(async)]
async fn fetch_from_target(handle: tauri::AppHandle, project_id: &str) -> Result<(), Error> {
let app = handle.state::<app::App>();
app.fetch_from_target(project_id)?;
Ok(())
}
#[timed(duration(printer = "debug!"))]
#[tauri::command(async)]
async fn get_target_data(
@ -883,6 +891,7 @@ fn main() {
unapply_branch,
push_virtual_branch,
create_virtual_branch_from_branch,
fetch_from_target,
])
.build(tauri_context)
.expect("Failed to build tauri app")

View File

@ -11,6 +11,8 @@
import PopupMenuItem from '$lib/components/PopupMenu/PopupMenuItem.svelte';
import { getRemoteBranches } from './remoteBranches';
import { Value } from 'svelte-loadable-store';
import { get } from 'lscache';
import { invoke } from '@tauri-apps/api';
export let target: Target;
export let branches: Branch[];
@ -49,6 +51,17 @@
});
}
async function getTargetData(params: { projectId: string }) {
target = await invoke<Target>('get_target_data', params);
}
function fetchTarget() {
remoteBranchOperations.fetchFromTarget().then(() => {
virtualBranches.refresh();
});
getTargetData({ projectId });
}
function rememberWidth(node: HTMLElement) {
const cachedWidth = localStorage.getItem(cacheKey);
if (cachedWidth) node.style.width = cachedWidth;
@ -79,14 +92,24 @@
<div class="flex-grow text-lg font-bold" title={behindMessage}>{target.name}</div>
<div>{target.behind > 0 ? `behind ${target.behind}` : 'up-to-date'}</div>
<div class="flex-shrink-0 text-light-700 dark:text-dark-100" title={behindMessage}>
<button
class="p-1 disabled:text-light-300 disabled:dark:text-dark-300"
on:click={updateTargetModal.show}
disabled={target.behind == 0}
title={target.behind > 0 ? 'click to update target' : 'already up-to-date'}
>
<IconRefresh />
</button>
{#if target.behind == 0}
<button
class="p-1 disabled:text-light-300 disabled:dark:text-dark-300"
on:click={fetchTarget}
title="click to fetch"
>
<IconRefresh />
</button>
{:else}
<button
class="p-1 disabled:text-light-300 disabled:dark:text-dark-300"
on:click={updateTargetModal.show}
disabled={target.behind == 0}
title={target.behind > 0 ? 'click to update target' : 'already up-to-date'}
>
<IconRefresh />
</button>
{/if}
</div>
</div>

View File

@ -12,6 +12,7 @@ export interface RemoteBranchOperations {
refresh(): Promise<void | object>;
updateBranchTarget(): Promise<void | object>;
createvBranchFromBranch(branch: string): Promise<void | string>;
fetchFromTarget(): Promise<void | object>;
}
export function getRemoteBranches(
@ -26,7 +27,8 @@ export function getRemoteBranches(
refresh: () => refresh(projectId, writeable),
subscribe: writeable.subscribe,
updateBranchTarget: () => updateBranchTarget(writeable, projectId),
createvBranchFromBranch: (branch) => createvBranchFromBranch(writeable, projectId, branch)
createvBranchFromBranch: (branch) => createvBranchFromBranch(writeable, projectId, branch),
fetchFromTarget: () => fetchFromTarget(writeable, projectId)
};
cache.set(projectId, store);
@ -53,6 +55,16 @@ function updateBranchTarget(writable: Writable<Loadable<BranchData[]>>, projectI
});
}
function fetchFromTarget(writable: Writable<Loadable<BranchData[]>>, projectId: string) {
return invoke<void>('fetch_from_target', { projectId: projectId })
.then(() => {
refresh(projectId, writable);
})
.catch(() => {
error('Unable to fetch from upstream!');
});
}
function refresh(projectId: string, store: Writable<Loadable<BranchData[]>>) {
return getRemoteBranchesData(projectId).then((newBranches) =>
store.set({ isLoading: false, value: newBranches })