Skip to content

Commit

Permalink
Merge branch 'feat/search-from-me' of https://github.com/mary-ext/for…
Browse files Browse the repository at this point in the history
…k-bsky-app into mary-ext-feat/search-from-me
  • Loading branch information
pfrazee committed Jan 23, 2024
2 parents 6076b8f + 7f4d3dc commit 68a439d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/lib/strings/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,27 @@ export function countLines(str: string | undefined): number {
if (!str) return 0
return str.match(/\n/g)?.length ?? 0
}

// Augments search query with additional syntax like `from:me`
export function augmentSearchQuery(query: string, {did}: {did?: string}) {
// Don't do anything if there's no DID
if (!did) {
return query
}

// We don't want to replace substrings that are being "quoted" because those
// are exact string matches, so what we'll do here is to split them apart

// Even-indexed strings are unquoted, odd-indexed strings are quoted
const splits = query.split(/("(?:[^"\\]|\\.)*")/g)

return splits
.map((str, idx) => {
if (idx % 2 === 0) {
return str.replaceAll(/(^|\s)from:me(\s|$)/g, `$1${did}$2`)
}

return str
})
.join('')
}
9 changes: 7 additions & 2 deletions src/view/screens/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {isNative, isWeb} from '#/platform/detection'
import {listenSoftReset} from '#/state/events'
import {s} from '#/lib/styles'
import AsyncStorage from '@react-native-async-storage/async-storage'
import {augmentSearchQuery} from '#/lib/strings/helpers'

function Loader() {
const pal = usePalette('default')
Expand Down Expand Up @@ -318,9 +319,13 @@ export function SearchScreenInner({
const pal = usePalette('default')
const setMinimalShellMode = useSetMinimalShellMode()
const setDrawerSwipeDisabled = useSetDrawerSwipeDisabled()
const {hasSession} = useSession()
const {hasSession, currentAccount} = useSession()
const {isDesktop} = useWebMediaQueries()

const augmentedQuery = React.useMemo(() => {
return augmentSearchQuery(query || '', {did: currentAccount?.did})
}, [query, currentAccount])

const onPageSelected = React.useCallback(
(index: number) => {
setMinimalShellMode(false)
Expand All @@ -343,7 +348,7 @@ export function SearchScreenInner({
)}
initialPage={0}>
<View>
<SearchScreenPostResults query={query} />
<SearchScreenPostResults query={augmentedQuery} />
</View>
<View>
<SearchScreenUserResults query={query} />
Expand Down

0 comments on commit 68a439d

Please sign in to comment.