feat(core): readwise import settings ui (#10913)

close AF-2308
This commit is contained in:
CatsJuice
2025-03-20 23:20:57 +00:00
parent 48f79d6467
commit f1c8a88a7c
15 changed files with 615 additions and 25 deletions
@@ -30,6 +30,13 @@ export class ReadwiseIntegration extends Entity<{ writer: IntegrationWriter }> {
importing$ = new LiveData(false);
settings$ = LiveData.from(this.readwiseStore.watchSetting(), undefined);
setting$<T extends keyof ReadwiseConfig>(
key: T
): LiveData<ReadwiseConfig[T]> {
return this.settings$.selector(setting => setting?.[key]);
}
updateSetting<T extends keyof ReadwiseConfig>(
key: T,
value: ReadwiseConfig[T]
@@ -77,6 +84,8 @@ export class ReadwiseIntegration extends Entity<{ writer: IntegrationWriter }> {
localRefs.map(ref => [ref.refMeta.highlightId, ref])
);
const updateStrategy = this.readwiseStore.getSetting('updateStrategy');
const syncNewHighlights =
this.readwiseStore.getSetting('syncNewHighlights');
const chunks = chunk(highlights, 2);
const total = highlights.length;
let finished = 0;
@@ -99,8 +108,14 @@ export class ReadwiseIntegration extends Entity<{ writer: IntegrationWriter }> {
const refMeta = localRef?.refMeta;
const localUpdatedAt = refMeta?.updatedAt;
const localDocId = localRef?.id;
const action = this.getAction({
localUpdatedAt,
remoteUpdatedAt: highlight.updated_at,
updateStrategy,
syncNewHighlights,
});
// write if not matched
if (localUpdatedAt !== highlight.updated_at && !signal?.aborted) {
if (action !== 'skip' && !signal?.aborted) {
await this.highlightToAffineDoc(highlight, book, localDocId, {
updateStrategy,
integrationId,
@@ -139,7 +154,7 @@ export class ReadwiseIntegration extends Entity<{ writer: IntegrationWriter }> {
title: book.title,
docId,
comment: highlight.note,
updateStrategy,
updateStrategy: updateStrategy ?? 'append',
});
// write failed
@@ -168,8 +183,43 @@ export class ReadwiseIntegration extends Entity<{ writer: IntegrationWriter }> {
});
}
getAction(info: {
localUpdatedAt?: string;
remoteUpdatedAt?: string;
updateStrategy?: ReadwiseConfig['updateStrategy'];
syncNewHighlights?: ReadwiseConfig['syncNewHighlights'];
}) {
const {
localUpdatedAt,
remoteUpdatedAt,
updateStrategy,
syncNewHighlights,
} = info;
return !localUpdatedAt
? syncNewHighlights
? 'new'
: 'skip'
: localUpdatedAt !== remoteUpdatedAt
? updateStrategy
? 'update'
: 'skip'
: 'skip';
}
connect(token: string) {
this.readwiseStore.setSettings({
token,
updateStrategy: 'append',
syncNewHighlights: true,
});
}
disconnect() {
this.readwiseStore.setSetting('token', undefined);
this.readwiseStore.setSetting('lastImportedAt', undefined);
this.readwiseStore.setSettings({
token: undefined,
updateStrategy: undefined,
syncNewHighlights: undefined,
});
}
}
@@ -81,4 +81,11 @@ export class ReadwiseStore extends Store {
[key]: value,
});
}
setSettings(settings: Partial<ReadwiseConfig>) {
this.globalState.set(this.getStorageKey(), {
...this.getSetting(),
...settings,
});
}
}
@@ -79,6 +79,10 @@ export interface ReadwiseConfig {
* The last import time
*/
lastImportedAt?: string;
/**
* Whether to sync new highlights
*/
syncNewHighlights?: boolean;
/**
* The update strategy
*/