Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 57 additions & 3 deletions apps/desktop/src/store/tinybase/persister/local/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as _UI from "tinybase/ui-react/with-schemas";
import { createMergeableStore } from "tinybase/with-schemas";

import { getCurrentWebviewWindowLabel } from "@hypr/plugin-windows";
import { type Schemas } from "@hypr/store";
import { SCHEMA, type Schemas } from "@hypr/store";

import type { Store } from "../../store/main";
import { STORE_ID } from "../../store/main";
Expand Down Expand Up @@ -115,10 +116,64 @@ function migrateWordsAndHintsToTranscripts(store: Store): boolean {
return true;
}

async function cleanupSessionsFromSqlite(): Promise<void> {
const tempStore = createMergeableStore()
.setTablesSchema(SCHEMA.table)
.setValuesSchema(SCHEMA.value) as Store;

const tempPersister = createLocalPersister(tempStore, {
storeTableName: STORE_ID,
storeIdColumnName: "id",
});

await tempPersister.load();

const sessionIds = tempStore.getRowIds("sessions");
if (sessionIds.length === 0) {
tempPersister.destroy();
return;
}

tempStore.transaction(() => {
for (const sessionId of sessionIds) {
tempStore.delRow("sessions", sessionId);
}

for (const mappingId of tempStore.getRowIds(
"mapping_session_participant",
)) {
tempStore.delRow("mapping_session_participant", mappingId);
}

for (const tagId of tempStore.getRowIds("tags")) {
tempStore.delRow("tags", tagId);
}

for (const mappingId of tempStore.getRowIds("mapping_tag_session")) {
tempStore.delRow("mapping_tag_session", mappingId);
}

for (const transcriptId of tempStore.getRowIds("transcripts")) {
tempStore.delRow("transcripts", transcriptId);
}

for (const noteId of tempStore.getRowIds("enhanced_notes")) {
tempStore.delRow("enhanced_notes", noteId);
}
});

await tempPersister.save();
tempPersister.destroy();
}

export function useLocalPersister(store: Store) {
return useCreatePersister(
store,
async (store) => {
if (getCurrentWebviewWindowLabel() === "main") {
await cleanupSessionsFromSqlite();
}

const persister = createLocalPersister(store as Store, {
storeTableName: STORE_ID,
storeIdColumnName: "id",
Expand All @@ -127,8 +182,7 @@ export function useLocalPersister(store: Store) {
await persister.load();

if (getCurrentWebviewWindowLabel() === "main") {
const migrated = migrateWordsAndHintsToTranscripts(store as Store);
if (migrated) {
if (migrateWordsAndHintsToTranscripts(store as Store)) {
await persister.save();
}
}
Expand Down
Loading