Skip to content

Commit

Permalink
Roam import WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
quolpr committed Feb 22, 2022
1 parent 9ed7c7d commit 44cbd9d
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 493 deletions.
62 changes: 61 additions & 1 deletion packages/web-app/src/pages/VaultsPage/ImportModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import React from 'react';
import {
generateId,
roamToHarikaJson,
VaultApplication,
} from '@harika/web-core';
import { isArray } from 'lodash-es';
import React, { useEffect } from 'react';

import { modalClass } from '../../components/Modal/Modal';
import { useGetSyncToken } from '../../hooks/useGetSyncToken';
import { useUserVaults } from '../../hooks/useUserApp';
import { paths } from '../../paths';
import { cn } from '../../utils';

const formModalClass = cn('vault-form-modal');
Expand All @@ -12,6 +21,57 @@ export const ImportModal = ({
file: File;
vaultName: string;
}) => {
const userVaults = useUserVaults();
const getSyncToken = useGetSyncToken();

useEffect(() => {
const callback = async () => {
let data: any | undefined = undefined;

try {
data = JSON.parse(await file.text());
} catch {
console.error('Failed to parse JSON!');

return;
}

const dump = (() => {
if (isArray(data)) {
return roamToHarikaJson(data);
// we assume it is roam format
} else {
// Otherwise harika format
return data;
}
})();

const vaultId = generateId();
const vault = await userVaults?.createVault({
name: vaultName,
dbId: vaultId,
});
if (!vault) {
console.error('Failed to create vault');

return;
}

const vaultApp = new VaultApplication(
vaultId,
import.meta.env.VITE_PUBLIC_WS_URL as string,
getSyncToken,
);

await vaultApp.start();
await vaultApp.getImportExportService().importData(dump);

// window.location.pathname = paths.vaultDailyPath({ vaultId: vault.id });
};

callback();
}, [file, getSyncToken, userVaults, vaultName]);

return (
<>
<h1 className={`${formModalClass('header')} ${modalClass('header')} `}>
Expand Down
1 change: 1 addition & 0 deletions packages/web-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export * from './src/apps/VaultApplication/BlocksExtension/models/BaseBlock';
export * from './src/apps/VaultApplication/BlocksExtension/models/BlocksSelection';
export * from './src/lib/blockParser/blockUtils';
export * from './src/apps/VaultApplication/BlocksExtension/selectors/getGroupedBacklinks';
export * from './src/lib/roamToHarikaJson';
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,22 @@ export class TextBlocksRepository extends BaseBlockRepository<
TextBlockDoc,
TextBlockRow
> {
bulkCreate(
async bulkCreate(
attrsArray: TextBlockDoc[],
ctx: ISyncCtx,
e: IQueryExecuter = this.db,
) {
return e.transaction(async (t) => {
const res = await super.bulkCreate(attrsArray, ctx, t);

await t.insertRecords(
textBlocksFTSTable,
res.map((row) => ({
id: row.id,
textContent: row.content.toLowerCase(),
})),
);
const res = await super.bulkCreate(attrsArray, ctx);

return res;
});
await e.insertRecords(
textBlocksFTSTable,
res.map((row) => ({
id: row.id,
textContent: row.content.toLowerCase(),
})),
);

return res;
}

bulkUpdate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,51 +73,49 @@ export class ImportExportService {

const links: BlockLinkRow[] = [];

await this.noteBlocksRepository.transaction(async (t) => {
for (const { tableName, rows } of dump.data) {
if (tableName === noteBlocksTable) {
// eslint-disable-next-line no-loop-func
rows.forEach((row) => {
row.linkedBlockIds.forEach((id: string) => {
links.push({
id: generateId(),
blockId: row.id,
linkedToBlockId: id,
orderPosition: i++,
createdAt: new Date().getTime(),
updatedAt: new Date().getTime(),
});
for (const { tableName, rows } of dump.data) {
if (tableName === noteBlocksTable) {
// eslint-disable-next-line no-loop-func
rows.forEach((row) => {
row.linkedBlockIds.forEach((id: string) => {
links.push({
id: generateId(),
blockId: row.id,
linkedToBlockId: id,
orderPosition: i++,
createdAt: new Date().getTime(),
updatedAt: new Date().getTime(),
});

delete row['linkedBlockIds'];
});

await this.noteBlocksRepository.bulkCreate(rows, ctx, t);
} else if (tableName === textBlocksTable) {
// eslint-disable-next-line no-loop-func
rows.forEach((row) => {
row.linkedBlockIds.forEach((id: string) => {
links.push({
id: generateId(),
blockId: row.id,
linkedToBlockId: id,
orderPosition: i++,
createdAt: new Date().getTime(),
updatedAt: new Date().getTime(),
});
});
delete row['linkedBlockIds'];
});

delete row['linkedBlockIds'];
await this.noteBlocksRepository.bulkCreate(rows, ctx);
} else if (tableName === textBlocksTable) {
// eslint-disable-next-line no-loop-func
rows.forEach((row) => {
row.linkedBlockIds.forEach((id: string) => {
links.push({
id: generateId(),
blockId: row.id,
linkedToBlockId: id,
orderPosition: i++,
createdAt: new Date().getTime(),
updatedAt: new Date().getTime(),
});
});

await this.textBlocksRepository.bulkCreate(rows, ctx, t);
} else if (tableName === blocksScopesTable) {
await this.blocksScopesRepository.bulkCreate(rows, ctx, t);
}
delete row['linkedBlockIds'];
});

await this.textBlocksRepository.bulkCreate(rows, ctx);
} else if (tableName === blocksScopesTable) {
await this.blocksScopesRepository.bulkCreate(rows, ctx);
}
}

await this.blockLinksRepo.bulkCreate(links, ctx, t);
});
await this.blockLinksRepo.bulkCreate(links, ctx);
}

private async secondVersionImport(dump: Dump) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ export class OnDbChangeNotifier {
windowId !== this.currentWindowId || source === 'inDbChanges',
);

console.log('New events need to notify', JSON.stringify(evs));

if (evs.length === 0) return;

const groupedMappedData = this.getGroupedMappedData(evs);
Expand Down
Loading

0 comments on commit 44cbd9d

Please sign in to comment.