Skip to content

Commit 5accf2e

Browse files
committed
fix: remove dirty state update after http call
1 parent ff74b50 commit 5accf2e

File tree

2 files changed

+10
-59
lines changed

2 files changed

+10
-59
lines changed

apps/codeimage/src/pages/Dashboard/dashboard.state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function makeDashboardState(authState = getAuth0State()) {
7171
},
7272
editors: [
7373
{
74-
code: `from server: ${appEnvironment.defaultState.editor.code}`,
74+
code: appEnvironment.defaultState.editor.code,
7575
languageId: appEnvironment.defaultState.editor.languageId,
7676
tabName: 'index.tsx',
7777
},

apps/codeimage/src/state/editor/createEditorSync.ts

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import type * as ApiTypes from '@codeimage/api/api-types';
2-
import {createTrackObserver} from '@codeimage/atomic-state';
32
import {getAuth0State} from '@codeimage/store/auth/auth0';
43
import {getRootEditorStore} from '@codeimage/store/editor';
54
import {getFrameState} from '@codeimage/store/editor/frame';
65
import {getEditorStore} from '@codeimage/store/editor/index';
76
import {ProjectEditorPersistedState} from '@codeimage/store/editor/model';
87
import {getTerminalState} from '@codeimage/store/editor/terminal';
8+
import {toast} from '@codeimage/ui';
99
import {appEnvironment} from '@core/configuration';
1010
import {createContextProvider} from '@solid-primitives/context';
1111
import {useNavigate} from '@solidjs/router';
1212
import {
13+
catchError,
1314
combineLatest,
1415
debounceTime,
1516
EMPTY,
@@ -30,7 +31,7 @@ import {
3031
Resource,
3132
untrack,
3233
} from 'solid-js';
33-
import {reconcile, unwrap} from 'solid-js/store';
34+
import {unwrap} from 'solid-js/store';
3435
import {API} from '../../data-access/api';
3536
import {useIdb} from '../../hooks/use-indexed-db';
3637

@@ -43,7 +44,6 @@ function createEditorSyncAdapter(props: {snippetId: string}) {
4344
const [activeWorkspace, setActiveWorkspace] = createSignal<
4445
ApiTypes.GetProjectByIdApi['response'] | null
4546
>();
46-
const [tracked, untrackCallback] = createTrackObserver();
4747
const authState = getAuth0State();
4848
const frameStore = getFrameState();
4949
const terminalStore = getTerminalState();
@@ -155,7 +155,6 @@ function createEditorSyncAdapter(props: {snippetId: string}) {
155155
function initRemoteDbSync() {
156156
const subscription = onChange$
157157
.pipe(
158-
filter(tracked),
159158
debounceTime(150),
160159
filter(() => authState.loggedIn()),
161160
tap(() => setRemoteSync(true)),
@@ -175,63 +174,15 @@ function createEditorSyncAdapter(props: {snippetId: string}) {
175174
body: dataToSave,
176175
params: {id: workspace.id},
177176
}),
177+
).pipe(
178+
catchError(() => {
179+
toast.error('An error occurred while saving');
180+
return EMPTY;
181+
}),
178182
);
179183
}),
180184
)
181-
.subscribe(updatedSnippet => {
182-
untrackCallback(() => {
183-
const activeEditorIndex = editorStore.state.editors.findIndex(
184-
({id}) => id === editorStore.state.activeEditorId,
185-
);
186-
187-
frameStore.setStore(state => ({
188-
...state,
189-
visible: updatedSnippet.frame.visible,
190-
opacity: updatedSnippet.frame.opacity,
191-
radius: updatedSnippet.frame.radius ?? state.radius,
192-
padding: updatedSnippet.frame.padding,
193-
background: updatedSnippet.frame.background ?? state.background,
194-
}));
195-
editorStore.setState(
196-
reconcile(
197-
{
198-
activeEditorId:
199-
updatedSnippet.editorTabs[activeEditorIndex].id ??
200-
updatedSnippet.editorTabs[0].id,
201-
options: {
202-
focused: true,
203-
themeId: updatedSnippet.editorOptions.themeId,
204-
showLineNumbers: updatedSnippet.editorOptions.showLineNumbers,
205-
fontId: updatedSnippet.editorOptions.fontId,
206-
fontWeight: updatedSnippet.editorOptions.fontWeight,
207-
},
208-
editors: updatedSnippet.editorTabs.map(editor => ({
209-
code: editor.code,
210-
id: editor.id,
211-
tab: {
212-
tabName: editor.tabName,
213-
},
214-
languageId: editor.languageId,
215-
})),
216-
},
217-
{merge: true},
218-
),
219-
);
220-
terminalStore.setState(state => ({
221-
...state,
222-
type: updatedSnippet.terminal.type,
223-
opacity: updatedSnippet.terminal.opacity,
224-
showWatermark: updatedSnippet.terminal.showWatermark ?? false,
225-
showHeader: updatedSnippet.terminal.showHeader,
226-
background: updatedSnippet.terminal.background ?? state.background,
227-
showGlassReflection: updatedSnippet.terminal.showGlassReflection,
228-
alternativeTheme: updatedSnippet.terminal.alternativeTheme,
229-
accentVisible: updatedSnippet.terminal.accentVisible,
230-
textColor: updatedSnippet.terminal.textColor ?? state.textColor,
231-
shadow: updatedSnippet.terminal.shadow,
232-
}));
233-
});
234-
});
185+
.subscribe();
235186

236187
return onCleanup(() => {
237188
const workspace = untrack(activeWorkspace);

0 commit comments

Comments
 (0)