|
| 1 | +# useDocsSearch |
| 2 | + |
| 3 | +## Getting Started |
| 4 | + |
| 5 | +To use the search component, you need to initialize it by passing the URL of the edge function created from the dashboard. [More details below](#edge-function). |
| 6 | + |
| 7 | +``` |
| 8 | +import { useDocsSearch } from ./index"; |
| 9 | +
|
| 10 | +function Search() { |
| 11 | + const searchUrl = "https://myhost.sqlite.cloud:8090/v2/functions/search-js"; |
| 12 | +
|
| 13 | + // Initialize the useSqlcSearch custom hook |
| 14 | + const { |
| 15 | + searchText, // Text to search for |
| 16 | + searchRes, // Search results |
| 17 | + searchError, // Error information if search fails |
| 18 | + validSearchUrl,// Boolean indicating if the search URL is valid |
| 19 | + handleSearch, // Function to handle search input changes |
| 20 | + } = useDocsSearch(searchUrl); |
| 21 | +
|
| 22 | + return ( |
| 23 | + <div> |
| 24 | + <input |
| 25 | + type="text" |
| 26 | + value={searchText} |
| 27 | + onChange={handleSearch} |
| 28 | + placeholder="Type here..." |
| 29 | + /> |
| 30 | + <pre>{JSON.stringify(searchRes, null, 2)}</pre> |
| 31 | + </div> |
| 32 | + ) |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | + |
| 37 | +## Typescript |
| 38 | +The following types can be imported and used: |
| 39 | + |
| 40 | +``` |
| 41 | +import { useDocsSearch } from ./index"; |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | +## Edge Function |
| 46 | + |
| 47 | +The code for the edge function to be created is as follows [(Edge Functions documentation)](https://docs.sqlitecloud.io/docs/introduction/edge_functions): |
| 48 | +``` |
| 49 | +const query = request.params.query; |
| 50 | +const requestid = request.params.requestid; |
| 51 | +return { |
| 52 | + data: { |
| 53 | + search: await connection.sql`SELECT url, replace(snippet(documentation, -1, '<b>', '</b>', '...', 10), x'0A', ' ') as 'snippet' FROM documentation WHERE content MATCH concat(${query}, '*') ORDER BY rank LIMIT 256;`, |
| 54 | + requestid: requestid |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | +- Replace `documentation` with the name of your database. |
| 59 | +- Select `JavaScript` as the function type. |
| 60 | +- Choose an appropriate API KEY with the correct permission to read from your database. The API KEY can be created from the dashboard's Security/API KEY section. |
| 61 | + |
| 62 | + |
| 63 | +## Demo |
| 64 | +[LIVE DEMO](https://sqlc-react-search.vercel.app/) |
| 65 | + |
| 66 | +We provide a simple example that shows how to use the component in the [example folder](https://github.com/sqlitecloud/sqlc-components/tree/main/packages/sqlc-react-search/example). |
| 67 | + |
| 68 | +To run the example, download the repo and follow these steps: |
| 69 | +1. Run `npm install` at the root directory level. |
| 70 | +2. Run `npm run start` in the `./packages/sqlc-react-search/example` subdirectory. |
| 71 | + |
| 72 | +The example code will be executed at `localhost:1234` |
| 73 | + |
0 commit comments