mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Better lookback update
This commit is contained in:
32
src/utils/initQuery.ts
Normal file
32
src/utils/initQuery.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import queryClient from '@helpers/queryClient'
|
||||
import { store } from '@root/store'
|
||||
import { prefetchTimelineQuery } from './queryHooks/timeline'
|
||||
import { Instance, updateInstanceActive } from './slices/instancesSlice'
|
||||
|
||||
const initQuery = async ({
|
||||
instance,
|
||||
prefetch
|
||||
}: {
|
||||
instance: Instance
|
||||
prefetch?: { enabled: boolean; page?: 'Following' | 'LocalPublic' }
|
||||
}) => {
|
||||
store.dispatch(updateInstanceActive(instance))
|
||||
await queryClient.resetQueries()
|
||||
|
||||
if (prefetch?.enabled && instance.timelinesLookback) {
|
||||
if (
|
||||
prefetch.page &&
|
||||
instance.timelinesLookback[prefetch.page]?.ids?.length > 0
|
||||
) {
|
||||
await prefetchTimelineQuery(instance.timelinesLookback[prefetch.page])
|
||||
}
|
||||
|
||||
for (const page of Object.keys(instance.timelinesLookback)) {
|
||||
if (page !== prefetch.page) {
|
||||
prefetchTimelineQuery(instance.timelinesLookback[page])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default initQuery
|
@@ -2,30 +2,32 @@ import apiGeneral from '@api/general'
|
||||
import apiTooot from '@api/tooot'
|
||||
import { displayMessage } from '@components/Message'
|
||||
import navigationRef from '@helpers/navigationRef'
|
||||
import { Dispatch } from '@reduxjs/toolkit'
|
||||
import { isDevelopment } from '@utils/checkEnvironment'
|
||||
import { disableAllPushes, Instance } from '@utils/slices/instancesSlice'
|
||||
import { useTheme } from '@utils/styles/ThemeManager'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import { useEffect } from 'react'
|
||||
import { TFunction } from 'react-i18next'
|
||||
import { useDispatch } from 'react-redux'
|
||||
|
||||
export interface Params {
|
||||
mode: 'light' | 'dark'
|
||||
t: TFunction<'screens'>
|
||||
instances: Instance[]
|
||||
dispatch: Dispatch<any>
|
||||
}
|
||||
|
||||
const pushUseConnect = ({ mode, t, instances, dispatch }: Params) => {
|
||||
const pushUseConnect = ({ t, instances }: Params) => {
|
||||
const dispatch = useDispatch()
|
||||
const { mode } = useTheme()
|
||||
|
||||
return useEffect(() => {
|
||||
const connect = async () => {
|
||||
const expoToken = isDevelopment
|
||||
? 'DEVELOPMENT_TOKEN_1'
|
||||
: (
|
||||
await Notifications.getExpoPushTokenAsync({
|
||||
experienceId: '@xmflsct/tooot'
|
||||
})
|
||||
).data
|
||||
? 'DEVELOPMENT_TOKEN_1'
|
||||
: (
|
||||
await Notifications.getExpoPushTokenAsync({
|
||||
experienceId: '@xmflsct/tooot'
|
||||
})
|
||||
).data
|
||||
|
||||
apiTooot({
|
||||
method: 'get',
|
||||
|
@@ -1,19 +1,18 @@
|
||||
import { displayMessage } from '@components/Message'
|
||||
import queryClient from '@helpers/queryClient'
|
||||
import initQuery from '@utils/initQuery'
|
||||
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
|
||||
import { Instance, updateInstanceActive } from '@utils/slices/instancesSlice'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import { findIndex } from 'lodash'
|
||||
import { useEffect } from 'react'
|
||||
import { QueryClient } from 'react-query'
|
||||
import { useDispatch } from 'react-redux'
|
||||
import pushUseNavigate from './useNavigate'
|
||||
|
||||
export interface Params {
|
||||
queryClient: QueryClient
|
||||
instances: Instance[]
|
||||
}
|
||||
|
||||
const pushUseReceive = ({ queryClient, instances }: Params) => {
|
||||
const pushUseReceive = ({ instances }: Params) => {
|
||||
const dispatch = useDispatch()
|
||||
|
||||
return useEffect(() => {
|
||||
@@ -30,8 +29,7 @@ const pushUseReceive = ({ queryClient, instances }: Params) => {
|
||||
accountId: string
|
||||
}
|
||||
|
||||
const notificationIndex = findIndex(
|
||||
instances,
|
||||
const notificationIndex = instances.findIndex(
|
||||
instance =>
|
||||
instance.url === payloadData.instanceUrl &&
|
||||
instance.account.id === payloadData.accountId
|
||||
@@ -42,8 +40,10 @@ const pushUseReceive = ({ queryClient, instances }: Params) => {
|
||||
description: notification.request.content.body!,
|
||||
onPress: () => {
|
||||
if (notificationIndex !== -1) {
|
||||
dispatch(updateInstanceActive(instances[notificationIndex]))
|
||||
queryClient.clear()
|
||||
initQuery({
|
||||
instance: instances[notificationIndex],
|
||||
prefetch: { enabled: true }
|
||||
})
|
||||
}
|
||||
pushUseNavigate(payloadData.notification_id)
|
||||
}
|
||||
|
@@ -1,19 +1,19 @@
|
||||
import { Dispatch } from '@reduxjs/toolkit'
|
||||
import queryClient from '@helpers/queryClient'
|
||||
import initQuery from '@utils/initQuery'
|
||||
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
|
||||
import { Instance, updateInstanceActive } from '@utils/slices/instancesSlice'
|
||||
import { Instance } from '@utils/slices/instancesSlice'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import { findIndex } from 'lodash'
|
||||
import { useEffect } from 'react'
|
||||
import { QueryClient } from 'react-query'
|
||||
import { useDispatch } from 'react-redux'
|
||||
import pushUseNavigate from './useNavigate'
|
||||
|
||||
export interface Params {
|
||||
queryClient: QueryClient
|
||||
instances: Instance[]
|
||||
dispatch: Dispatch<any>
|
||||
}
|
||||
|
||||
const pushUseRespond = ({ queryClient, instances, dispatch }: Params) => {
|
||||
const pushUseRespond = ({ instances }: Params) => {
|
||||
const dispatch = useDispatch()
|
||||
|
||||
return useEffect(() => {
|
||||
const subscription = Notifications.addNotificationResponseReceivedListener(
|
||||
({ notification }) => {
|
||||
@@ -28,15 +28,16 @@ const pushUseRespond = ({ queryClient, instances, dispatch }: Params) => {
|
||||
accountId: string
|
||||
}
|
||||
|
||||
const notificationIndex = findIndex(
|
||||
instances,
|
||||
const notificationIndex = instances.findIndex(
|
||||
instance =>
|
||||
instance.url === payloadData.instanceUrl &&
|
||||
instance.account.id === payloadData.accountId
|
||||
)
|
||||
if (notificationIndex !== -1) {
|
||||
dispatch(updateInstanceActive(instances[notificationIndex]))
|
||||
queryClient.clear()
|
||||
initQuery({
|
||||
instance: instances[notificationIndex],
|
||||
prefetch: { enabled: true }
|
||||
})
|
||||
}
|
||||
pushUseNavigate(payloadData.notification_id)
|
||||
}
|
||||
|
@@ -1,5 +1,4 @@
|
||||
import queryClient from '@helpers/queryClient'
|
||||
import { findIndex } from 'lodash'
|
||||
import { InfiniteData } from 'react-query'
|
||||
import {
|
||||
MutationVarsTimelineUpdateStatusProperty,
|
||||
@@ -37,7 +36,9 @@ const updateStatusProperty = ({
|
||||
'boolean'
|
||||
) {
|
||||
const items = page.body as Mastodon.Conversation[]
|
||||
const tootIndex = findIndex(items, ['last_status.id', id])
|
||||
const tootIndex = items.findIndex(
|
||||
({ last_status }) => last_status?.id === id
|
||||
)
|
||||
if (tootIndex >= 0) {
|
||||
foundToot = true
|
||||
updateConversation({ item: items[tootIndex], payload })
|
||||
@@ -47,17 +48,18 @@ const updateStatusProperty = ({
|
||||
typeof (page.body as Mastodon.Notification[])[0].type === 'string'
|
||||
) {
|
||||
const items = page.body as Mastodon.Notification[]
|
||||
const tootIndex = findIndex(items, ['status.id', id])
|
||||
const tootIndex = items.findIndex(
|
||||
({ status }) => status?.id === id
|
||||
)
|
||||
if (tootIndex >= 0) {
|
||||
foundToot = true
|
||||
updateNotification({ item: items[tootIndex], payload })
|
||||
}
|
||||
} else {
|
||||
const items = page.body as Mastodon.Status[]
|
||||
const tootIndex = findIndex(items, [
|
||||
reblog ? 'reblog.id' : 'id',
|
||||
id
|
||||
])
|
||||
const tootIndex = reblog
|
||||
? items.findIndex(({ reblog }) => reblog?.id === id)
|
||||
: items.findIndex(toot => toot.id === id)
|
||||
// if favouriets page and notifications page, remove the item instead
|
||||
if (tootIndex >= 0) {
|
||||
foundToot = true
|
||||
@@ -90,7 +92,9 @@ const updateStatusProperty = ({
|
||||
'boolean'
|
||||
) {
|
||||
const items = page.body as Mastodon.Conversation[]
|
||||
const tootIndex = findIndex(items, ['last_status.id', id])
|
||||
const tootIndex = items.findIndex(
|
||||
({ last_status }) => last_status?.id === id
|
||||
)
|
||||
if (tootIndex >= 0) {
|
||||
foundToot = true
|
||||
updateConversation({ item: items[tootIndex], payload })
|
||||
@@ -101,17 +105,18 @@ const updateStatusProperty = ({
|
||||
'string'
|
||||
) {
|
||||
const items = page.body as Mastodon.Notification[]
|
||||
const tootIndex = findIndex(items, ['status.id', id])
|
||||
const tootIndex = items.findIndex(
|
||||
({ status }) => status?.id === id
|
||||
)
|
||||
if (tootIndex >= 0) {
|
||||
foundToot = true
|
||||
updateNotification({ item: items[tootIndex], payload })
|
||||
}
|
||||
} else {
|
||||
const items = page.body as Mastodon.Status[]
|
||||
const tootIndex = findIndex(items, [
|
||||
reblog ? 'reblog.id' : 'id',
|
||||
id
|
||||
])
|
||||
const tootIndex = reblog
|
||||
? items.findIndex(({ reblog }) => reblog?.id === id)
|
||||
: items.findIndex(toot => toot.id === id)
|
||||
// if favouriets page and notifications page, remove the item instead
|
||||
if (tootIndex >= 0) {
|
||||
foundToot = true
|
||||
|
@@ -3,7 +3,6 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
||||
import { RootState } from '@root/store'
|
||||
import { ComposeStateDraft } from '@screens/Compose/utils/types'
|
||||
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
|
||||
import { findIndex } from 'lodash'
|
||||
import addInstance from './instances/add'
|
||||
import removeInstance from './instances/remove'
|
||||
import { updateAccountPreferences } from './instances/updateAccountPreferences'
|
||||
@@ -130,10 +129,9 @@ const instancesSlice = createSlice({
|
||||
action: PayloadAction<ComposeStateDraft>
|
||||
) => {
|
||||
const activeIndex = findInstanceActive(instances)
|
||||
const draftIndex = findIndex(instances[activeIndex].drafts, [
|
||||
'timestamp',
|
||||
action.payload.timestamp
|
||||
])
|
||||
const draftIndex = instances[activeIndex].drafts.findIndex(
|
||||
({ timestamp }) => timestamp === action.payload.timestamp
|
||||
)
|
||||
if (draftIndex === -1) {
|
||||
instances[activeIndex].drafts.unshift(action.payload)
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user