From 6885fb2b41efdb29e386cf696ea7304baf3a87c8 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Mon, 2 Jan 2023 20:56:35 -0600 Subject: Bump api@0.0.5 and use the type guard helpers --- src/state/models/post-thread-view.ts | 24 ++++-------------------- src/view/com/util/PostEmbeds.tsx | 28 +++++++++++++--------------- 2 files changed, 17 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/state/models/post-thread-view.ts b/src/state/models/post-thread-view.ts index f8065bb20..2ada18a40 100644 --- a/src/state/models/post-thread-view.ts +++ b/src/state/models/post-thread-view.ts @@ -4,11 +4,6 @@ import {AtUri} from '../../third-party/uri' import {RootStoreModel} from './root-store' import * as apilib from '../lib/api' -interface UnknownPost { - $type: string - [k: string]: unknown -} - function* reactKeyGenerator(): Generator { let counter = 0 while (true) { @@ -16,17 +11,6 @@ function* reactKeyGenerator(): Generator { } } -function isThreadViewPost( - v: GetPostThread.ThreadViewPost | GetPostThread.NotFoundPost | UnknownPost, -): v is GetPostThread.ThreadViewPost { - return v.$type === 'app.bksy.feed.getPostThread#threadViewPost' -} -function isNotFoundPost( - v: GetPostThread.ThreadViewPost | GetPostThread.NotFoundPost | UnknownPost, -): v is GetPostThread.NotFoundPost { - return v.$type === 'app.bsky.feed.getPostThread#notFoundPost' -} - export class PostThreadViewPostModel { // ui state _reactKey: string = '' @@ -58,7 +42,7 @@ export class PostThreadViewPostModel { ) { // parents if (includeParent && v.parent) { - if (isThreadViewPost(v.parent)) { + if (GetPostThread.isThreadViewPost(v.parent)) { const parentModel = new PostThreadViewPostModel( this.rootStore, keyGen.next().value, @@ -69,7 +53,7 @@ export class PostThreadViewPostModel { parentModel.assignTreeModels(keyGen, v.parent, true, false) } this.parent = parentModel - } else if (isNotFoundPost(v.parent)) { + } else if (GetPostThread.isNotFoundPost(v.parent)) { this.parent = v.parent } } @@ -77,7 +61,7 @@ export class PostThreadViewPostModel { if (includeChildren && v.replies) { const replies = [] for (const item of v.replies) { - if (isThreadViewPost(item)) { + if (GetPostThread.isThreadViewPost(item)) { const itemModel = new PostThreadViewPostModel( this.rootStore, keyGen.next().value, @@ -88,7 +72,7 @@ export class PostThreadViewPostModel { itemModel.assignTreeModels(keyGen, item, false, true) } replies.push(itemModel) - } else if (isNotFoundPost(item)) { + } else if (GetPostThread.isNotFoundPost(item)) { replies.push(item) } } diff --git a/src/view/com/util/PostEmbeds.tsx b/src/view/com/util/PostEmbeds.tsx index df2de681c..611cedf55 100644 --- a/src/view/com/util/PostEmbeds.tsx +++ b/src/view/com/util/PostEmbeds.tsx @@ -1,5 +1,5 @@ import React from 'react' -import {ImageStyle, StyleSheet, StyleProp, View, ViewStyle} from 'react-native' +import {StyleSheet, StyleProp, View, ViewStyle} from 'react-native' import {AppBskyEmbedImages, AppBskyEmbedExternal} from '@atproto/api' import {Link} from '../util/Link' import {Text} from './text/Text' @@ -23,39 +23,38 @@ export function PostEmbeds({ }) { const pal = usePalette('default') const store = useStores() - if (embed?.$type === 'app.bsky.embed.images#presented') { - const imgEmbed = embed as AppBskyEmbedImages.Presented - if (imgEmbed.images.length > 0) { - const uris = imgEmbed.images.map(img => img.fullsize) + if (AppBskyEmbedImages.isPresented(embed)) { + if (embed.images.length > 0) { + const uris = embed.images.map(img => img.fullsize) const openLightbox = (index: number) => { store.shell.openLightbox(new ImagesLightbox(uris, index)) } - if (imgEmbed.images.length === 4) { + if (embed.images.length === 4) { return ( img.thumb)} + uris={embed.images.map(img => img.thumb)} onPress={openLightbox} /> ) - } else if (imgEmbed.images.length === 3) { + } else if (embed.images.length === 3) { return ( img.thumb)} + uris={embed.images.map(img => img.thumb)} onPress={openLightbox} /> ) - } else if (imgEmbed.images.length === 2) { + } else if (embed.images.length === 2) { return ( img.thumb)} + uris={embed.images.map(img => img.thumb)} onPress={openLightbox} /> @@ -64,7 +63,7 @@ export function PostEmbeds({ return ( openLightbox(0)} containerStyle={{borderRadius: 4}} /> @@ -73,9 +72,8 @@ export function PostEmbeds({ } } } - if (embed?.$type === 'app.bsky.embed.external#presented') { - const externalEmbed = embed as AppBskyEmbedExternal.Presented - const link = externalEmbed.external + if (AppBskyEmbedExternal.isPresented(embed)) { + const link = embed.external return (