JMHZ číselník obcí
import { createFileRoute } from '@tanstack/react-router'
import { useEffect, useId, useMemo, useRef, useState } from 'react'
import type { KeyboardEvent } from 'react'
import { municipalities } from '../data/municipalities'
export const Route = createFileRoute('/')({ component: App })
type Municipality = (typeof municipalities)[number]
const RECENT_MUNICIPALITIES_KEY = 'municipality-search-recent'
function normalise(value: string) {
return value
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.toLocaleLowerCase('cs-CZ')
.trim()
}
function SearchIcon() {
return (
)
}
function CopyIcon() {
return (
)
}
function App() {
const dataState = 'ready'
const [query, setQuery] = useState('')
const [isOpen, setIsOpen] = useState(false)
const [activeIndex, setActiveIndex] = useState(-1)
const [selected, setSelected] = useState(null)
const [copied, setCopied] = useState(false)
const [recentMunicipalities, setRecentMunicipalities] = useState([])
const [historyReady, setHistoryReady] = useState(false)
const inputRef = useRef(null)
const listId = useId()
useEffect(() => {
try {
const stored = window.localStorage.getItem(RECENT_MUNICIPALITIES_KEY)
if (!stored) return
const parsed: unknown = JSON.parse(stored)
if (!Array.isArray(parsed)) return
const validRecords = parsed
.filter((item): item is Municipality =>
typeof item === 'object' &&
item !== null &&
'municipalityCode' in item &&
typeof item.municipalityCode === 'string' &&
municipalities.some((municipality) => municipality.municipalityCode === item.municipalityCode),
)
.slice(0, 10)
setRecentMunicipalities(validRecords)
} catch (error) {
console.error('Recent municipality history could not be read:', error)
} finally {
setHistoryReady(true)
}
}, [])
useEffect(() => {
if (!historyReady) return
try {
window.localStorage.setItem(RECENT_MUNICIPALITIES_KEY, JSON.stringify(recentMunicipalities))
} catch (error) {
console.error('Recent municipality history could not be saved:', error)
}
}, [historyReady, recentMunicipalities])
const results = useMemo(() => {
const search = normalise(query)
if (search.length < 2) return []
return municipalities
.filter((item) => normalise(item.municipalityName).includes(search))
.sort((a, b) => {
const aName = normalise(a.municipalityName)
const bName = normalise(b.municipalityName)
const aStarts = aName.startsWith(search) ? 0 : 1
const bStarts = bName.startsWith(search) ? 0 : 1
return aStarts - bStarts || aName.length - bName.length || a.municipalityName.localeCompare(b.municipalityName, 'cs')
})
.slice(0, 10)
}, [municipalities, query])
const hasSearch = normalise(query).length >= 2
const showList = isOpen && hasSearch && dataState === 'ready'
function choose(item: Municipality) {
setSelected(item)
setRecentMunicipalities((previous) => [
item,
...previous.filter((recent) => recent.municipalityCode !== item.municipalityCode),
].slice(0, 10))
setQuery(item.municipalityName)
setIsOpen(false)
setActiveIndex(-1)
setCopied(false)
inputRef.current?.focus()
}
function handleKeyDown(event: KeyboardEvent) {
if (!showList) {
if (event.key === 'ArrowDown' && results.length) {
event.preventDefault()
setIsOpen(true)
setActiveIndex(0)
}
return
}
if (event.key === 'ArrowDown') {
event.preventDefault()
setActiveIndex((index) => (index + 1) % results.length)
}
if (event.key === 'ArrowUp') {
event.preventDefault()
setActiveIndex((index) => (index <= 0 ? results.length - 1 : index - 1))
}
if (event.key === 'Enter' && activeIndex >= 0) {
event.preventDefault()
choose(results[activeIndex])
}
if (event.key === 'Escape') {
setIsOpen(false)
setActiveIndex(-1)
}
}
async function copyCode() {
if (!selected) return
setCopied(true)
try {
await navigator.clipboard.writeText(selected.municipalityCode)
} catch {
const textarea = document.createElement('textarea')
textarea.value = selected.municipalityCode
textarea.style.position = 'fixed'
textarea.style.opacity = '0'
document.body.appendChild(textarea)
textarea.select()
document.execCommand('copy')
textarea.remove()
}
window.setTimeout(() => setCopied(false), 2200)
}
return (
{
setQuery(event.target.value)
setSelected(null)
setCopied(false)
setIsOpen(true)
setActiveIndex(-1)
}}
onFocus={() => hasSearch && setIsOpen(true)}
onKeyDown={handleKeyDown}
role="combobox"
aria-autocomplete="list"
aria-controls={listId}
aria-expanded={showList}
aria-activedescendant={activeIndex >= 0 ? `${listId}-${activeIndex}` : undefined}
aria-describedby="search-help"
placeholder="Zadejte název města nebo obce…"
className="h-14 min-w-0 flex-1 bg-transparent text-lg outline-none placeholder:text-sea-ink-soft/65"
data-testid="municipality-search"
/>
{selected && (
)}
{recentMunicipalities.length > 0 && (
{recentMunicipalities.length}/10
)}
)
}
ČESKÝ ČÍSELNÍK OBCÍ
Vyhledávač obcí
Vyhledejte město nebo obec a zjistěte její kód.
Po dvou písmenech se zobrazí seznam obcí. K pohybu použijte šipky a výběr potvrďte Enterem.
{showList && (
{results.length ? (
results.map((item, index) => (
))
) : (
)}
)}
Obec nenalezena
Zkuste upravit hledaný název.
{selected.municipalityName}
okres {selected.districtName}
KÓD OBCE
- Správní obvod
- {selected.administrativeDistrictName}
HISTORIE
Poslední hledané obce
{recentMunicipalities.map((item) => (
))}
Aktuální číselník obcí RÚIAN
