Add fastIsType util
This commit is contained in:
@@ -14,6 +14,7 @@ import {QueryClient} from '@tanstack/react-query'
|
||||
import chunk from 'lodash.chunk'
|
||||
|
||||
import {labelIsHideableOffense} from '#/lib/moderation'
|
||||
import * as atp from '#/types/atproto'
|
||||
import {precacheProfile} from '../profile'
|
||||
import {FeedNotification, FeedPage, NotificationType} from './types'
|
||||
|
||||
@@ -255,14 +256,17 @@ function getSubjectUri(
|
||||
return notif.uri
|
||||
} else if (type === 'post-like' || type === 'repost') {
|
||||
if (
|
||||
AppBskyFeedRepost.isRecord(notif.record) ||
|
||||
AppBskyFeedLike.isRecord(notif.record)
|
||||
atp.fastIsType<AppBskyFeedRepost.Record>(
|
||||
notif.record,
|
||||
AppBskyFeedRepost.isRecord,
|
||||
) ||
|
||||
atp.fastIsType<AppBskyFeedLike.Record>(
|
||||
notif.record,
|
||||
AppBskyFeedLike.isRecord,
|
||||
)
|
||||
) {
|
||||
const record = notif.record as
|
||||
| AppBskyFeedRepost.Record
|
||||
| AppBskyFeedLike.Record
|
||||
return typeof record.subject?.uri === 'string'
|
||||
? record.subject?.uri
|
||||
return typeof notif.record.subject?.uri === 'string'
|
||||
? notif.record.subject?.uri
|
||||
: undefined
|
||||
}
|
||||
} else if (type === 'feedgen-like') {
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
findAllProfilesInQueryData as findAllProfilesInSearchQueryData,
|
||||
} from '#/state/queries/search-posts'
|
||||
import {useAgent} from '#/state/session'
|
||||
import * as atp from '#/types/atproto'
|
||||
import {
|
||||
findAllPostsInQueryData as findAllPostsInNotifsQueryData,
|
||||
findAllProfilesInQueryData as findAllProfilesInNotifsQueryData,
|
||||
@@ -329,7 +330,10 @@ function responseToThreadNodes(
|
||||
): ThreadNode {
|
||||
if (
|
||||
AppBskyFeedDefs.isThreadViewPost(node) &&
|
||||
AppBskyFeedPost.isValidRecord(node.post.record)
|
||||
atp.fastIsType<AppBskyFeedPost.Record>(
|
||||
node.post.record,
|
||||
AppBskyFeedPost.isRecord,
|
||||
)
|
||||
) {
|
||||
const post = node.post
|
||||
// These should normally be present. They're missing only for
|
||||
|
||||
@@ -1 +1,24 @@
|
||||
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