Elffuss BraidHub

kikocis/continue public

main · 5.3 KiBRawPermalink
1import { createSlice, PayloadAction } from "@reduxjs/toolkit";
2import { SlashCommandDescWithSource } from "core";
3import { SerializedOrgWithProfiles } from "core/config/ProfileLifecycleManager";
4
5const DEFAULT_SLASH_COMMANDS_BOOKMARKS_COUNT = 5;
6
7const INITIAL_PREFERENCES_STATE: PreferencesState = {
8 bookmarkedSlashCommands: [],
9};
10
11export interface PreferencesState {
12 bookmarkedSlashCommands: string[];
13}
14
15export interface ProfilesState {
16 organizations: SerializedOrgWithProfiles[];
17 selectedProfileId: string | null;
18 selectedOrganizationId: string | null;
19 preferencesByProfileId: Record<string, PreferencesState>;
20}
21
22const initialState: ProfilesState = {
23 preferencesByProfileId: {},
24 selectedProfileId: null,
25 selectedOrganizationId: null,
26 organizations: [
27 {
28 id: "personal",
29 profiles: [
30 {
31 title: "Local Agent",
32 id: "local",
33 errors: [],
34 profileType: "local",
35 uri: "",
36 iconUrl: "",
37 fullSlug: {
38 ownerSlug: "",
39 packageSlug: "",
40 versionSlug: "",
41 },
42 },
43 ],
44 slug: "",
45 selectedProfileId: "local",
46 name: "Personal",
47 iconUrl: "",
48 },
49 ],
50};
51
52export const profilesSlice = createSlice({
53 name: "profiles",
54 initialState,
55 reducers: {
56 setSelectedProfile: (state, { payload }: PayloadAction<string | null>) => {
57 state.selectedProfileId = payload;
58 const currentOrg = state.organizations.find(
59 (o) => o.id === state.selectedOrganizationId,
60 );
61 if (currentOrg) {
62 currentOrg.selectedProfileId = payload;
63 }
64 },
65 setOrganizations: (
66 state,
67 { payload }: PayloadAction<SerializedOrgWithProfiles[]>,
68 ) => {
69 state.organizations = payload;
70 },
71 setSelectedOrgId: (state, { payload }: PayloadAction<string | null>) => {
72 state.selectedOrganizationId = payload;
73 const org = state.organizations.find((o) => o.id === payload);
74 if (org) {
75 state.selectedProfileId = org.selectedProfileId;
76 } else {
77 state.selectedProfileId = null;
78 }
79 },
80 initializeProfilePreferences: (
81 state,
82 action: PayloadAction<{
83 profileId: string;
84 defaultSlashCommands?: SlashCommandDescWithSource[];
85 }>,
86 ) => {
87 const { profileId, defaultSlashCommands = [] } = action.payload;
88 const defaultSlashCommandNames = defaultSlashCommands.map(
89 (cmd) => cmd.name,
90 );
91
92 // First ensure all profile preferences are complete to handle
93 // the case where a new preference has been added since last load
94 Object.keys(state.preferencesByProfileId).forEach((pid) => {
95 state.preferencesByProfileId[pid] = {
96 ...INITIAL_PREFERENCES_STATE,
97 ...state.preferencesByProfileId[pid],
98 };
99 });
100
101 // Then initialize preferences for the new profile if needed
102 if (!state.preferencesByProfileId[profileId]) {
103 state.preferencesByProfileId[profileId] = {
104 bookmarkedSlashCommands: defaultSlashCommandNames.slice(
105 0,
106 DEFAULT_SLASH_COMMANDS_BOOKMARKS_COUNT,
107 ),
108 };
109 }
110 },
111 bookmarkSlashCommand: (
112 state,
113 action: PayloadAction<{ commandName: string }>,
114 ) => {
115 const { commandName } = action.payload;
116 const preferences =
117 state.preferencesByProfileId[state.selectedProfileId ?? ""];
118
119 if (!preferences) return;
120
121 if (!preferences.bookmarkedSlashCommands.includes(commandName)) {
122 preferences.bookmarkedSlashCommands.push(commandName);
123 }
124 },
125 unbookmarkSlashCommand: (
126 state,
127 action: PayloadAction<{ commandName: string }>,
128 ) => {
129 const { commandName } = action.payload;
130 const profileId = state.selectedProfileId;
131
132 if (!profileId) return;
133
134 const preferences = state.preferencesByProfileId[profileId];
135 if (!preferences) return;
136
137 preferences.bookmarkedSlashCommands =
138 preferences.bookmarkedSlashCommands.filter(
139 (cmd) => cmd !== commandName,
140 );
141 },
142 },
143 selectors: {
144 selectSelectedProfile: (state) => {
145 const selectedOrg = state.organizations.find(
146 (org) => org.id === state.selectedOrganizationId,
147 );
148 if (selectedOrg) {
149 const profile = selectedOrg.profiles.find(
150 (profile) => profile.id === state.selectedProfileId,
151 );
152 return profile ?? null;
153 } else {
154 return null;
155 }
156 },
157
158 selectCurrentOrg: (state) => {
159 return (
160 state.organizations.find(
161 (org) => org.id === state.selectedOrganizationId,
162 ) ?? null
163 );
164 },
165
166 selectBookmarkedSlashCommands: (state) => {
167 if (!state.selectedProfileId) return [];
168 const preferences = state.preferencesByProfileId[state.selectedProfileId];
169 return preferences?.bookmarkedSlashCommands || [];
170 },
171
172 selectPreferencesByProfileId: (state) => state.preferencesByProfileId,
173 },
174});
175
176export const {
177 setSelectedProfile,
178 bookmarkSlashCommand,
179 unbookmarkSlashCommand,
180 initializeProfilePreferences,
181 setOrganizations,
182 setSelectedOrgId,
183} = profilesSlice.actions;
184
185export const {
186 selectSelectedProfile,
187 selectBookmarkedSlashCommands,
188 selectPreferencesByProfileId,
189 selectCurrentOrg,
190} = profilesSlice.selectors;
191
192export const { reducer: profilesReducer } = profilesSlice;