Skip to content

Commit

Permalink
Add slices
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb-T-Owens committed Jan 9, 2025
1 parent b3efa53 commit 04cf028
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
4 changes: 4 additions & 0 deletions apps/desktop/src/lib/redux/store.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { branchesReducer } from '@gitbutler/shared/branches/branchesSlice';
import { patchesReducer } from '@gitbutler/shared/branches/patchesSlice';
import { feedsReducer } from '@gitbutler/shared/feeds/feedsSlice';
import { postsReducer } from '@gitbutler/shared/feeds/postsSlice';
import { organizationsReducer } from '@gitbutler/shared/organizations/organizationsSlice';
Expand Down Expand Up @@ -55,6 +57,8 @@ export class DesktopState extends AppState implements AppDesktopOnlyState {
orgnaizations: organizationsReducer,
users: usersReducer,
projects: projectsReducer,
branches: branchesReducer,
patches: patchesReducer,
desktopOnly: desktopOnly.reducer
}
});
Expand Down
32 changes: 32 additions & 0 deletions packages/shared/src/lib/branches/branchesSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { loadableUpsert, loadableUpsertMany } from '$lib/network/loadable';
import { createEntityAdapter, createSlice } from '@reduxjs/toolkit';
import type { LoadableBranch } from '$lib/branches/types';

const branchesAdapter = createEntityAdapter<LoadableBranch, LoadableBranch['id']>({
selectId: (branch: LoadableBranch) => branch.id
});

const branchesSlice = createSlice({
name: 'branches',
initialState: branchesAdapter.getInitialState(),
reducers: {
addBranch: branchesAdapter.addOne,
addBranches: branchesAdapter.addMany,
removeBranch: branchesAdapter.removeOne,
removeBranches: branchesAdapter.removeMany,
upsertBranch: loadableUpsert(branchesAdapter),
upsertBranches: loadableUpsertMany(branchesAdapter)
}
});

export const branchesReducer = branchesSlice.reducer;

export const branchesSelectors = branchesAdapter.getSelectors();
export const {
addBranch,
addBranches,
removeBranch,
removeBranches,
upsertBranch,
upsertBranches
} = branchesSlice.actions;
26 changes: 26 additions & 0 deletions packages/shared/src/lib/branches/patchesSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { loadableUpsert, loadableUpsertMany } from '$lib/network/loadable';
import { createEntityAdapter, createSlice } from '@reduxjs/toolkit';
import type { LoadablePatch } from '$lib/branches/types';

const patchesAdapter = createEntityAdapter<LoadablePatch, LoadablePatch['id']>({
selectId: (patch: LoadablePatch) => patch.id
});

const patchesSlice = createSlice({
name: 'patches',
initialState: patchesAdapter.getInitialState(),
reducers: {
addPatch: patchesAdapter.addOne,
addPatches: patchesAdapter.addMany,
removePatch: patchesAdapter.removeOne,
removePatches: patchesAdapter.removeMany,
upsertPatch: loadableUpsert(patchesAdapter),
upsertPatches: loadableUpsertMany(patchesAdapter)
}
});

export const patchesReducer = patchesSlice.reducer;

export const patchesSelectors = patchesAdapter.getSelectors();
export const { addPatch, addPatches, removePatch, removePatches, upsertPatch, upsertPatches } =
patchesSlice.actions;
6 changes: 6 additions & 0 deletions packages/shared/src/lib/branches/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { LoadableData } from '$lib/network/types';

export type ApiPatchStatistics = {
file_count: number;
section_count: number;
Expand Down Expand Up @@ -72,6 +74,8 @@ export type Patch = {
reviewAll: PatchReview;
};

export type LoadablePatch = LoadableData<Patch, Patch['changeId']>;

export function apiToPatch(api: ApiPatch): Patch {
return {
changeId: api.change_id,
Expand Down Expand Up @@ -123,6 +127,8 @@ export type Branch = {
patch_ids: string[];
};

export type LoadableBranch = LoadableData<Branch, Branch['branchId']>;

export function apiToBranch(api: ApiBranch): Branch {
return {
branchId: api.branch_id,
Expand Down
28 changes: 26 additions & 2 deletions packages/shared/src/lib/redux/store.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { branchesReducer } from '$lib/branches/branchesSlice';
import { patchesReducer } from '$lib/branches/patchesSlice';
import { feedsReducer } from '$lib/feeds/feedsSlice';
import { postsReducer } from '$lib/feeds/postsSlice';
import { organizationsReducer } from '$lib/organizations/organizationsSlice';
Expand Down Expand Up @@ -34,6 +36,14 @@ export interface AppProjectsState {
readonly projects: ReturnType<typeof projectsReducer>;
}

export interface AppPatchesState {
readonly patches: ReturnType<typeof patchesReducer>;
}

export interface AppBranchesState {
readonly branches: ReturnType<typeof branchesReducer>;
}

export class AppDispatch {
constructor(readonly dispatch: typeof AppState.prototype._store.dispatch) {}
}
Expand All @@ -45,7 +55,9 @@ export class AppState
AppFeedsState,
AppOrganizationsState,
AppUsersState,
AppProjectsState
AppProjectsState,
AppPatchesState,
AppBranchesState
{
/**
* The base store.
Expand All @@ -60,7 +72,9 @@ export class AppState
feeds: feedsReducer,
orgnaizations: organizationsReducer,
users: usersReducer,
projects: projectsReducer
projects: projectsReducer,
patches: patchesReducer,
branches: branchesReducer
}
});

Expand Down Expand Up @@ -90,13 +104,23 @@ export class AppState
[this.selectSelf],
(rootState) => rootState.projects
);
private readonly selectPatches = createSelector(
[this.selectSelf],
(rootState) => rootState.patches
);
private readonly selectBranches = createSelector(
[this.selectSelf],
(rootState) => rootState.branches
);

readonly example = $derived(this.selectExample(this.rootState));
readonly posts = $derived(this.selectPosts(this.rootState));
readonly feeds = $derived(this.selectFeeds(this.rootState));
readonly organizations = $derived(this.selectOrganizations(this.rootState));
readonly users = $derived(this.selectUsers(this.rootState));
readonly projects = $derived(this.selectProjects(this.rootState));
readonly patches = $derived(this.selectPatches(this.rootState));
readonly branches = $derived(this.selectBranches(this.rootState));

constructor() {
$effect(() => {
Expand Down

0 comments on commit 04cf028

Please sign in to comment.