Skip to content

Commit 6a88310

Browse files
committed
fix data sync
1 parent 0c8cd9d commit 6a88310

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

apps/desktop/src/components/editor-area/index.tsx

-9
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ export default function EditorArea({
2525
editable: boolean;
2626
sessionId: string;
2727
}) {
28-
const { ongoingSessionStatus } = useOngoingSession((s) => ({
29-
ongoingSessionStatus: s.status,
30-
}));
31-
3228
const showRaw = useSession(sessionId, (s) => s.showRaw);
3329

3430
const [rawContent, setRawContent] = useSession(sessionId, (s) => [
@@ -44,7 +40,6 @@ export default function EditorArea({
4440

4541
const sessionStore = useSession(sessionId, (s) => ({
4642
session: s.session,
47-
refresh: s.refresh,
4843
}));
4944

5045
const editorRef = useRef<{ editor: TiptapEditor | null }>(null);
@@ -53,10 +48,6 @@ export default function EditorArea({
5348
[sessionId, showRaw],
5449
);
5550

56-
useEffect(() => {
57-
sessionStore.refresh();
58-
}, [sessionStore.refresh, ongoingSessionStatus]);
59-
6051
const enhance = useEnhanceMutation({
6152
sessionId,
6253
rawContent,

apps/desktop/src/components/editor-area/note-header/index.tsx

+4-7
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ interface NoteHeaderProps {
1515
}
1616

1717
export function NoteHeader({ onNavigateToEditor, editable, sessionId, hashtags = [] }: NoteHeaderProps) {
18-
const sessionStore = useSession(sessionId, (s) => ({
19-
session: s.session,
20-
updateTitle: s.updateTitle,
21-
persistSession: s.persistSession,
22-
}));
18+
const updateTitle = useSession(sessionId, (s) => s.updateTitle);
19+
const sessionTitle = useSession(sessionId, (s) => s.session.title);
2320

2421
const handleTitleChange = (e: ChangeEvent<HTMLInputElement>) => {
25-
sessionStore.updateTitle(e.target.value);
22+
updateTitle(e.target.value);
2623
};
2724

2825
const noteMatch = useMatch({ from: "/app/note/$id", shouldThrow: false });
@@ -34,7 +31,7 @@ export function NoteHeader({ onNavigateToEditor, editable, sessionId, hashtags =
3431
<div className="flex-1 space-y-1">
3532
<TitleInput
3633
editable={editable}
37-
value={sessionStore.session.title}
34+
value={sessionTitle}
3835
onChange={handleTitleChange}
3936
onNavigateToEditor={onNavigateToEditor}
4037
/>

apps/desktop/src/routes/app.new.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const Route = createFileRoute("/app/new")({
4040
insert(session);
4141

4242
await queryClient.invalidateQueries({
43-
queryKey: ["event-session", calendarEventId],
43+
predicate: (query) => query.queryKey.some((key) => (typeof key === "string") && key.includes("session")),
4444
});
4545
} else {
4646
const session = await dbCommands.upsertSession({

packages/utils/src/stores/ongoing-session.ts

+17-11
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const createOngoingSessionStore = (sessionsStore: ReturnType<typeof creat
6868
});
6969
},
7070
stop: () => {
71-
const { sessionId, channel } = get();
71+
const { channel, sessionId } = get();
7272

7373
if (channel) {
7474
listenerCommands.unsubscribe(channel);
@@ -77,11 +77,14 @@ export const createOngoingSessionStore = (sessionsStore: ReturnType<typeof creat
7777
listenerCommands.stopSession().then(() => {
7878
set(initialState);
7979

80-
// session stored in sessionStore become stale during ongoing-session. Refresh it here.
81-
if (sessionId) {
82-
const sessionStore = sessionsStore.getState().sessions[sessionId];
83-
sessionStore.getState().refresh();
84-
}
80+
// We need refresh since session in store is now stale.
81+
// setTimeout is needed because of debounce.
82+
setTimeout(() => {
83+
if (sessionId) {
84+
const sessionStore = sessionsStore.getState().sessions[sessionId];
85+
sessionStore.getState().refresh();
86+
}
87+
}, 1500);
8588
});
8689
},
8790
pause: () => {
@@ -90,11 +93,14 @@ export const createOngoingSessionStore = (sessionsStore: ReturnType<typeof creat
9093
listenerCommands.pauseSession().then(() => {
9194
set({ status: "running_paused" });
9295

93-
// session stored in sessionStore become stale during ongoing-session. Refresh it here.
94-
if (sessionId) {
95-
const sessionStore = sessionsStore.getState().sessions[sessionId];
96-
sessionStore.getState().refresh();
97-
}
96+
// We need refresh since session in store is now stale.
97+
// setTimeout is needed because of debounce.
98+
setTimeout(() => {
99+
if (sessionId) {
100+
const sessionStore = sessionsStore.getState().sessions[sessionId];
101+
sessionStore.getState().refresh();
102+
}
103+
}, 1500);
98104
});
99105
},
100106
resume: () => {

packages/utils/src/stores/session.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,15 @@ export const createSessionStore = (session: Session) => {
6969
});
7070
},
7171
persistSession: async (session?: Session, force?: boolean) => {
72-
const item = session ?? get().session;
72+
const { session: { id } } = get();
73+
const sessionFromDB = await dbCommands.getSession({ id });
74+
75+
// TODO: this is temp solution.
76+
const item: Session = {
77+
...(session ?? get().session),
78+
conversations: sessionFromDB?.conversations ?? [],
79+
};
80+
7381
const fn = force
7482
? dbCommands.upsertSession
7583
: pDebounce((v: Session) => dbCommands.upsertSession(v), 50);

0 commit comments

Comments
 (0)