feat(core): delete all readwise highlights when disconnect (#10975)

close AF-2306
This commit is contained in:
CatsJuice
2025-03-20 23:20:58 +00:00
parent e37328c83b
commit fbcb313de8
3 changed files with 30 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
import { Button, Modal } from '@affine/component'; import { Button, Modal } from '@affine/component';
import { useAsyncCallback } from '@affine/core/components/hooks/affine-async-hooks';
import { IntegrationService } from '@affine/core/modules/integration'; import { IntegrationService } from '@affine/core/modules/integration';
import { useI18n } from '@affine/i18n'; import { useI18n } from '@affine/i18n';
import { useService } from '@toeverything/infra'; import { useService } from '@toeverything/infra';
@@ -22,9 +23,11 @@ export const DisconnectDialog = ({ onClose }: { onClose: () => void }) => {
readwise.disconnect(); readwise.disconnect();
onClose(); onClose();
}, [onClose, readwise]); }, [onClose, readwise]);
// const handleDelete = useAsyncCallback(async () => { const handleDelete = useAsyncCallback(async () => {
// // TODO await readwise.deleteAll();
// }, []); readwise.disconnect();
onClose();
}, [onClose, readwise]);
return ( return (
<Modal <Modal
@@ -41,7 +44,7 @@ export const DisconnectDialog = ({ onClose }: { onClose: () => void }) => {
<footer className={styles.footer}> <footer className={styles.footer}>
<Button onClick={handleCancel}>{t['Cancel']()}</Button> <Button onClick={handleCancel}>{t['Cancel']()}</Button>
<div className={styles.actions}> <div className={styles.actions}>
<Button variant="error"> <Button variant="error" onClick={handleDelete}>
{t['com.affine.integration.readwise.disconnect.delete']()} {t['com.affine.integration.readwise.disconnect.delete']()}
</Button> </Button>
<Button variant="primary" onClick={handleKeep}> <Button variant="primary" onClick={handleKeep}>

View File

@@ -103,6 +103,7 @@ export const ImportDialog = ({ onClose }: { onClose: () => void }) => {
}, []); }, []);
const handleRetryCrawl = useCallback(() => { const handleRetryCrawl = useCallback(() => {
crawler.abort();
crawler.crawl(); crawler.crawl();
}, [crawler]); }, [crawler]);
@@ -144,6 +145,7 @@ export const ImportDialog = ({ onClose }: { onClose: () => void }) => {
highlights={highlights} highlights={highlights}
onClose={onClose} onClose={onClose}
onConfirm={handleConfirmImport} onConfirm={handleConfirmImport}
onResetLastImportedAt={handleRetryCrawl}
/> />
) )
) : ( ) : (
@@ -167,11 +169,13 @@ const SelectStage = ({
highlights, highlights,
onClose, onClose,
onConfirm, onConfirm,
onResetLastImportedAt,
}: { }: {
loading: boolean; loading: boolean;
highlights: ReadwiseHighlight[]; highlights: ReadwiseHighlight[];
onClose: () => void; onClose: () => void;
onConfirm: (ids: ReadwiseHighlight['id'][]) => void; onConfirm: (ids: ReadwiseHighlight['id'][]) => void;
onResetLastImportedAt: () => void;
}) => { }) => {
const t = useI18n(); const t = useI18n();
const readwise = useService(IntegrationService).readwise; const readwise = useService(IntegrationService).readwise;
@@ -181,7 +185,8 @@ const SelectStage = ({
const handleResetLastImportedAt = useCallback(() => { const handleResetLastImportedAt = useCallback(() => {
readwise.updateSetting('lastImportedAt', undefined); readwise.updateSetting('lastImportedAt', undefined);
}, [readwise]); onResetLastImportedAt();
}, [onResetLastImportedAt, readwise]);
const handleConfirmImport = useCallback(() => { const handleConfirmImport = useCallback(() => {
onConfirm(selected); onConfirm(selected);

View File

@@ -224,6 +224,23 @@ export class ReadwiseIntegration extends Entity<{ writer: IntegrationWriter }> {
token: undefined, token: undefined,
updateStrategy: undefined, updateStrategy: undefined,
syncNewHighlights: undefined, syncNewHighlights: undefined,
lastImportedAt: undefined,
}); });
} }
/**
* Delete all highlights of current user in current workspace
*/
async deleteAll() {
const refs = await this.getRefs();
await Promise.all(
refs.map(ref => {
const doc = this.docsService.list.doc$(ref.id).value;
if (doc) {
doc.moveToTrash();
}
return this.integrationRefStore.deleteRef(ref.id);
})
);
}
} }