Merge remote-tracking branch 'origin/main' into starter-packs

This commit is contained in:
Hailey
2024-06-18 14:17:33 -07:00
3 changed files with 58 additions and 4 deletions
+54 -1
View File
@@ -1,4 +1,7 @@
import React from 'react'
import React, {useEffect} from 'react'
import * as persisted from '#/state/persisted'
import {useAgent, useSession} from '../session'
type StateContext = Map<string, boolean>
type SetStateContext = (uri: string, value: boolean) => void
@@ -21,6 +24,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
},
[setState],
)
useMigrateMutes(setThreadMute)
return (
<stateContext.Provider value={state}>
<setStateContext.Provider value={setThreadMute}>
@@ -42,3 +48,50 @@ export function useIsThreadMuted(uri: string, defaultValue = false) {
export function useSetThreadMute() {
return React.useContext(setStateContext)
}
function useMigrateMutes(setThreadMute: SetStateContext) {
const agent = useAgent()
const {currentAccount} = useSession()
useEffect(() => {
if (currentAccount) {
if (
!persisted
.get('mutedThreads')
.some(uri => uri.includes(currentAccount.did))
) {
return
}
let cancelled = false
const migrate = async () => {
while (!cancelled) {
const threads = persisted.get('mutedThreads')
const root = threads.findLast(uri => uri.includes(currentAccount.did))
if (!root) break
persisted.write(
'mutedThreads',
threads.filter(uri => uri !== root),
)
setThreadMute(root, true)
await agent.api.app.bsky.graph
.muteThread({root})
// not a big deal if this fails, since the post might have been deleted
.catch(console.error)
}
}
migrate()
return () => {
cancelled = true
}
}
}, [agent, currentAccount, setThreadMute])
}
+2 -1
View File
@@ -74,7 +74,6 @@ export const schema = z.object({
flickr: z.enum(externalEmbedOptions).optional(),
})
.optional(),
mutedThreads: z.array(z.string()), // should move to server
invites: z.object({
copiedInvites: z.array(z.string()),
}),
@@ -96,6 +95,8 @@ export const schema = z.object({
})
.optional(),
usedStarterPacks: z.array(z.string()).optional(),
/** @deprecated */
mutedThreads: z.array(z.string()),
})
export type Schema = z.infer<typeof schema>
+2 -2
View File
@@ -304,8 +304,8 @@ export function useThreadMuteMutationQueue(
const queueToggle = useToggleMutationQueue<boolean>({
initialState: isThreadMuted,
runMutation: async (_prev, shouldLike) => {
if (shouldLike) {
runMutation: async (_prev, shouldMute) => {
if (shouldMute) {
await threadMuteMutation.mutateAsync({
uri: rootUri,
})