export last 100 items

This commit is contained in:
Hongbo Wu 2024-03-21 14:06:56 +08:00
parent 7d4bc1b088
commit 445d2846b5
3 changed files with 28 additions and 8 deletions

View file

@ -1,5 +1,5 @@
import { IntegrationType } from '../../entity/integration'
import { findIntegration } from '../../services/integrations'
import { findIntegration, updateIntegration } from '../../services/integrations'
import { findRecentLibraryItems } from '../../services/library_item'
import { findActiveUser } from '../../services/user'
import { enqueueExportItem } from '../../utils/createTask'
@ -39,8 +39,8 @@ export const exportAllItems = async (jobData: ExportAllItemsJobData) => {
return
}
const maxItems = 1000
const limit = 100
const maxItems = 100
const limit = 10
let offset = 0
// get max 1000 most recent items from the database
while (offset < maxItems) {
@ -72,4 +72,12 @@ export const exportAllItems = async (jobData: ExportAllItemsJobData) => {
integrationId,
})
}
logger.info('exported all items', {
userId,
integrationId,
})
// clear task name in integration
await updateIntegration(integration.id, { taskName: null }, userId)
}

View file

@ -93,6 +93,11 @@ export const setIntegrationResolver = authorized<
// save integration
const integration = await saveIntegration(integrationToSave, uid)
if (integration.name.toLowerCase() === 'readwise') {
// create a task to export all the items for readwise temporarily
await enqueueExportToIntegration(integration.id, uid)
}
analytics.capture({
distinctId: uid,
event: 'integration_set',

View file

@ -13,7 +13,7 @@ import 'antd/dist/antd.compact.css'
import { CheckboxValueType } from 'antd/lib/checkbox/Group'
import Image from 'next/image'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
import { useCallback, useEffect, useState } from 'react'
import { HStack, VStack } from '../../../components/elements/LayoutPrimitives'
import { PageMetaData } from '../../../components/patterns/PageMetaData'
import { Beta } from '../../../components/templates/Beta'
@ -46,7 +46,7 @@ export default function Notion(): JSX.Element {
const [form] = Form.useForm<FieldType>()
const [messageApi, contextHolder] = message.useMessage()
const [exporting, setExporting] = useState(false)
const [exporting, setExporting] = useState(!!notion.taskName)
useEffect(() => {
form.setFieldsValue({
@ -97,7 +97,12 @@ export default function Notion(): JSX.Element {
form.setFieldsValue({ properties: value.map((v) => v.toString()) })
}
const exportToNotion = async () => {
const exportToNotion = useCallback(async () => {
if (exporting) {
messageApi.warning('Exporting process is already running.')
return
}
try {
const task = await exportToIntegrationMutation(notion.id)
// long polling to check the status of the task in every 10 seconds
@ -121,7 +126,7 @@ export default function Notion(): JSX.Element {
} catch (error) {
messageApi.error('There was an error exporting to Notion.')
}
}
}, [exporting, messageApi, notion.id])
return (
<>
@ -170,6 +175,7 @@ export default function Notion(): JSX.Element {
<Form.Item<FieldType>
label="Notion Page Id"
name="parentPageId"
help="The id of the Notion page where the items will be exported to. You can find it in the URL of the page."
rules={[
{
required: true,
@ -192,6 +198,7 @@ export default function Notion(): JSX.Element {
label="Automatic Sync"
name="enabled"
valuePropName="checked"
help="Once connected all new items will be exported to Notion"
>
<Switch />
</Form.Item>
@ -222,7 +229,7 @@ export default function Notion(): JSX.Element {
onClick={exportToNotion}
disabled={exporting}
>
{exporting ? 'Exporting' : 'Export most recent items to Notion'}
{exporting ? 'Exporting' : 'Export last 100 items'}
</Button>
</Spin>
</div>