From c2a39d7c1f59ddc0625b49d423c15799b5eba61b Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Tue, 22 Nov 2022 12:02:31 -0600 Subject: Refactor: create src/lib to reflect actual sharing of that code --- src/lib/numbers.ts | 3 + src/lib/strings.ts | 151 ++++++++++++++++++++++++++++ src/state/lib/api.ts | 2 +- src/state/models/feed-view.ts | 2 +- src/state/models/notifications-view.ts | 2 +- src/state/models/post.ts | 2 +- src/view/com/modals/CreateScene.tsx | 2 +- src/view/com/modals/EditProfile.tsx | 6 +- src/view/com/notifications/FeedItem.tsx | 2 +- src/view/com/post-thread/PostThreadItem.tsx | 2 +- src/view/com/profile/ProfileHeader.tsx | 2 +- src/view/com/util/DropdownBtn.tsx | 2 +- src/view/com/util/PostMeta.tsx | 2 +- src/view/lib/strings.ts | 151 ---------------------------- src/view/screens/Login.tsx | 6 +- src/view/screens/PostDownvotedBy.tsx | 2 +- src/view/screens/PostRepostedBy.tsx | 2 +- src/view/screens/PostThread.tsx | 2 +- src/view/screens/PostUpvotedBy.tsx | 2 +- src/view/shell/mobile/TabsSelector.tsx | 2 +- src/view/shell/mobile/index.tsx | 1 + 21 files changed, 180 insertions(+), 168 deletions(-) create mode 100644 src/lib/numbers.ts create mode 100644 src/lib/strings.ts delete mode 100644 src/view/lib/strings.ts (limited to 'src') diff --git a/src/lib/numbers.ts b/src/lib/numbers.ts new file mode 100644 index 000000000..fdf2cad85 --- /dev/null +++ b/src/lib/numbers.ts @@ -0,0 +1,3 @@ +export function clamp(v: number, min: number, max: number): number { + return Math.min(max, Math.max(min, v)) +} diff --git a/src/lib/strings.ts b/src/lib/strings.ts new file mode 100644 index 000000000..8f2c3a3ea --- /dev/null +++ b/src/lib/strings.ts @@ -0,0 +1,151 @@ +import {AtUri} from '../third-party/uri' +import {Entity} from '../third-party/api/src/client/types/app/bsky/feed/post' +import {PROD_SERVICE} from '../state' + +export const MAX_DISPLAY_NAME = 64 +export const MAX_DESCRIPTION = 256 + +export function pluralize(n: number, base: string, plural?: string): string { + if (n === 1) { + return base + } + if (plural) { + return plural + } + return base + 's' +} + +export function makeRecordUri( + didOrName: string, + collection: string, + rkey: string, +) { + const urip = new AtUri(`at://host/`) + urip.host = didOrName + urip.collection = collection + urip.rkey = rkey + return urip.toString() +} + +const MINUTE = 60 +const HOUR = MINUTE * 60 +const DAY = HOUR * 24 +const MONTH = DAY * 30 +const YEAR = DAY * 365 +export function ago(date: number | string | Date): string { + let ts: number + if (typeof date === 'string') { + ts = Number(new Date(date)) + } else if (date instanceof Date) { + ts = Number(date) + } else { + ts = date + } + const diffSeconds = Math.floor((Date.now() - ts) / 1e3) + if (diffSeconds < MINUTE) { + return `${diffSeconds}s` + } else if (diffSeconds < HOUR) { + return `${Math.floor(diffSeconds / MINUTE)}m` + } else if (diffSeconds < DAY) { + return `${Math.floor(diffSeconds / HOUR)}h` + } else if (diffSeconds < MONTH) { + return `${Math.floor(diffSeconds / DAY)}d` + } else if (diffSeconds < YEAR) { + return `${Math.floor(diffSeconds / MONTH)}mo` + } else { + return new Date(ts).toLocaleDateString() + } +} + +export function extractEntities( + text: string, + knownHandles?: Set, +): Entity[] | undefined { + let match + let ents: Entity[] = [] + { + // mentions + const re = /(^|\s)(@)([a-zA-Z0-9\.-]+)(\b)/dg + while ((match = re.exec(text))) { + if (knownHandles && !knownHandles.has(match[3])) { + continue // not a known handle + } + ents.push({ + type: 'mention', + value: match[3], + index: { + start: match.indices[2][0], // skip the (^|\s) but include the '@' + end: match.indices[3][1], + }, + }) + } + } + { + // links + const re = /(^|\s)(https?:\/\/[\S]+)(\b)/dg + while ((match = re.exec(text))) { + ents.push({ + type: 'link', + value: match[2], + index: { + start: match.indices[1][0], // skip the (^|\s) but include the '@' + end: match.indices[2][1], + }, + }) + } + } + return ents.length > 0 ? ents : undefined +} + +export function makeValidHandle(str: string): string { + if (str.length > 20) { + str = str.slice(0, 20) + } + str = str.toLowerCase() + return str.replace(/^[^a-z]+/g, '').replace(/[^a-z0-9-]/g, '') +} + +export function createFullHandle(name: string, domain: string): string { + name = name.replace(/[\.]+$/, '') + domain = domain.replace(/^[\.]+/, '') + return `${name}.${domain}` +} + +export function enforceLen(str: string, len: number): string { + str = str || '' + if (str.length > len) { + return str.slice(0, len) + } + return str +} + +export function cleanError(str: string): string { + if (str.includes('Network request failed')) { + return 'Unable to connect. Please check your internet connection and try again.' + } + if (str.startsWith('Error: ')) { + return str.slice('Error: '.length) + } + return str +} + +export function toNiceDomain(url: string): string { + try { + const urlp = new URL(url) + if (`https://${urlp.host}` === PROD_SERVICE) { + return 'Bluesky Social' + } + return urlp.host + } catch (e) { + return url + } +} + +export function toShareUrl(url: string) { + if (!url.startsWith('https')) { + const urlp = new URL('https://bsky.app') + urlp.pathname = url + url = urlp.toString() + } + return url +} diff --git a/src/state/lib/api.ts b/src/state/lib/api.ts index 5f147e01f..842905d1d 100644 --- a/src/state/lib/api.ts +++ b/src/state/lib/api.ts @@ -10,7 +10,7 @@ import * as Post from '../../third-party/api/src/client/types/app/bsky/feed/post import {AtUri} from '../../third-party/uri' import {APP_BSKY_GRAPH} from '../../third-party/api' import {RootStoreModel} from '../models/root-store' -import {extractEntities} from '../../view/lib/strings' +import {extractEntities} from '../../lib/strings' export function doPolyfill() { AtpApi.xrpc.fetch = fetchHandler diff --git a/src/state/models/feed-view.ts b/src/state/models/feed-view.ts index 2cd6b591c..202476fa3 100644 --- a/src/state/models/feed-view.ts +++ b/src/state/models/feed-view.ts @@ -4,7 +4,7 @@ import * as GetAuthorFeed from '../../third-party/api/src/client/types/app/bsky/ import {AtUri} from '../../third-party/uri' import {RootStoreModel} from './root-store' import * as apilib from '../lib/api' -import {cleanError} from '../../view/lib/strings' +import {cleanError} from '../../lib/strings' export class FeedItemMyStateModel { repost?: string diff --git a/src/state/models/notifications-view.ts b/src/state/models/notifications-view.ts index 76617c6db..09189cfbb 100644 --- a/src/state/models/notifications-view.ts +++ b/src/state/models/notifications-view.ts @@ -4,7 +4,7 @@ import {RootStoreModel} from './root-store' import {Declaration} from './_common' import {hasProp} from '../lib/type-guards' import {APP_BSKY_GRAPH} from '../../third-party/api' -import {cleanError} from '../../view/lib/strings' +import {cleanError} from '../../lib/strings' const UNGROUPABLE_REASONS = ['trend', 'assertion'] diff --git a/src/state/models/post.ts b/src/state/models/post.ts index 767182a93..c6dfaeacd 100644 --- a/src/state/models/post.ts +++ b/src/state/models/post.ts @@ -2,7 +2,7 @@ import {makeAutoObservable} from 'mobx' import * as Post from '../../third-party/api/src/client/types/app/bsky/feed/post' import {AtUri} from '../../third-party/uri' import {RootStoreModel} from './root-store' -import {cleanError} from '../../view/lib/strings' +import {cleanError} from '../../lib/strings' export type PostEntities = Post.Record['entities'] export type PostReply = Post.Record['reply'] diff --git a/src/view/com/modals/CreateScene.tsx b/src/view/com/modals/CreateScene.tsx index 3fb3ab6f8..445374623 100644 --- a/src/view/com/modals/CreateScene.tsx +++ b/src/view/com/modals/CreateScene.tsx @@ -18,7 +18,7 @@ import { enforceLen, MAX_DISPLAY_NAME, MAX_DESCRIPTION, -} from '../../lib/strings' +} from '../../../lib/strings' import {AppBskyActorCreateScene} from '../../../third-party/api/index' export const snapPoints = ['60%'] diff --git a/src/view/com/modals/EditProfile.tsx b/src/view/com/modals/EditProfile.tsx index d4cb6692c..f739b0843 100644 --- a/src/view/com/modals/EditProfile.tsx +++ b/src/view/com/modals/EditProfile.tsx @@ -7,7 +7,11 @@ import {ErrorMessage} from '../util/ErrorMessage' import {useStores} from '../../../state' import {ProfileViewModel} from '../../../state/models/profile-view' import {s, colors, gradients} from '../../lib/styles' -import {enforceLen, MAX_DISPLAY_NAME, MAX_DESCRIPTION} from '../../lib/strings' +import { + enforceLen, + MAX_DISPLAY_NAME, + MAX_DESCRIPTION, +} from '../../../lib/strings' import * as Profile from '../../../third-party/api/src/client/types/app/bsky/actor/profile' export const snapPoints = ['60%'] diff --git a/src/view/com/notifications/FeedItem.tsx b/src/view/com/notifications/FeedItem.tsx index c3f2b1cfd..8741e4236 100644 --- a/src/view/com/notifications/FeedItem.tsx +++ b/src/view/com/notifications/FeedItem.tsx @@ -5,7 +5,7 @@ import {AtUri} from '../../../third-party/uri' import {FontAwesomeIcon, Props} from '@fortawesome/react-native-fontawesome' import {NotificationsViewItemModel} from '../../../state/models/notifications-view' import {s, colors} from '../../lib/styles' -import {ago, pluralize} from '../../lib/strings' +import {ago, pluralize} from '../../../lib/strings' import {UpIconSolid} from '../../lib/icons' import {UserAvatar} from '../util/UserAvatar' import {PostText} from '../post/PostText' diff --git a/src/view/com/post-thread/PostThreadItem.tsx b/src/view/com/post-thread/PostThreadItem.tsx index 83aac1010..cb439bc32 100644 --- a/src/view/com/post-thread/PostThreadItem.tsx +++ b/src/view/com/post-thread/PostThreadItem.tsx @@ -12,7 +12,7 @@ import {PostDropdownBtn} from '../util/DropdownBtn' import Toast from '../util/Toast' import {UserAvatar} from '../util/UserAvatar' import {s, colors} from '../../lib/styles' -import {ago, pluralize} from '../../lib/strings' +import {ago, pluralize} from '../../../lib/strings' import {useStores} from '../../../state' import {PostMeta} from '../util/PostMeta' import {PostCtrls} from '../util/PostCtrls' diff --git a/src/view/com/profile/ProfileHeader.tsx b/src/view/com/profile/ProfileHeader.tsx index ed8924964..5a31d5c54 100644 --- a/src/view/com/profile/ProfileHeader.tsx +++ b/src/view/com/profile/ProfileHeader.tsx @@ -17,7 +17,7 @@ import { EditProfileModel, InviteToSceneModel, } from '../../../state/models/shell-ui' -import {pluralize} from '../../lib/strings' +import {pluralize} from '../../../lib/strings' import {s, colors} from '../../lib/styles' import {getGradient} from '../../lib/asset-gen' import {MagnifyingGlassIcon} from '../../lib/icons' diff --git a/src/view/com/util/DropdownBtn.tsx b/src/view/com/util/DropdownBtn.tsx index 01c9259a2..ea4e19503 100644 --- a/src/view/com/util/DropdownBtn.tsx +++ b/src/view/com/util/DropdownBtn.tsx @@ -13,7 +13,7 @@ import {IconProp} from '@fortawesome/fontawesome-svg-core' import RootSiblings from 'react-native-root-siblings' import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import {colors} from '../../lib/styles' -import {toShareUrl} from '../../lib/strings' +import {toShareUrl} from '../../../lib/strings' import {useStores} from '../../../state' import {ConfirmModel} from '../../../state/models/shell-ui' import {TABS_ENABLED} from '../../../build-flags' diff --git a/src/view/com/util/PostMeta.tsx b/src/view/com/util/PostMeta.tsx index e89edec35..d158418e9 100644 --- a/src/view/com/util/PostMeta.tsx +++ b/src/view/com/util/PostMeta.tsx @@ -4,7 +4,7 @@ import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import {Link} from '../util/Link' import {PostDropdownBtn} from '../util/DropdownBtn' import {s} from '../../lib/styles' -import {ago} from '../../lib/strings' +import {ago} from '../../../lib/strings' interface PostMetaOpts { itemHref: string diff --git a/src/view/lib/strings.ts b/src/view/lib/strings.ts deleted file mode 100644 index 84ee07f37..000000000 --- a/src/view/lib/strings.ts +++ /dev/null @@ -1,151 +0,0 @@ -import {AtUri} from '../../third-party/uri' -import {Entity} from '../../third-party/api/src/client/types/app/bsky/feed/post' -import {PROD_SERVICE} from '../../state' - -export const MAX_DISPLAY_NAME = 64 -export const MAX_DESCRIPTION = 256 - -export function pluralize(n: number, base: string, plural?: string): string { - if (n === 1) { - return base - } - if (plural) { - return plural - } - return base + 's' -} - -export function makeRecordUri( - didOrName: string, - collection: string, - rkey: string, -) { - const urip = new AtUri(`at://host/`) - urip.host = didOrName - urip.collection = collection - urip.rkey = rkey - return urip.toString() -} - -const MINUTE = 60 -const HOUR = MINUTE * 60 -const DAY = HOUR * 24 -const MONTH = DAY * 30 -const YEAR = DAY * 365 -export function ago(date: number | string | Date): string { - let ts: number - if (typeof date === 'string') { - ts = Number(new Date(date)) - } else if (date instanceof Date) { - ts = Number(date) - } else { - ts = date - } - const diffSeconds = Math.floor((Date.now() - ts) / 1e3) - if (diffSeconds < MINUTE) { - return `${diffSeconds}s` - } else if (diffSeconds < HOUR) { - return `${Math.floor(diffSeconds / MINUTE)}m` - } else if (diffSeconds < DAY) { - return `${Math.floor(diffSeconds / HOUR)}h` - } else if (diffSeconds < MONTH) { - return `${Math.floor(diffSeconds / DAY)}d` - } else if (diffSeconds < YEAR) { - return `${Math.floor(diffSeconds / MONTH)}mo` - } else { - return new Date(ts).toLocaleDateString() - } -} - -export function extractEntities( - text: string, - knownHandles?: Set, -): Entity[] | undefined { - let match - let ents: Entity[] = [] - { - // mentions - const re = /(^|\s)(@)([a-zA-Z0-9\.-]+)(\b)/dg - while ((match = re.exec(text))) { - if (knownHandles && !knownHandles.has(match[3])) { - continue // not a known handle - } - ents.push({ - type: 'mention', - value: match[3], - index: { - start: match.indices[2][0], // skip the (^|\s) but include the '@' - end: match.indices[3][1], - }, - }) - } - } - { - // links - const re = /(^|\s)(https?:\/\/[\S]+)(\b)/dg - while ((match = re.exec(text))) { - ents.push({ - type: 'link', - value: match[2], - index: { - start: match.indices[1][0], // skip the (^|\s) but include the '@' - end: match.indices[2][1], - }, - }) - } - } - return ents.length > 0 ? ents : undefined -} - -export function makeValidHandle(str: string): string { - if (str.length > 20) { - str = str.slice(0, 20) - } - str = str.toLowerCase() - return str.replace(/^[^a-z]+/g, '').replace(/[^a-z0-9-]/g, '') -} - -export function createFullHandle(name: string, domain: string): string { - name = name.replace(/[\.]+$/, '') - domain = domain.replace(/^[\.]+/, '') - return `${name}.${domain}` -} - -export function enforceLen(str: string, len: number): string { - str = str || '' - if (str.length > len) { - return str.slice(0, len) - } - return str -} - -export function cleanError(str: string): string { - if (str.includes('Network request failed')) { - return 'Unable to connect. Please check your internet connection and try again.' - } - if (str.startsWith('Error: ')) { - return str.slice('Error: '.length) - } - return str -} - -export function toNiceDomain(url: string): string { - try { - const urlp = new URL(url) - if (`https://${urlp.host}` === PROD_SERVICE) { - return 'Bluesky Social' - } - return urlp.host - } catch (e) { - return url - } -} - -export function toShareUrl(url: string) { - if (!url.startsWith('https')) { - const urlp = new URL('https://bsky.app') - urlp.pathname = url - url = urlp.toString() - } - return url -} diff --git a/src/view/screens/Login.tsx b/src/view/screens/Login.tsx index aca587286..abd5274da 100644 --- a/src/view/screens/Login.tsx +++ b/src/view/screens/Login.tsx @@ -16,7 +16,11 @@ import * as EmailValidator from 'email-validator' import {observer} from 'mobx-react-lite' import {Picker} from '../com/util/Picker' import {s, colors} from '../lib/styles' -import {makeValidHandle, createFullHandle, toNiceDomain} from '../lib/strings' +import { + makeValidHandle, + createFullHandle, + toNiceDomain, +} from '../../lib/strings' import {useStores, DEFAULT_SERVICE} from '../../state' import {ServiceDescription} from '../../state/models/session' import {ServerInputModel} from '../../state/models/shell-ui' diff --git a/src/view/screens/PostDownvotedBy.tsx b/src/view/screens/PostDownvotedBy.tsx index 4d9752799..b16ec5c0a 100644 --- a/src/view/screens/PostDownvotedBy.tsx +++ b/src/view/screens/PostDownvotedBy.tsx @@ -4,7 +4,7 @@ import {ViewHeader} from '../com/util/ViewHeader' import {PostVotedBy as PostLikedByComponent} from '../com/post-thread/PostVotedBy' import {ScreenParams} from '../routes' import {useStores} from '../../state' -import {makeRecordUri} from '../lib/strings' +import {makeRecordUri} from '../../lib/strings' export const PostDownvotedBy = ({navIdx, visible, params}: ScreenParams) => { const store = useStores() diff --git a/src/view/screens/PostRepostedBy.tsx b/src/view/screens/PostRepostedBy.tsx index 8e8346693..d8e4b910e 100644 --- a/src/view/screens/PostRepostedBy.tsx +++ b/src/view/screens/PostRepostedBy.tsx @@ -4,7 +4,7 @@ import {ViewHeader} from '../com/util/ViewHeader' import {PostRepostedBy as PostRepostedByComponent} from '../com/post-thread/PostRepostedBy' import {ScreenParams} from '../routes' import {useStores} from '../../state' -import {makeRecordUri} from '../lib/strings' +import {makeRecordUri} from '../../lib/strings' export const PostRepostedBy = ({navIdx, visible, params}: ScreenParams) => { const store = useStores() diff --git a/src/view/screens/PostThread.tsx b/src/view/screens/PostThread.tsx index 7db247a31..1e63ac390 100644 --- a/src/view/screens/PostThread.tsx +++ b/src/view/screens/PostThread.tsx @@ -1,6 +1,6 @@ import React, {useEffect, useMemo, useState} from 'react' import {View} from 'react-native' -import {makeRecordUri} from '../lib/strings' +import {makeRecordUri} from '../../lib/strings' import {ViewHeader} from '../com/util/ViewHeader' import {PostThread as PostThreadComponent} from '../com/post-thread/PostThread' import {PostThreadViewModel} from '../../state/models/post-thread-view' diff --git a/src/view/screens/PostUpvotedBy.tsx b/src/view/screens/PostUpvotedBy.tsx index d87fa548d..2794d529a 100644 --- a/src/view/screens/PostUpvotedBy.tsx +++ b/src/view/screens/PostUpvotedBy.tsx @@ -4,7 +4,7 @@ import {ViewHeader} from '../com/util/ViewHeader' import {PostVotedBy as PostLikedByComponent} from '../com/post-thread/PostVotedBy' import {ScreenParams} from '../routes' import {useStores} from '../../state' -import {makeRecordUri} from '../lib/strings' +import {makeRecordUri} from '../../lib/strings' export const PostUpvotedBy = ({navIdx, visible, params}: ScreenParams) => { const store = useStores() diff --git a/src/view/shell/mobile/TabsSelector.tsx b/src/view/shell/mobile/TabsSelector.tsx index 4b246728f..be54cfc5b 100644 --- a/src/view/shell/mobile/TabsSelector.tsx +++ b/src/view/shell/mobile/TabsSelector.tsx @@ -21,7 +21,7 @@ import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import Swipeable from 'react-native-gesture-handler/Swipeable' import {useStores} from '../../../state' import {s, colors} from '../../lib/styles' -import {toShareUrl} from '../../lib/strings' +import {toShareUrl} from '../../../lib/strings' import {match} from '../../routes' const TAB_HEIGHT = 42 diff --git a/src/view/shell/mobile/index.tsx b/src/view/shell/mobile/index.tsx index 1c148b18c..4c85d3b1a 100644 --- a/src/view/shell/mobile/index.tsx +++ b/src/view/shell/mobile/index.tsx @@ -37,6 +37,7 @@ import {MainMenu} from './MainMenu' import {TabsSelector} from './TabsSelector' import {Composer} from './Composer' import {s, colors} from '../../lib/styles' +import {clamp} from '../../../lib/numbers' import { GridIcon, GridIconSolid, -- cgit 1.4.1