Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: How to add fields to search #4016

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions doc/how-to/how-to-add-fields-to-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# How to add fields to the Editor search index

## Overview

This guide outlines the process of adding new searchable fields to the PlanX Editors' frontend search functionality, which uses Fuse.js for indexing and searching. This is a required step with adding a new component to PlanX, or adding new fields to an existing component.

## Background

- Search is currently implemented in the frontend using Fuse.js
- Only certain fields are searchable:
- Text fields (simple text)
- Rich text fields (HTML)
- Data values (e.g. `data.fn`)

## Process

### 1. Update facets configuration

Location: `src/pages/FlowEditor/components/Sidebar/Search/facets.ts`

#### Guidelines:
- Use key paths to the new fields (e.g. `data.myNewField`)
- Wrap rich text fields with `richTextField()` helper - this strips HTML tags
- Add data fields to `DATA_FACETS`
- Add text fields to `ALL_FACETS`
- Avoid adding duplicate values already held in `ALL_FACETS` (e.g., `data.title`, `data.description`)

#### Example:

```ts
// facets.ts

const myNewComponent: SearchFacets = [
richTextField("data.myRichTextField"),
"data.myPlainTextField"
];

export const ALL_FACETS: SearchFacets = [
...otherComponents,
...myNewComponent,
...DATA_FACETS,
];
```

### 2. Assign display values

Location: `src/pages/FlowEditor/components/Sidebar/Search/SearchResultCard/getDisplayDetailsForResult.tsx`

#### Add key formatters:

```ts
// getDisplayDetailsForResult.tsx

const keyFormatters: KeyMap = {
...existingFormatters,
"data.myNewField": {
getDisplayKey: () => "My New Field",
},
};
```

### 3. Update tests

Locations:
- `src/pages/FlowEditor/components/Sidebar/Search/SearchResultCard/allFacets.test.ts`
- `src/pages/FlowEditor/components/Sidebar/Search/SearchResultCard/dataFacets.test.ts`

#### Test steps:
1. Add new component to `mockFlow`
2. Create mock search result
- Example: `mockMyComponentResult: SearchResult<IndexedNode>`

### Debugging tip
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


A search result can be easily logged to the console from `SearchResultCard`. Simply search for one of your new fields, and click on the card.

```ts
// SearchResultCard/index.tsx

const handleClick = () => {
const url = getURLForNode(result.item.id);
// Temporarily disable navigation
// navigate(url);

// Log the full search result to console
console.log({ result });
};
```

For reference, [please see this PR](https://github.com/theopensystemslab/planx-new/pull/4015) which added the text fields for the `Feedback` component to the search index.
4 changes: 4 additions & 0 deletions editor.planx.uk/docs/adding-a-new-component.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ import SetValue from "@planx/components/SetValue/Editor";
set: SetValueComponent,
```

6. Add text fields to search index

Please see detailed guide here - https://github.com/theopensystemslab/planx-new/blob/main/doc/how-to/how-to-add-fields-to-search.md

## Preview environment & integrations

1. `src/pages/Preview/Node.tsx`
Expand Down
Loading