Use an action so we can update local cache when moving to inbox

This commit is contained in:
Jackson Harper
2024-06-25 13:04:34 +08:00
parent 9d9c6f5f3c
commit deccb6f946
4 changed files with 46 additions and 23 deletions

View File

@ -16,6 +16,7 @@ export type LinkedItemCardAction =
| 'open-notebook'
| 'unsubscribe'
| 'update-item'
| 'move-to-inbox'
export type LinkedItemCardProps = {
item: LibraryItemNode

View File

@ -13,7 +13,6 @@ import { TrashIcon } from '../../elements/icons/TrashIcon'
import { LabelIcon } from '../../elements/icons/LabelIcon'
import { UnarchiveIcon } from '../../elements/icons/UnarchiveIcon'
import { BrowserIcon } from '../../elements/icons/BrowserIcon'
import { LibraryIcon } from '../../elements/icons/LibraryIcon'
import useLibraryItemActions from '../../../lib/hooks/useLibraryItemActions'
import { MoveToInboxIcon } from '../../elements/icons/MoveToInboxIcon'
@ -82,9 +81,7 @@ export const LibraryHoverActions = (props: LibraryHoverActionsProps) => {
onClick={async (event) => {
event.preventDefault()
event.stopPropagation()
if (await moveItem(props.item.id)) {
props.handleAction('update-item')
}
props.handleAction('moveToInbox')
}}
>
<MoveToInboxIcon

View File

@ -13,6 +13,7 @@ import { setLinkArchivedMutation } from '../mutations/setLinkArchivedMutation'
import { updatePageMutation } from '../mutations/updatePageMutation'
import { gqlFetcher } from '../networkHelpers'
import { Label } from './../fragments/labelFragment'
import { moveToFolderMutation } from '../mutations/moveToLibraryMutation'
export interface ReadableItem {
id: string
@ -51,6 +52,7 @@ type LibraryItemAction =
| 'refresh'
| 'unsubscribe'
| 'update-item'
| 'move-to-inbox'
export type LibraryItemsData = {
search: LibraryItems
@ -298,16 +300,11 @@ export function useGetLibraryItemsQuery(
for (const searchResults of responsePages) {
const itemIndex = getIndexOf(searchResults.search, item)
console.log(' --- item index', itemIndex)
if (itemIndex !== -1) {
if (typeof mutatedItem === 'undefined') {
searchResults.search.edges.splice(itemIndex, 1)
} else {
searchResults.search.edges.splice(itemIndex, 1, mutatedItem)
console.log(
'earchResults.search.edges:',
searchResults.search.edges[itemIndex]
)
}
break
}
@ -316,8 +313,26 @@ export function useGetLibraryItemsQuery(
}
switch (action) {
case 'move-to-inbox':
updateData({
cursor: item.cursor,
node: {
...item.node,
folder: 'inbox',
},
})
moveToFolderMutation(item.cursor, 'inbox').then((res) => {
if (res) {
showSuccessToast('Link moved', { position: 'bottom-right' })
} else {
showErrorToast('Error moving link', { position: 'bottom-right' })
}
})
mutate()
break
case 'archive':
console.log('setting item archived')
updateData({
cursor: item.cursor,
node: {
@ -341,17 +356,13 @@ export function useGetLibraryItemsQuery(
break
case 'unarchive':
if (/in:all/.test(query)) {
updateData({
cursor: item.cursor,
node: {
...item.node,
isArchived: false,
},
})
} else {
updateData(undefined)
}
updateData({
cursor: item.cursor,
node: {
...item.node,
isArchived: false,
},
})
setLinkArchivedMutation({
linkId: item.node.id,
@ -365,9 +376,16 @@ export function useGetLibraryItemsQuery(
})
}
})
mutate()
break
case 'delete':
updateData(undefined)
updateData({
cursor: item.cursor,
node: {
...item.node,
state: State.DELETED,
},
})
const pageId = item.node.id
deleteLinkMutation(pageId).then((res) => {

View File

@ -74,7 +74,14 @@ export default function Home(): JSX.Element {
return (
<LibraryContainer
folder="archive"
filterFunc={(item) => item.state != 'DELETED' && item.isArchived}
filterFunc={(item) => {
console.log(
'running archive filter: ',
item.title,
item.isArchived
)
return item.state != 'DELETED' && item.isArchived
}}
showNavigationMenu={showNavigationMenu}
/>
)