-
Notifications
You must be signed in to change notification settings - Fork 146
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
Implements search based on SurrealDB #24
Conversation
✅ Deploy Preview for surrealdb-docs ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Use std color for the url Keep the keywords when closing and reopening the search dialog
src/theme/SearchBar/search.ts
Outdated
} | ||
|
||
function findBiggestOffsets(offsets) { | ||
return offsets.map(({ s, e }) => e - s).reduce((a, b) => a + b, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using .map followed by .reduce, you can use a single .reduce to calculate the total length of offsets:
function findBiggestOffsets(offsets: Offset[]): number {
return offsets.reduce((sum, { s, e }) => sum + (e - s), 0);
}
src/theme/SearchBar/search.ts
Outdated
// - group offsets per line (see groupOffsets() function) | ||
// - Sort descending based on the length of the offsets | ||
// - pick the biggest offset | ||
const [linenumber, offsets] = Object.entries(doc.offsets).sort( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of Object.entries(doc.offsets).sort(...)[0] to get the biggest offset, you could use a loop to find the largest without sorting, which would reduce the operation from O(n log n) to O(n). Sorting just to get the maximum value isn't efficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let maxOffsetSize = -1;
let linenumber = null;
let offsets = null;
for (const [line, off] of Object.entries(doc.offsets)) {
const currentOffsetSize = findBiggestOffsets(off);
if (currentOffsetSize > maxOffsetSize) {
maxOffsetSize = currentOffsetSize;
linenumber = line;
offsets = off;
}
}
This above loop iterates over each entry of doc.offsets just once, computes the size of the offsets, and keeps track of the largest value seen so far. This is a direct O(n) operation, which is slightly more efficient than the O(n log n) operation from sorting, especially for large numbers of offsets.
}; | ||
|
||
async function query(sql: string) { | ||
const raw = await fetch('https://blog-db.surrealdb.com/sql', { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just out of interest, can we check that the server hosting 'https://blog-db.surrealdb.com/sql' is using compression like gzip for responses, which reduces the amount of data transmitted over the network?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ref's Usage:
dialogRef
is being used in the handleChange
function, but it doesn't seem to be actually utilised. This makes it unnecessary to include it in the dependency array of the useCallback.
The same applies to the useEffect hook, which means the dialogRef dependency can be omitted entirely from both hooks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's generally recommended to use the strict equality (===) instead of loose equality (==). In the conditional rendering section, you've used keywords == '', which should ideally be keywords === ''.
src/theme/SearchBar/index.tsx
Outdated
const [results, setResults] = useState<Doc[]>([]); | ||
|
||
const handleChange = useCallback(async (value: string) => { | ||
console.log('handleChange: ' + value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove console.log
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the search API (search function) can be potentially slow or rate-limited, it would be a good idea to debounce the input changes so that not every keystroke triggers an API call.
function debounce(func, delay) {
let inDebounce;
return function(...args) {
const context = this;
clearTimeout(inDebounce);
inDebounce = setTimeout(() => func.apply(context, args), delay);
};
}
const debouncedSearch = useCallback(debounce(async (value) => {
if (!value) {
setResults([]);
setKeywords('');
} else if (value !== keywords) {
setKeywords(value);
try {
let res = await search(value);
console.log(res);
setResults(res);
} catch (err) {
console.error(err);
}
}
}, 300), []); // Here, 300ms is the delay, we can re-adjust that if needed.
const handleChange = (value) => {
debouncedSearch(value);
};
Great review @dimitrianoudi! Everything's been applied |
package.json
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks great, although it misses the Lodash dependancy.
Can you please do a:
pnpm install lodash
then commit the package.json and pnpm-lock.yaml and that should do!
Implements the search backed by SurrealDB :)