Add fastIsType util
This commit is contained in:
@@ -14,6 +14,7 @@ import {QueryClient} from '@tanstack/react-query'
|
|||||||
import chunk from 'lodash.chunk'
|
import chunk from 'lodash.chunk'
|
||||||
|
|
||||||
import {labelIsHideableOffense} from '#/lib/moderation'
|
import {labelIsHideableOffense} from '#/lib/moderation'
|
||||||
|
import * as atp from '#/types/atproto'
|
||||||
import {precacheProfile} from '../profile'
|
import {precacheProfile} from '../profile'
|
||||||
import {FeedNotification, FeedPage, NotificationType} from './types'
|
import {FeedNotification, FeedPage, NotificationType} from './types'
|
||||||
|
|
||||||
@@ -255,14 +256,17 @@ function getSubjectUri(
|
|||||||
return notif.uri
|
return notif.uri
|
||||||
} else if (type === 'post-like' || type === 'repost') {
|
} else if (type === 'post-like' || type === 'repost') {
|
||||||
if (
|
if (
|
||||||
AppBskyFeedRepost.isRecord(notif.record) ||
|
atp.fastIsType<AppBskyFeedRepost.Record>(
|
||||||
AppBskyFeedLike.isRecord(notif.record)
|
notif.record,
|
||||||
|
AppBskyFeedRepost.isRecord,
|
||||||
|
) ||
|
||||||
|
atp.fastIsType<AppBskyFeedLike.Record>(
|
||||||
|
notif.record,
|
||||||
|
AppBskyFeedLike.isRecord,
|
||||||
|
)
|
||||||
) {
|
) {
|
||||||
const record = notif.record as
|
return typeof notif.record.subject?.uri === 'string'
|
||||||
| AppBskyFeedRepost.Record
|
? notif.record.subject?.uri
|
||||||
| AppBskyFeedLike.Record
|
|
||||||
return typeof record.subject?.uri === 'string'
|
|
||||||
? record.subject?.uri
|
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
} else if (type === 'feedgen-like') {
|
} else if (type === 'feedgen-like') {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import {
|
|||||||
findAllProfilesInQueryData as findAllProfilesInSearchQueryData,
|
findAllProfilesInQueryData as findAllProfilesInSearchQueryData,
|
||||||
} from '#/state/queries/search-posts'
|
} from '#/state/queries/search-posts'
|
||||||
import {useAgent} from '#/state/session'
|
import {useAgent} from '#/state/session'
|
||||||
|
import * as atp from '#/types/atproto'
|
||||||
import {
|
import {
|
||||||
findAllPostsInQueryData as findAllPostsInNotifsQueryData,
|
findAllPostsInQueryData as findAllPostsInNotifsQueryData,
|
||||||
findAllProfilesInQueryData as findAllProfilesInNotifsQueryData,
|
findAllProfilesInQueryData as findAllProfilesInNotifsQueryData,
|
||||||
@@ -329,7 +330,10 @@ function responseToThreadNodes(
|
|||||||
): ThreadNode {
|
): ThreadNode {
|
||||||
if (
|
if (
|
||||||
AppBskyFeedDefs.isThreadViewPost(node) &&
|
AppBskyFeedDefs.isThreadViewPost(node) &&
|
||||||
AppBskyFeedPost.isValidRecord(node.post.record)
|
atp.fastIsType<AppBskyFeedPost.Record>(
|
||||||
|
node.post.record,
|
||||||
|
AppBskyFeedPost.isRecord,
|
||||||
|
)
|
||||||
) {
|
) {
|
||||||
const post = node.post
|
const post = node.post
|
||||||
// These should normally be present. They're missing only for
|
// These should normally be present. They're missing only for
|
||||||
|
|||||||
@@ -1 +1,24 @@
|
|||||||
export * as profile from '#/types/atproto/profile'
|
export * as profile from '#/types/atproto/profile'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use sparingly, and only when you know it's safe to do so.
|
||||||
|
*
|
||||||
|
* Our SDK's `is*` identity utils do not assert the type of the entire object,
|
||||||
|
* and although the `isValid*` utils do, they also fully validate the object
|
||||||
|
* shape, which has a performance cost. This util allows us to prescribe the
|
||||||
|
* type we expect, while only checking the `$type` value of the record.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* ```ts
|
||||||
|
* import * as atp from '#/types/atproto'
|
||||||
|
*
|
||||||
|
* if (atp.fastIsType<AppBskyFeedPost.Record>(node.post.record, AppBskyFeedPost.isRecord)) {
|
||||||
|
* }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
export function fastIsType<R>(
|
||||||
|
record: unknown,
|
||||||
|
identity: <V>(v: V) => boolean,
|
||||||
|
): record is R {
|
||||||
|
return identity(record)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user