mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {useCallback, useMemo} from 'react'
2import {type RichText} from '@atproto/api'
3import Graphemer from 'graphemer'
4
5import {shortenLinks} from './rich-text-manip'
6
7export function enforceLen(
8 str: string,
9 len: number,
10 ellipsis = false,
11 mode: 'end' | 'middle' = 'end',
12): string {
13 str = str || ''
14 if (str.length > len) {
15 if (ellipsis) {
16 if (mode === 'end') {
17 return str.slice(0, len) + '…'
18 } else if (mode === 'middle') {
19 const half = Math.floor(len / 2)
20 return str.slice(0, half) + '…' + str.slice(-half)
21 } else {
22 // fallback
23 return str.slice(0, len)
24 }
25 } else {
26 return str.slice(0, len)
27 }
28 }
29 return str
30}
31
32export function useEnforceMaxGraphemeCount() {
33 const splitter = useMemo(() => new Graphemer(), [])
34
35 return useCallback(
36 (text: string, maxCount: number) => {
37 if (splitter.countGraphemes(text) > maxCount) {
38 return splitter.splitGraphemes(text).slice(0, maxCount).join('')
39 } else {
40 return text
41 }
42 },
43 [splitter],
44 )
45}
46
47export function useWarnMaxGraphemeCount({
48 text,
49 maxCount,
50}: {
51 text: string | RichText
52 maxCount: number
53}) {
54 const splitter = useMemo(() => new Graphemer(), [])
55
56 return useMemo(() => {
57 if (typeof text === 'string') {
58 return splitter.countGraphemes(text) > maxCount
59 } else {
60 return shortenLinks(text).graphemeLength > maxCount
61 }
62 }, [splitter, maxCount, text])
63}
64
65// https://stackoverflow.com/a/52171480
66export function toHashCode(str: string, seed = 0): number {
67 let h1 = 0xdeadbeef ^ seed,
68 h2 = 0x41c6ce57 ^ seed
69 for (let i = 0, ch; i < str.length; i++) {
70 ch = str.charCodeAt(i)
71 h1 = Math.imul(h1 ^ ch, 2654435761)
72 h2 = Math.imul(h2 ^ ch, 1597334677)
73 }
74 h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507)
75 h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909)
76 h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507)
77 h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909)
78
79 return 4294967296 * (2097151 & h2) + (h1 >>> 0)
80}
81
82export function countLines(str: string | undefined): number {
83 if (!str) return 0
84 return str.match(/\n/g)?.length ?? 0
85}
86
87// Augments search query with additional syntax like `from:me`
88export function augmentSearchQuery(query: string, {did}: {did?: string}) {
89 // Don't do anything if there's no DID
90 if (!did) {
91 return query
92 }
93
94 // replace “smart quotes” with normal ones
95 // iOS keyboard will add fancy unicode quotes, but only normal ones work
96 query = query.replaceAll(/[“”]/g, '"')
97
98 // We don't want to replace substrings that are being "quoted" because those
99 // are exact string matches, so what we'll do here is to split them apart
100
101 // Even-indexed strings are unquoted, odd-indexed strings are quoted
102 const splits = query.split(/("(?:[^"\\]|\\.)*")/g)
103
104 return splits
105 .map((str, idx) => {
106 if (idx % 2 === 0) {
107 return str.replaceAll(/(^|\s)from:me(\s|$)/g, `$1${did}$2`)
108 }
109
110 return str
111 })
112 .join('')
113}