-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
580b67b
commit b1f88b3
Showing
5 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import zod from 'zod' | ||
|
||
import {BaseNux} from '#/state/queries/nux/types' | ||
|
||
export enum Nux { | ||
One = 'one', | ||
Two = 'two', | ||
} | ||
|
||
export const nuxNames = new Set(Object.values(Nux)) | ||
|
||
export type AppNux = | ||
| BaseNux<{ | ||
id: Nux.One | ||
data: { | ||
likes: number | ||
} | ||
}> | ||
| BaseNux<{ | ||
id: Nux.Two | ||
data: undefined | ||
}> | ||
|
||
export const NuxSchemas = { | ||
[Nux.One]: zod.object({ | ||
likes: zod.number(), | ||
}), | ||
[Nux.Two]: undefined, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import {useMutation, useQueryClient} from '@tanstack/react-query' | ||
|
||
import {AppNux, Nux} from '#/state/queries/nux/definitions' | ||
import {parseAppNux, serializeAppNux} from '#/state/queries/nux/util' | ||
import { | ||
preferencesQueryKey, | ||
usePreferencesQuery, | ||
} from '#/state/queries/preferences' | ||
import {useAgent} from '#/state/session' | ||
|
||
export {Nux} from '#/state/queries/nux/definitions' | ||
|
||
export function useNuxs() { | ||
const {data, ...rest} = usePreferencesQuery() | ||
|
||
if (data && rest.isSuccess) { | ||
const nuxs = data.bskyAppState.nuxs | ||
?.map(parseAppNux) | ||
?.filter(Boolean) as AppNux[] | ||
|
||
if (nuxs) { | ||
return { | ||
nuxs, | ||
...rest, | ||
} | ||
} | ||
} | ||
|
||
return { | ||
nuxs: undefined, | ||
...rest, | ||
} | ||
} | ||
|
||
export function useNux<T extends Nux>(id: T) { | ||
const {nuxs, ...rest} = useNuxs() | ||
|
||
if (nuxs && rest.isSuccess) { | ||
const nux = nuxs.find(nux => nux.id === id) | ||
|
||
if (nux) { | ||
return { | ||
nux: nux as Extract<AppNux, {id: T}>, | ||
...rest, | ||
} | ||
} | ||
} | ||
|
||
return { | ||
nux: undefined, | ||
...rest, | ||
} | ||
} | ||
|
||
export function useUpsertNuxMutation() { | ||
const queryClient = useQueryClient() | ||
const agent = useAgent() | ||
|
||
return useMutation({ | ||
mutationFn: async (nux: AppNux) => { | ||
await agent.bskyAppUpsertNux(serializeAppNux(nux)) | ||
// triggers a refetch | ||
await queryClient.invalidateQueries({ | ||
queryKey: preferencesQueryKey, | ||
}) | ||
}, | ||
}) | ||
} | ||
|
||
export function useRemoveNuxsMutation() { | ||
const queryClient = useQueryClient() | ||
const agent = useAgent() | ||
|
||
return useMutation({ | ||
mutationFn: async (ids: string[]) => { | ||
await agent.bskyAppRemoveNuxs(ids) | ||
// triggers a refetch | ||
await queryClient.invalidateQueries({ | ||
queryKey: preferencesQueryKey, | ||
}) | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {AppBskyActorDefs} from '@atproto/api' | ||
|
||
export type Data = Record<string, unknown> | undefined | ||
|
||
export type BaseNux< | ||
T extends Pick<AppBskyActorDefs.Nux, 'id' | 'expiresAt'> & {data: Data}, | ||
> = T & { | ||
completed: boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {AppBskyActorDefs, nuxSchema} from '@atproto/api' | ||
|
||
import { | ||
AppNux, | ||
Nux, | ||
nuxNames, | ||
NuxSchemas, | ||
} from '#/state/queries/nux/definitions' | ||
|
||
export function parseAppNux(nux: AppBskyActorDefs.Nux): AppNux | undefined { | ||
if (!nuxNames.has(nux.id as Nux)) return | ||
if (!nuxSchema.safeParse(nux).success) return | ||
|
||
const {data, ...rest} = nux | ||
|
||
const schema = NuxSchemas[nux.id as Nux] | ||
|
||
if (schema && data) { | ||
const parsedData = JSON.parse(data) | ||
|
||
if (!schema.safeParse(parsedData).success) return | ||
|
||
return { | ||
...rest, | ||
data: parsedData, | ||
} as AppNux | ||
} | ||
|
||
return { | ||
...rest, | ||
data: undefined, | ||
} as AppNux | ||
} | ||
|
||
export function serializeAppNux(nux: AppNux): AppBskyActorDefs.Nux { | ||
const {data, ...rest} = nux | ||
const schema = NuxSchemas[nux.id as Nux] | ||
|
||
const result: AppBskyActorDefs.Nux = { | ||
...rest, | ||
data: undefined, | ||
} | ||
|
||
if (schema) { | ||
schema.parse(data) | ||
result.data = JSON.stringify(data) | ||
} | ||
|
||
nuxSchema.parse(result) | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters