feat: replace react-mentions with rc-mentions in FindInput component
- Updated FindInput component to use rc-mentions for better performance and flexibility. - Removed unused styles related to react-mentions and added new styles for rc-mentions in globals.scss. - Enhanced search functionality to handle mentions and clear input more effectively. - Updated FindPage to manage searched queries and loading state.
This commit is contained in:
1 parent
5fbc296931
commit
28cd757366
7 files changed
+216
-116
No files matched your search
@@ -1,50 +1,106 @@
|
||||
import React, { useState } from 'react'
|
||||
import inputStyle from './findInputStyle'
|
||||
import { MentionsInput, Mention } from 'react-mentions'
|
||||
import { searchPeople } from '@/handlers/api/people.handler';
|
||||
import mentionStyle from './findMentionStyle';
|
||||
import React from 'react'
|
||||
import Mentions from 'rc-mentions'
|
||||
import { listPeople, searchPeople } from '@/handlers/api/people.handler'
|
||||
import { ArrowRight, Loader2, Search, X } from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
interface FindInputProps {
|
||||
onSearch: (query: string) => void;
|
||||
value: string;
|
||||
onChange: (value: string) => void;
|
||||
loading?: boolean;
|
||||
}
|
||||
|
||||
export default function FindInput({ onSearch, value, onChange }: FindInputProps) {
|
||||
export default function FindInput({ onSearch, value, onChange, loading }: FindInputProps) {
|
||||
const [options, setOptions] = React.useState<{ value: string; label: string }[]>([])
|
||||
const [focused, setFocused] = React.useState(false)
|
||||
const nameToIdRef = React.useRef<Record<string, string>>({})
|
||||
|
||||
const handleSearchPeople = async (e: any, callback: any) => {
|
||||
if (!e.length) return [];
|
||||
return searchPeople(e).then((people) => people.map((person: any) => ({
|
||||
id: person.id,
|
||||
display: person.name,
|
||||
}))).then((people) => callback(people));
|
||||
const handleMentionSearch = async (text: string, prefix: string) => {
|
||||
if (prefix !== '@') {
|
||||
setOptions([])
|
||||
return
|
||||
}
|
||||
const people = text.length
|
||||
? await searchPeople(text)
|
||||
: await listPeople({ page: 1, perPage: 50, sort: 'assetCount', sortOrder: 'desc' }).then((r) => r.people)
|
||||
people.forEach((person: any) => {
|
||||
if (person.name) nameToIdRef.current[person.name] = person.id
|
||||
})
|
||||
setOptions(
|
||||
people
|
||||
.filter((person: any) => person.name)
|
||||
.map((person: any) => ({
|
||||
value: person.name,
|
||||
label: person.name,
|
||||
}))
|
||||
)
|
||||
}
|
||||
|
||||
const handleClear = () => {
|
||||
onChange('')
|
||||
}
|
||||
|
||||
const buildQueryWithIds = (displayValue: string) => {
|
||||
let query = displayValue
|
||||
for (const [name, id] of Object.entries(nameToIdRef.current)) {
|
||||
query = query.replaceAll(`@${name}`, `@${id}`)
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
const handleSubmit = () => {
|
||||
if (value.trim()) {
|
||||
onSearch(buildQueryWithIds(value))
|
||||
}
|
||||
}
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (e.key === 'Enter') {
|
||||
e.preventDefault()
|
||||
handleSubmit()
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<MentionsInput
|
||||
value={value}
|
||||
singleLine={true}
|
||||
placeholder='Search for photos, use @ to search for people'
|
||||
style={inputStyle}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter') {
|
||||
onSearch(value);
|
||||
}
|
||||
}}
|
||||
onChange={(e) => onChange(e.target.value)}>
|
||||
<Mention
|
||||
trigger="@"
|
||||
style={mentionStyle}
|
||||
data={handleSearchPeople}
|
||||
renderSuggestion={(
|
||||
highlightedDisplay,
|
||||
focused
|
||||
) => (
|
||||
<div className={`user ${focused ? 'focused' : ''} `}>
|
||||
{highlightedDisplay.display}
|
||||
</div>
|
||||
)}
|
||||
<div className={cn(
|
||||
"flex items-center gap-2 rounded-xl border bg-background px-3 transition-shadow w-full",
|
||||
focused ? "ring-2 ring-ring shadow-sm" : "border-input"
|
||||
)}>
|
||||
<Search className="h-5 w-5 shrink-0 text-muted-foreground" />
|
||||
<Mentions
|
||||
value={value}
|
||||
prefix="@"
|
||||
placeholder="Search for photos, use @ to mention people"
|
||||
onSearch={handleMentionSearch}
|
||||
onChange={onChange}
|
||||
onKeyDown={handleKeyDown}
|
||||
onFocus={() => setFocused(true)}
|
||||
onBlur={() => setFocused(false)}
|
||||
options={options}
|
||||
rows={1}
|
||||
/>
|
||||
</MentionsInput>
|
||||
{value && !loading && (
|
||||
<button
|
||||
onClick={handleClear}
|
||||
className="shrink-0 rounded-full p-1 hover:bg-muted transition-colors"
|
||||
aria-label="Clear search"
|
||||
>
|
||||
<X className="h-4 w-4 text-muted-foreground" />
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
onClick={handleSubmit}
|
||||
disabled={!value.trim() || loading}
|
||||
className="shrink-0 rounded-lg p-1.5 bg-primary text-primary-foreground hover:bg-primary/90 transition-colors disabled:opacity-50 disabled:pointer-events-none"
|
||||
aria-label="Search"
|
||||
>
|
||||
{loading ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<ArrowRight className="h-4 w-4" />
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
const findInputStyle = {
|
||||
control: {
|
||||
backgroundColor: 'hsl(var(--input))',
|
||||
fontSize: 20,
|
||||
fontWeight: 'normal',
|
||||
outline: 'none',
|
||||
borderRadius: '15px',
|
||||
|
||||
},
|
||||
|
||||
'&singleLine': {
|
||||
// display: 'inline-block',
|
||||
width: '100%',
|
||||
|
||||
highlighter: {
|
||||
padding: '10px 15px',
|
||||
border: 'none',
|
||||
outline: 'none',
|
||||
},
|
||||
input: {
|
||||
padding: '10px 15px',
|
||||
border: 'none',
|
||||
outline: 'none',
|
||||
},
|
||||
},
|
||||
|
||||
suggestions: {
|
||||
|
||||
list: {
|
||||
backgroundColor: 'hsl(var(--background))',
|
||||
fontSize: 14,
|
||||
border: '1px solid hsl(var(--border))',
|
||||
overflow: 'hidden',
|
||||
},
|
||||
item: {
|
||||
padding: '5px 15px',
|
||||
border: 'none',
|
||||
'&focused': {
|
||||
backgroundColor: 'hsl(var(--chart-2))',
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
export default findInputStyle;
|
||||
@@ -1,6 +0,0 @@
|
||||
const mentionStyle: React.CSSProperties = {
|
||||
backgroundColor: 'hsla(var(--chart-2) / 0.3)',
|
||||
position: 'relative',
|
||||
}
|
||||
|
||||
export default mentionStyle;
|
||||
@@ -51,6 +51,7 @@ export default function FindPage() {
|
||||
const { toast } = useToast(); // Initialize toast
|
||||
const { aiEnabled } = useConfig();
|
||||
const [query, setQuery] = useState('');
|
||||
const [searchedQuery, setSearchedQuery] = useState('');
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [filters, setFilters] = useState<IFindFilters>({});
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
@@ -92,7 +93,9 @@ export default function FindPage() {
|
||||
}, [filters]);
|
||||
|
||||
const handleSearch = (query: string) => {
|
||||
if (!query.trim()) return;
|
||||
setQuery(query);
|
||||
setSearchedQuery(query);
|
||||
setLoading(true);
|
||||
setError(null); // Clear previous errors
|
||||
findAssets(query)
|
||||
@@ -193,7 +196,7 @@ export default function FindPage() {
|
||||
</div>
|
||||
)
|
||||
}
|
||||
else if (query.length === 0) {
|
||||
else if (searchedQuery.length === 0) {
|
||||
return (
|
||||
<div className="flex justify-center flex-col gap-2 items-center h-full">
|
||||
<Search className='w-10 h-10 text-muted-foreground' />
|
||||
@@ -209,7 +212,10 @@ export default function FindPage() {
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="text-xs"
|
||||
onClick={() => handleSearch(suggestion.query)}
|
||||
onClick={() => {
|
||||
setQuery(suggestion.query);
|
||||
handleSearch(suggestion.query);
|
||||
}}
|
||||
>
|
||||
{suggestion.label}
|
||||
</Button>
|
||||
@@ -279,6 +285,7 @@ export default function FindPage() {
|
||||
value={query}
|
||||
onChange={setQuery}
|
||||
onSearch={handleSearch}
|
||||
loading={loading}
|
||||
/>
|
||||
</div>
|
||||
{/* renderContent now includes the Provider */}
|
||||
|
||||
@@ -117,4 +117,92 @@
|
||||
}
|
||||
.leaflet-control-container {
|
||||
z-index: 3 !important;
|
||||
}
|
||||
|
||||
/* rc-mentions styles for FindInput */
|
||||
.rc-mentions {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
width: 100%;
|
||||
font-size: 16px;
|
||||
font-weight: normal;
|
||||
background: transparent;
|
||||
border: none;
|
||||
outline: none;
|
||||
position: relative;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.rc-mentions > textarea {
|
||||
width: 100% !important;
|
||||
height: 40px !important;
|
||||
padding: 10px 0;
|
||||
border: none;
|
||||
outline: none;
|
||||
font-size: 16px;
|
||||
font-weight: normal;
|
||||
font-family: inherit;
|
||||
background: transparent;
|
||||
color: hsl(var(--foreground));
|
||||
resize: none;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.rc-mentions-measure {
|
||||
position: absolute !important;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
z-index: -1;
|
||||
color: transparent !important;
|
||||
pointer-events: none;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.rc-mentions > textarea::placeholder {
|
||||
color: hsl(var(--muted-foreground));
|
||||
}
|
||||
|
||||
.rc-mentions-dropdown {
|
||||
position: absolute;
|
||||
z-index: 50;
|
||||
background-color: hsl(var(--popover));
|
||||
border: 1px solid hsl(var(--border));
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
font-size: 14px;
|
||||
box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
|
||||
animation: mentions-dropdown-in 0.15s ease-out;
|
||||
}
|
||||
|
||||
@keyframes mentions-dropdown-in {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(-4px);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
.rc-mentions-dropdown-menu {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 4px;
|
||||
}
|
||||
|
||||
.rc-mentions-dropdown-menu-item {
|
||||
padding: 6px 12px;
|
||||
cursor: pointer;
|
||||
border-radius: 4px;
|
||||
transition: background-color 0.1s ease;
|
||||
}
|
||||
|
||||
.rc-mentions-dropdown-menu-item-active {
|
||||
background-color: hsl(var(--accent));
|
||||
color: hsl(var(--accent-foreground));
|
||||
}
|
||||
Reference in new issue
Block a user