Skip to content

Commit

Permalink
Usernames search (#101)
Browse files Browse the repository at this point in the history
* feat: usernames search component

* chore: branding

* chore: fix tsconfig
  • Loading branch information
michalstruck authored Nov 1, 2024
1 parent c13b828 commit 9e24a70
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Minikit-JS
# minikit-js

## 🚀 Getting Started

Minikit is currently available on npm. To install, run:
MiniKit is currently available on npm. To install, run:
`pnpm i @worldcoin/minikit-js`

or use the CDN:
Expand Down
4 changes: 2 additions & 2 deletions packages/core/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Minikit-JS
# minikit-js

## 🚀 Getting Started

Minikit is currently available on npm. To install, run:
MiniKit is currently available on npm. To install, run:
`pnpm i @worldcoin/minikit-js`

or use the CDN:
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@worldcoin/minikit-js",
"version": "1.1.1",
"homepage": "https://docs.worldcoin.org/mini-apps",
"description": "Minikit-js is our SDK for building mini-apps.",
"description": "minikit-js is our SDK for building mini-apps.",
"license": "MIT",
"private": false,
"type": "module",
Expand Down
4 changes: 2 additions & 2 deletions packages/react/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Minikit-React
# minikit-react

This package contains helper functions and components for building mini apps with React. This is not required to run a mini app and is only a convenience package for developers as it contains nice abstractions.

## 🚀 Getting Started

Minikit is currently available on npm. To install, run:
MiniKit is currently available on npm. To install, run:

```
pnpm i @worldcoin/minikit-react
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@worldcoin/minikit-react",
"version": "1.1.1",
"homepage": "https://docs.worldcoin.org/mini-apps",
"description": "Minikit-react is a set of hooks for building mini-apps",
"description": "minikit-react is a set of hooks for building mini-apps",
"license": "MIT",
"private": false,
"type": "module",
Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {
UsernameSearch,
type GetSearchedUsernameResult,
} from "./username-search";
89 changes: 89 additions & 0 deletions packages/react/src/components/username-search.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const createDebounce = () => {
let timeoutId: NodeJS.Timeout;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return <T extends (...args: any) => any>(fn: T, delay: number) => {
return (...args: Parameters<T>) => {
if (timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
fn(...args);
}, delay);
};
};
};

const DEBOUNCE_DELAY_MS = 300;
const debounce = createDebounce();

type SearchUsernameResponseBodySuccess = Array<{
address: string;
profile_picture_url: string | null;
username: string;
}>;

export type GetSearchedUsernameResult = Awaited<
ReturnType<typeof getSearchedUsername>
>;

const getSearchedUsername = async (username: string) => {
const response = await fetch(
`https://usernames.worldcoin.org/api/v1/search/${username}`
);

if (response.status === 200) {
const json = (await response.json()) as SearchUsernameResponseBodySuccess;
return { status: response.status, data: json };
}

return { status: response.status, error: "Error fetching data" };
};

type Props = {
value: string;
handleChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
setSearchedUsernames: (searchedUsernames: GetSearchedUsernameResult) => void;
className?: string;
inputProps?: React.InputHTMLAttributes<HTMLInputElement>;
};

/**
* Simple component that allows users to search for usernames.
*
* It is encouraged to build your own components, using this as a base/reference
*
* You can add more <input /> props/override existing ones by passing inputProps.
* Debounce delay is 300ms.
*/
export const UsernameSearch = ({
value,
handleChange,
setSearchedUsernames,
className,
inputProps,
}: Props) => {
const debouncedSearch = debounce(
async (e: React.ChangeEvent<HTMLInputElement>) => {
const username = e.target.value;
const data = await getSearchedUsername(username);

setSearchedUsernames(data);
},
DEBOUNCE_DELAY_MS
);

const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
debouncedSearch(e);
handleChange(e);
};

return (
<input
type="text"
value={value}
onChange={onChange}
className={className || "rounded-md border-black border-2"}
{...inputProps}
/>
);
};
1 change: 1 addition & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { useWaitForTransactionReceipt } from "./transaction/hooks";
export * from "./types/client";
export * from "./components";
1 change: 1 addition & 0 deletions packages/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"sourceMap": false,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"jsx": "react-jsx",
"noImplicitAny": false,
"skipLibCheck": true,
"baseUrl": ".",
Expand Down

0 comments on commit 9e24a70

Please sign in to comment.