{"id":13810120,"url":"https://github.com/UretzkyZvi/search-address","last_synced_at":"2025-05-14T10:32:16.341Z","repository":{"id":238754706,"uuid":"797458754","full_name":"UretzkyZvi/search-address","owner":"UretzkyZvi","description":null,"archived":false,"fork":false,"pushed_at":"2024-05-20T11:26:16.000Z","size":127,"stargazers_count":49,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-06-14T18:42:42.429Z","etag":null,"topics":["component","react","search-address"],"latest_commit_sha":null,"homepage":"https://search-address-ebon.vercel.app","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/UretzkyZvi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-07T21:54:46.000Z","updated_at":"2024-06-14T04:14:37.000Z","dependencies_parsed_at":"2024-05-07T23:36:34.673Z","dependency_job_id":null,"html_url":"https://github.com/UretzkyZvi/search-address","commit_stats":null,"previous_names":["uretzkyzvi/search-address"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UretzkyZvi%2Fsearch-address","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UretzkyZvi%2Fsearch-address/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UretzkyZvi%2Fsearch-address/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UretzkyZvi%2Fsearch-address/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/UretzkyZvi","download_url":"https://codeload.github.com/UretzkyZvi/search-address/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":213870383,"owners_count":15650179,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["component","react","search-address"],"created_at":"2024-08-04T02:00:46.201Z","updated_at":"2024-08-04T02:01:33.751Z","avatar_url":"https://github.com/UretzkyZvi.png","language":"TypeScript","funding_links":[],"categories":["Libs and Components","Components","Components \u0026 Libraries"],"sub_categories":[],"readme":"# SearchAddress Component\n\nThe `SearchAddress` component provides a flexible and interactive search interface for addresses, utilizing the powerful Nominatim service from OpenStreetMap. This component is built with React and integrates UI components from `shadcn-ui` and icons from `lucide-react`.\n\n## Screenshots\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./public/images/data.png\" width=\"400\" alt=\"screenshot 1\"/\u003e\n  \u003cimg src=\"./public/images/search.png\" width=\"400\" alt=\"screenshot 2\"/\u003e\n\u003c/p\u003e\n\n## Features\n\n- **Autocomplete Search**: Offers suggestions as you type, reducing the need for full address inputs and improving user experience.\n- **Grouped Results**: Displays search results grouped by address type, making it easier to find the exact location.\n- **Interactive UI**: Uses a popover to display search results that can be selected via click or touch interaction.\n- **Debounced Input**: Improves performance by debouncing the search input.\n- **Multiple Providers Support**: Uses `leaflet-geosearch` to support multiple geocoding providers.\n\n## Installation\n\n### Dependencies\n\nFirst, ensure that `shadcn-ui` components are added to your project. If not already installed, you can add them using the following command:\n\n```bash\nnpx shadcn-ui@latest add button command popover\n```\n\nAdditionally, install `lucide-react` for using icons:\n\n```bash\nnpm install lucide-react\n```\n\nOr using yarn:\n\n```bash\nyarn add lucide-react\n```\n\nNext, install `leaflet-geosearch` for geocoding support:\n\n```bash\nnpm install leaflet-geosearch\n```\n\nOr using yarn:\n\n```bash\nyarn add leaflet-geosearch\n```\n\n### Component File\n\nCopy the component file into your project:\n\n1. Download or copy the file `search-address.tsx` from the repository.\n2. Place the file in your project directory, typically under `components/ui/`.\n\n### Hook Files\n\nCopy the hook files into your project:\n\n1. Download or copy the files `use-debounce.ts` and `use-search-address.ts` from the repository.\n2. Place the files in your project directory, typically under `hooks/`.\n\n## Usage\n\n1. **Import the Component**:\n\n   Include the `SearchAddress` component in your React application by importing it:\n\n   ```javascript\n   import dynamic from \"next/dynamic\";\n\n   const SearchAddress = dynamic(() =\u003e import(\"@/components/ui/search-address\"), {\n     ssr: false,\n   });\n   ```\n\n   Using dynamic imports with SSR disabled helps avoid the `window is not defined` error during server-side rendering.\n\n2. **Utilize the Component**:\n\n   You can use the `SearchAddress` component anywhere within your React application:\n\n   ```jsx\n   function App() {\n     return (\n       \u003cdiv className=\"App\"\u003e\n         \u003cSearchAddress onSelectLocation={(location) =\u003e console.log(location)} /\u003e\n       \u003c/div\u003e\n     );\n   }\n   ```\n\n## Hooks\n\n### `useDebounce`\n\nThis hook debounces a value, improving performance by limiting the number of times a function is called.\n\n```typescript\nimport * as React from \"react\";\n\nexport function useDebounce\u003cT\u003e(value: T, delay?: number): T {\n  const [debouncedValue, setDebouncedValue] = React.useState\u003cT\u003e(value);\n\n  React.useEffect(() =\u003e {\n    const timer = setTimeout(() =\u003e setDebouncedValue(value), delay ?? 500);\n\n    return () =\u003e {\n      clearTimeout(timer);\n    };\n  }, [value, delay]);\n\n  return debouncedValue;\n}\n```\n\n### `useSearchAddress`\n\nThis hook encapsulates the search logic and state management for the `SearchAddress` component.\n\n```typescript\nimport { useState, useCallback, useEffect } from \"react\";\nimport { OpenStreetMapProvider } from \"leaflet-geosearch\";\nimport { RawResult } from \"leaflet-geosearch/dist/providers/bingProvider.js\";\nimport { SearchResult } from \"leaflet-geosearch/dist/providers/provider.js\";\nimport { useDebounce } from \"./use-debounce\";\n\ninterface UseSearchAddressResult {\n  query: string;\n  results: Record\u003cstring, SearchResult\u003cRawResult\u003e[]\u003e;\n  loading: boolean;\n  handleSearch: (value: string) =\u003e void;\n  selectedItem: SearchResult\u003cRawResult\u003e | null;\n  setSelectedItem: (item: SearchResult\u003cRawResult\u003e | null) =\u003e void;\n}\n\nexport const useSearchAddress = (): UseSearchAddressResult =\u003e {\n  const [query, setQuery] = useState(\"\");\n  const [results, setResults] = useState\u003c\n    Record\u003cstring, SearchResult\u003cRawResult\u003e[]\u003e\n  \u003e({});\n  const [loading, setLoading] = useState(false);\n  const [selectedItem, setSelectedItem] =\n    useState\u003cSearchResult\u003cRawResult\u003e | null\u003e(null);\n\n  const debouncedQuery = useDebounce(query, 500);\n\n  const groupByType = useCallback(\n    (data: SearchResult\u003cRawResult\u003e[]): Record\u003cstring, SearchResult\u003cRawResult\u003e[]\u003e =\u003e {\n      return data.reduce(\n        (acc, item) =\u003e {\n          const { raw } = item;\n\n          const rawData = raw as unknown as any;\n          const type = rawData.class;\n\n          if (!acc[type]) {\n            acc[type] = [];\n          }\n          acc[type]?.push(item);\n          return acc;\n        },\n        {} as Record\u003cstring, SearchResult\u003cRawResult\u003e[]\u003e\n      );\n    },\n    []\n  );\n\n  const handleSearch = (value: string) =\u003e {\n    setQuery(value);\n  };\n\n  useEffect(() =\u003e {\n    const fetchResults = async () =\u003e {\n      if (debouncedQuery.length \u003e 2) {\n        setLoading(true);\n        const provider = new OpenStreetMapProvider();\n        const results = await provider.search({ query: debouncedQuery });\n        setResults(groupByType(results as unknown as SearchResult\u003cRawResult\u003e[]));\n        setLoading(false);\n      } else {\n        setResults({});\n      }\n    };\n\n    fetchResults();\n  }, [debouncedQuery, groupByType]);\n\n  return {\n    query,\n    results,\n    loading,\n    handleSearch,\n    selectedItem,\n    setSelectedItem,\n  };\n};\n```\n\n## Component Dependencies\n\nThis component relies on the following external libraries and styles:\n\n- `shadcn-ui` for UI components such as Button, Command, and Popover.\n- `lucide-react` for icons like `Check` and `ChevronsUpDown`.\n- `leaflet-geosearch` for geocoding support with multiple providers.\n\n## API Configuration\n\nThe component makes requests to the `https://nominatim.openstreetmap.org/search` endpoint. Ensure that any API usage complies with the [Nominatim usage policy](https://operations.osmfoundation.org/policies/nominatim/), particularly regarding the custom HTTP header requirements for identification.\n\nFor configuration options and details on supported providers, refer to the [leaflet-geosearch documentation](https://smeijer.github.io/leaflet-geosearch/).\n\n## Contributing\n\nContributions to enhance or fix issues in the `SearchAddress` component are welcome. Please follow the standard pull request process for this repository.\n\n## License\n\nThis component is available under the MIT License. See the LICENSE file in the repository for full license text.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FUretzkyZvi%2Fsearch-address","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FUretzkyZvi%2Fsearch-address","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FUretzkyZvi%2Fsearch-address/lists"}