Skip to content

Commit f214145

Browse files
committed
update tab zustand store based on tab id
1 parent 8716e51 commit f214145

3 files changed

Lines changed: 16 additions & 14 deletions

File tree

src/components/molecules/environment/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const Env = ({ tab }) => {
3333
});
3434

3535
return (
36-
<div className='p-4'>
36+
<div className='p-4' key={tab.id}>
3737
<table className='w-full leading-normal'>
3838
<thead>
3939
<tr className='bg-ghost-50 text-ghost-600 text-left text-xs font-bold uppercase tracking-wider'>

src/stores/EnvStore.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ const useEnvStore = create((set, get) => ({
88
},
99
handleAddVariable: (key, value) => {
1010
set((state) => ({ variables: { ...state.variables, [key]: value } }));
11-
useTabStore.getState().updateEnvTab(get().variables);
11+
useTabStore.getState().updateEnvTab(useTabStore.getState().focusTabId, get().variables);
1212
},
1313
handleDeleteVariable: (key) => {
1414
const { [key]: _, ...newVariables } = get().variables;
1515
set({ variables: newVariables });
16-
useTabStore.getState().updateEnvTab(get().variables);
16+
useTabStore.getState().updateEnvTab(useTabStore.getState().focusTabId, get().variables);
1717
},
1818
}));
1919

src/stores/TabStore.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,20 @@ export const useTabStore = create((set, get) => ({
142142
set((state) => ({ tabs: [...state.tabs, newTab] }));
143143
set(() => ({ focusTabId: newTab.id }));
144144
},
145-
// these state changes are meant to be triggered by env tab in focus
146-
updateEnvTab: (variables) => {
147-
if (get().focusTabId) {
148-
const existingTab = get().tabs.find((t) => t.id === get().focusTabId);
149-
if (existingTab) {
150-
if (!existingTab.variablesDraft) {
151-
existingTab.variablesDraft = cloneDeep(existingTab.variables);
145+
updateEnvTab: (tabId, variables) => {
146+
set(
147+
produce((state) => {
148+
if (tabId) {
149+
const existingTab = state.tabs.find((t) => t.id === tabId);
150+
if (existingTab) {
151+
if (!existingTab.variablesDraft) {
152+
existingTab.variablesDraft = cloneDeep(existingTab.variables);
153+
}
154+
existingTab.variablesDraft = variables;
155+
}
152156
}
153-
existingTab.variablesDraft = variables;
154-
}
155-
console.log(existingTab);
156-
}
157+
}),
158+
);
157159
},
158160
closeTab: (id, collectionId) => {
159161
set((state) => ({ tabs: state.tabs.filter((t) => t.id !== id) }));

0 commit comments

Comments
 (0)