nomadro/apps/miniprogram/h5/src/components/DestinationsSection.tsx
eric 3e5b7c2098 Monorepo super-optimize: src layout, workspace scripts, docs.
Collapse portal under src/, unify pnpm workspace/scripts, thin VIP topic re-exports, drop orphan vip scaffolds and one-shot scripts, ignore xhs/release, document root commands.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-01 00:34:04 +08:00

129 lines
4.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { View, Text, Input, Button, Picker } from '@tarojs/components'
import { useMemo, useState } from 'react'
import Taro from '@tarojs/taro'
import { DestCard } from './DestCard'
import type { Destination } from '../services/api'
const FILTERS = [
{ key: 'all', label: '全部' },
{ key: 'sea', label: '东南亚' },
{ key: 'europe', label: '欧洲' },
{ key: 'latam', label: '拉美' },
{ key: 'asia', label: '东亚' },
]
const SORTS = [
{ key: 'rating', label: '评分' },
{ key: 'cost-asc', label: '费用低→高' },
{ key: 'cost-desc', label: '费用高→低' },
{ key: 'speed', label: '网速' },
]
const isH5 = process.env.TARO_ENV === 'h5'
function goDestinations() {
if (isH5) {
Taro.navigateTo({ url: '/pages/destinations/index' }).catch(() =>
Taro.redirectTo({ url: '/pages/destinations/index' })
)
return
}
Taro.navigateTo({ url: '/pages/destinations/index' }).catch(() =>
Taro.redirectTo({ url: '/pages/destinations/index' })
)
}
type Props = {
list: Destination[]
limit?: number
}
export function DestinationsSection({ list, limit }: Props) {
const [filter, setFilter] = useState('all')
const [search, setSearch] = useState('')
const [sort, setSort] = useState('rating')
const filtered = useMemo(() => {
let rows = list.slice()
if (filter !== 'all') rows = rows.filter((d) => d.region === filter)
if (search.trim()) {
const q = search.trim().toLowerCase()
rows = rows.filter(
(d) =>
String(d.name).toLowerCase().includes(q) ||
String(d.country || '').toLowerCase().includes(q),
)
}
if (sort === 'cost-asc') rows.sort((a, b) => (a.cost || 0) - (b.cost || 0))
else if (sort === 'cost-desc') rows.sort((a, b) => (b.cost || 0) - (a.cost || 0))
else if (sort === 'speed') rows.sort((a, b) => (b.speed || 0) - (a.speed || 0))
else rows.sort((a, b) => (b.rating || 0) - (a.rating || 0))
return rows
}, [list, filter, search, sort])
const visible = typeof limit === 'number' ? filtered.slice(0, limit) : filtered
const sortIdx = Math.max(0, SORTS.findIndex((s) => s.key === sort))
return (
<View className='section section-compact' id='destinations'>
<View className='container'>
<View className='section-header'>
<Text className='section-tag'>🌴 TOP DESTINATIONS</Text>
<Text className='section-h2'>热门旅居目的地</Text>
<Text className='section-p'>按预算、网速与气候筛选,点进城市看详情</Text>
<View style={{ marginTop: '20px' }}>
<Button className='btn btn-primary btn-sm' onClick={goDestinations}>
🎯 智能匹配
</Button>
</View>
</View>
<View className='filter-search'>
<Input
className='filter-search-input'
placeholder='搜索城市 / 国家'
placeholderClass='dest-empty'
value={search}
onInput={(e) => setSearch(e.detail.value)}
/>
</View>
<View className='filter-tags'>
{FILTERS.map((f) => (
<Button
key={f.key}
className={`filter-btn${filter === f.key ? ' active' : ''}`}
onClick={() => setFilter(f.key)}
>
{f.label}
</Button>
))}
</View>
<View className='filter-sort-row'>
<Picker
mode='selector'
range={SORTS.map((s) => s.label)}
value={sortIdx}
onChange={(e) => setSort(SORTS[Number(e.detail.value)]?.key || 'rating')}
>
<View className='filter-btn active'>排序:{SORTS[sortIdx]?.label}</View>
</Picker>
</View>
<View className='dest-grid'>
{visible.map((d) => (
<DestCard key={d.slug} d={d} />
))}
</View>
{typeof limit === 'number' && filtered.length > limit ? (
<Button className='btn btn-ghost' onClick={goDestinations}>
查看全部 {filtered.length} 座城市
</Button>
) : null}
{!visible.length ? <Text className='dest-empty'>没有匹配的城市</Text> : null}
</View>
</View>
)
}