-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3a20f4
commit 1f37d10
Showing
11 changed files
with
382 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/shared/src/lib/branches/branchesPreview.svelte.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { branchesSelectors } from '$lib/branches/branchesSlice'; | ||
import { BranchStatus, type LoadableBranch } from '$lib/branches/types'; | ||
import { registerInterest, type InView } from '$lib/interest/registerInterestFunction.svelte'; | ||
import type { BranchService } from '$lib/branches/branchService'; | ||
import type { AppBranchesState } from '$lib/redux/store.svelte'; | ||
import type { Reactive } from '$lib/storeUtils'; | ||
|
||
export function getBranchReviews( | ||
appState: AppBranchesState, | ||
branchService: BranchService, | ||
repositoryId: string, | ||
status: BranchStatus = BranchStatus.All, | ||
inView?: InView | ||
): Reactive<LoadableBranch[]> { | ||
const branchReviewsInterest = branchService.getBranchesInterest(repositoryId, status); | ||
registerInterest(branchReviewsInterest, inView); | ||
|
||
const branchReviews = $derived(branchesSelectors.selectAll(appState.branches)); | ||
|
||
return { | ||
get current() { | ||
return branchReviews; | ||
} | ||
}; | ||
} | ||
|
||
export function getBranchReview( | ||
appState: AppBranchesState, | ||
branchService: BranchService, | ||
repositoryId: string, | ||
branchId: string, | ||
inView?: InView | ||
): Reactive<LoadableBranch | undefined> { | ||
const branchReviewInterest = branchService.getBranchInterest(repositoryId, branchId); | ||
registerInterest(branchReviewInterest, inView); | ||
|
||
const branchReview = $derived(branchesSelectors.selectById(appState.branches, branchId)); | ||
|
||
return { | ||
get current() { | ||
return branchReview; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { createEntityAdapter, createSlice } from '@reduxjs/toolkit'; | ||
import type { Section } from '$lib/branches/types'; | ||
|
||
const patchSectionsAdapter = createEntityAdapter<Section, Section['id']>({ | ||
selectId: (patchSection: Section) => patchSection.id | ||
}); | ||
|
||
const patchSectionsSlice = createSlice({ | ||
name: 'patchSectionSections', | ||
initialState: patchSectionsAdapter.getInitialState(), | ||
reducers: { | ||
addPatchSection: patchSectionsAdapter.addOne, | ||
addPatchSections: patchSectionsAdapter.addMany, | ||
removePatchSection: patchSectionsAdapter.removeOne, | ||
removePatchSections: patchSectionsAdapter.removeMany, | ||
upsertPatchSection: patchSectionsAdapter.upsertOne, | ||
upsertPatchSections: patchSectionsAdapter.upsertMany | ||
} | ||
}); | ||
|
||
export const patchSectionsReducer = patchSectionsSlice.reducer; | ||
|
||
export const patchSectionsSelectors = patchSectionsAdapter.getSelectors(); | ||
export const { | ||
addPatchSection, | ||
addPatchSections, | ||
removePatchSection, | ||
removePatchSections, | ||
upsertPatchSection, | ||
upsertPatchSections | ||
} = patchSectionsSlice.actions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { upsertPatchSections } from '$lib/branches/patchSectionsSlice'; | ||
import { addPatch, upsertPatch } from '$lib/branches/patchesSlice'; | ||
import { apiToPatch, apiToSection, type ApiPatch, type Patch } from '$lib/branches/types'; | ||
import { InterestStore, type Interest } from '$lib/interest/intrestStore'; | ||
import { errorToLoadable } from '$lib/network/loadable'; | ||
import { POLLING_REGULAR } from '$lib/polling'; | ||
import type { HttpClient } from '$lib/network/httpClient'; | ||
import type { AppDispatch } from '$lib/redux/store.svelte'; | ||
|
||
type PatchUpdateParams = { | ||
signOff?: boolean; | ||
sectionOrder?: string[]; | ||
}; | ||
|
||
export class PatchService { | ||
private readonly patchInterests = new InterestStore<{ changeId: string }>(POLLING_REGULAR); | ||
|
||
constructor( | ||
private readonly httpClient: HttpClient, | ||
private readonly appDispatch: AppDispatch | ||
) {} | ||
|
||
getPatchWithSectionsInterest(branchId: string, changeId: string): Interest { | ||
return this.patchInterests | ||
.findOrCreateSubscribable({ changeId }, async () => { | ||
this.appDispatch.dispatch(addPatch({ status: 'loading', id: changeId })); | ||
try { | ||
const apiPatch = await this.httpClient.get<ApiPatch>( | ||
`patch_stack/${branchId}/patch/${changeId}` | ||
); | ||
|
||
const patch = apiToPatch(apiPatch); | ||
|
||
// This will always be here, but this makes the typescript | ||
// compiler happy | ||
if (apiPatch.sections) { | ||
const sections = apiPatch.sections.map(apiToSection); | ||
this.appDispatch.dispatch(upsertPatchSections(sections)); | ||
} | ||
|
||
this.appDispatch.dispatch(upsertPatch({ status: 'found', id: changeId, value: patch })); | ||
} catch (error: unknown) { | ||
this.appDispatch.dispatch(upsertPatch(errorToLoadable(error, changeId))); | ||
} | ||
}) | ||
.createInterest(); | ||
} | ||
|
||
async updatePatch(branchId: string, changeId: string, params: PatchUpdateParams): Promise<Patch> { | ||
const apiPatch = await this.httpClient.patch<ApiPatch>( | ||
`patch_stack/${branchId}/patch/${changeId}`, | ||
{ | ||
body: { | ||
sign_off: params.signOff, | ||
section_order: params.sectionOrder | ||
} | ||
} | ||
); | ||
|
||
const patch = apiToPatch(apiPatch); | ||
this.appDispatch.dispatch(upsertPatch({ status: 'found', id: changeId, value: patch })); | ||
|
||
// This will always be here, but this makes the typescript | ||
// compiler happy | ||
if (apiPatch.sections) { | ||
const sections = apiPatch.sections.map(apiToSection); | ||
this.appDispatch.dispatch(upsertPatchSections(sections)); | ||
} | ||
|
||
return patch; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { patchSectionsSelectors } from '$lib/branches/patchSectionsSlice'; | ||
import { patchesSelectors } from '$lib/branches/patchesSlice'; | ||
import { registerInterest, type InView } from '$lib/interest/registerInterestFunction.svelte'; | ||
import type { PatchService } from '$lib/branches/patchService'; | ||
import type { LoadablePatch, Section } from '$lib/branches/types'; | ||
import type { AppPatchesState, AppPatchSectionsState } from '$lib/redux/store.svelte'; | ||
import type { Reactive } from '$lib/storeUtils'; | ||
|
||
export function getPatch( | ||
appState: AppPatchesState, | ||
patchService: PatchService, | ||
branchId: string, | ||
changeId: string, | ||
inView?: InView | ||
): Reactive<LoadablePatch | undefined> { | ||
const patchInterest = patchService.getPatchWithSectionsInterest(branchId, changeId); | ||
registerInterest(patchInterest, inView); | ||
|
||
const patch = $derived(patchesSelectors.selectById(appState.patches, branchId)); | ||
|
||
return { | ||
get current() { | ||
return patch; | ||
} | ||
}; | ||
} | ||
|
||
export function getPatchSections( | ||
appState: AppPatchesState & AppPatchSectionsState, | ||
patchService: PatchService, | ||
branchId: string, | ||
changeId: string, | ||
inView?: InView | ||
): Reactive<Section[] | undefined> { | ||
const patchInterest = patchService.getPatchWithSectionsInterest(branchId, changeId); | ||
registerInterest(patchInterest, inView); | ||
|
||
const patch = $derived(patchesSelectors.selectById(appState.patches, branchId)); | ||
const sections = $derived.by(() => { | ||
if (patch?.status !== 'found') return; | ||
|
||
return (patch.value.sectionIds || []) | ||
.map((id) => patchSectionsSelectors.selectById(appState.patchSections, id)) | ||
.filter((a) => a) as Section[]; | ||
}); | ||
|
||
return { | ||
get current() { | ||
return sections; | ||
} | ||
}; | ||
} |
Oops, something went wrong.