Extracted js normalize branch name into a service

This commit is contained in:
Caleb Owens 2024-07-16 11:34:15 +02:00
parent b888fbc834
commit 8f01e64301
No known key found for this signature in database
4 changed files with 34 additions and 14 deletions

View File

@ -1,10 +1,10 @@
<script lang="ts">
import { BaseBranch } from '$lib/baseBranch/baseBranch';
import { getNameNormalizationServiceContext } from '$lib/branches/nameNormalizationService';
import Button from '$lib/shared/Button.svelte';
import { getContextStore } from '$lib/utils/context';
import { openExternalUrl } from '$lib/utils/url';
import { Branch } from '$lib/vbranches/types';
import { invoke } from '@tauri-apps/api/tauri';
export let isUnapplied = false;
export let hasIntegratedCommits = false;
@ -14,16 +14,15 @@
const baseBranch = getContextStore(BaseBranch);
const branch = getContextStore(Branch);
async function normalizeBranchName() {
return await invoke('normalize_branch_name', { name: $branch.displayName });
}
const nameNormalizationService = getNameNormalizationServiceContext();
let normalizedBranchName: string;
$: if ($branch.displayName) {
normalizeBranchName()
nameNormalizationService
.normalize($branch.displayName)
.then((name) => {
normalizedBranchName = name as string;
normalizedBranchName = name;
})
.catch((e) => {
console.error('Failed to normalize branch name', e);

View File

@ -1,6 +1,7 @@
<script lang="ts">
import { AIService } from '$lib/ai/service';
import { Project } from '$lib/backend/projects';
import { getNameNormalizationServiceContext } from '$lib/branches/nameNormalizationService';
import ContextMenu from '$lib/components/contextmenu/ContextMenu.svelte';
import ContextMenuItem from '$lib/components/contextmenu/ContextMenuItem.svelte';
import ContextMenuSection from '$lib/components/contextmenu/ContextMenuSection.svelte';
@ -15,7 +16,6 @@
import { getContext, getContextStore } from '$lib/utils/context';
import { BranchController } from '$lib/vbranches/branchController';
import { Branch, type NameConflictResolution } from '$lib/vbranches/types';
import { invoke } from '@tauri-apps/api/tauri';
export let contextMenuEl: ContextMenu;
export let target: HTMLElement;
@ -29,6 +29,8 @@
const aiGenEnabled = projectAiGenEnabled(project.id);
const branchController = getContext(BranchController);
const nameNormalizationService = getNameNormalizationServiceContext();
let aiConfigurationValid = false;
let deleteBranchModal: Modal;
let renameRemoteModal: Modal;
@ -110,17 +112,13 @@
}
}
// Normalize branch name
async function normalizeBranchName() {
return await invoke('normalize_branch_name', { name: branch.name });
}
let normalizedBranchName: string;
$: if (branch.name) {
normalizeBranchName()
nameNormalizationService
.normalize(branch.name)
.then((name) => {
normalizedBranchName = name as string;
normalizedBranchName = name;
})
.catch((e) => {
console.error('Failed to normalize branch name', e);

View File

@ -0,0 +1,17 @@
import { invoke as ipcInvoke } from '$lib/backend/ipc';
import { buildContext } from '$lib/utils/context';
export interface NameNormalizationService {
normalize(branchName: string): Promise<string>;
}
export class IpcNameNormalizationService implements NameNormalizationService {
constructor(private invoke: typeof ipcInvoke) {}
async normalize(branchName: string): Promise<string> {
return await this.invoke('normalize_branch_name', { name: branchName });
}
}
export const [getNameNormalizationServiceContext, setNameNormalizationServiceContext] =
buildContext<NameNormalizationService>('Name normalization service');

View File

@ -6,9 +6,14 @@
import { AuthService } from '$lib/backend/auth';
import { GitConfigService } from '$lib/backend/gitConfigService';
import { HttpClient } from '$lib/backend/httpClient';
import { invoke } from '$lib/backend/ipc';
import { ProjectService } from '$lib/backend/projects';
import { PromptService } from '$lib/backend/prompt';
import { UpdaterService } from '$lib/backend/updater';
import {
IpcNameNormalizationService,
setNameNormalizationServiceContext
} from '$lib/branches/nameNormalizationService';
import AppUpdater from '$lib/components/AppUpdater.svelte';
import GlobalSettingsMenuAction from '$lib/components/GlobalSettingsMenuAction.svelte';
import PromptModal from '$lib/components/PromptModal.svelte';
@ -54,6 +59,7 @@
setContext(RemotesService, data.remotesService);
setContext(AIPromptService, data.aiPromptService);
setContext(LineManagerFactory, data.lineManagerFactory);
setNameNormalizationServiceContext(new IpcNameNormalizationService(invoke));
const user = data.userService.user;
const accessToken = $derived($user?.github_access_token);