|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { |
| 4 | + getCategoryID, |
| 5 | + QuickJumpsByCategory, |
| 6 | +} from "@/components/mini-apps/explore-collections/quick-jumps-by-category"; |
| 7 | +import { SuggestionCategory } from "@/components/mini-apps/explore-collections/suggestion-category"; |
| 8 | +import { |
| 9 | + NameGraphFetchTopCollectionMembersResponse, |
| 10 | + NameGraphGroupedByCategoryResponse, |
| 11 | +} from "@namehash/namegraph-sdk/utils"; |
| 12 | +import { getCollectionsForQuery } from "@/lib/utils"; |
| 13 | +import { DebounceInput } from "react-debounce-input"; |
| 14 | +import { useEffect, useState } from "react"; |
| 15 | +import lodash, { debounce } from "lodash"; |
| 16 | + |
| 17 | +const SUGGESTION_CATEGORY_CLASSNAME = "suggestionCategory"; |
| 18 | + |
| 19 | +export default function ExploreCollectionsPage() { |
| 20 | + /** |
| 21 | + * nameIdeas state: |
| 22 | + * |
| 23 | + * undefined is set when component never tried querying name ideas |
| 24 | + * null is set when component tried querying name ideas but failed |
| 25 | + * NameGraphGroupedByCategoryResponse is set when name ideas were successfully queried |
| 26 | + */ |
| 27 | + const [nameIdeas, setNameIdeas] = useState< |
| 28 | + undefined | null | NameGraphGroupedByCategoryResponse |
| 29 | + >(undefined); |
| 30 | + |
| 31 | + const [nameIdeasLoading, setNameIdeasLoading] = useState(true); |
| 32 | + |
| 33 | + const [debouncedValue, setDebouncedValue] = useState(""); |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + if (debouncedValue) { |
| 37 | + let query = debouncedValue; |
| 38 | + if (debouncedValue.includes(".")) { |
| 39 | + query = debouncedValue.split(".")[0]; |
| 40 | + } |
| 41 | + |
| 42 | + setNameIdeas(undefined); |
| 43 | + setNameIdeasLoading(true); |
| 44 | + getCollectionsForQuery(query) |
| 45 | + .then((res) => setNameIdeas(res)) |
| 46 | + .catch(() => setNameIdeas(null)) |
| 47 | + .finally(() => setNameIdeasLoading(false)); |
| 48 | + } else { |
| 49 | + setNameIdeasLoading(false); |
| 50 | + } |
| 51 | + }, [debouncedValue]); |
| 52 | + |
| 53 | + const [activeCategoryID, setActiveCategoryID] = useState(""); |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + if (nameIdeas?.categories.length && !activeCategoryID) { |
| 57 | + setActiveCategoryID(getCategoryID(nameIdeas.categories[0])); |
| 58 | + } |
| 59 | + }, [nameIdeas?.categories]); |
| 60 | + |
| 61 | + const setFirstQuickJumpPillAsActive = () => { |
| 62 | + const firstCollectionPill = document.querySelector(".collectionPill"); |
| 63 | + const categoryID = firstCollectionPill?.getAttribute( |
| 64 | + "data-navigation-item", |
| 65 | + ); |
| 66 | + |
| 67 | + if (categoryID) { |
| 68 | + setActiveCategoryID(categoryID); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + const setActiveQuickJumpPill = () => { |
| 73 | + const scrollableContainer = document.getElementById("scrollable-elm"); |
| 74 | + |
| 75 | + if (scrollableContainer) { |
| 76 | + console.log(scrollableContainer.getBoundingClientRect()); |
| 77 | + |
| 78 | + // if the container was not yet scrolled |
| 79 | + if ( |
| 80 | + scrollableContainer.scrollTop < |
| 81 | + scrollableContainer?.getBoundingClientRect().y || |
| 82 | + !scrollableContainer.scrollTop |
| 83 | + ) { |
| 84 | + setFirstQuickJumpPillAsActive(); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const containerScrollTopPosition = |
| 89 | + scrollableContainer?.getBoundingClientRect().top + |
| 90 | + scrollableContainer?.scrollTop; |
| 91 | + const containerScrollBottomPosition = |
| 92 | + scrollableContainer?.getBoundingClientRect().bottom + |
| 93 | + scrollableContainer?.scrollTop; |
| 94 | + |
| 95 | + const categories = document.querySelectorAll( |
| 96 | + `.${SUGGESTION_CATEGORY_CLASSNAME}`, |
| 97 | + ); |
| 98 | + |
| 99 | + categories.forEach((category, idx) => { |
| 100 | + if (idx === 0) { |
| 101 | + const firstCategoryBottom = category.getAttribute( |
| 102 | + "data-category-bottom", |
| 103 | + ); |
| 104 | + const secondCategoryTop = |
| 105 | + categories[idx + 1].getAttribute("data-category-top"); |
| 106 | + |
| 107 | + if ( |
| 108 | + containerScrollTopPosition <= Number(firstCategoryBottom) || |
| 109 | + containerScrollBottomPosition < Number(secondCategoryTop) |
| 110 | + ) { |
| 111 | + setActiveCategoryID(category.id); |
| 112 | + } |
| 113 | + } else { |
| 114 | + const currentCategoryTop = category.getAttribute("data-category-top"); |
| 115 | + const currentCategoryBottom = category.getAttribute( |
| 116 | + "data-category-bottom", |
| 117 | + ); |
| 118 | + |
| 119 | + if ( |
| 120 | + containerScrollTopPosition <= Number(currentCategoryBottom) && |
| 121 | + containerScrollTopPosition >= Number(currentCategoryTop) |
| 122 | + ) { |
| 123 | + setActiveCategoryID(category.id); |
| 124 | + } |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + // if the container was scrolled to its bottom |
| 129 | + if ( |
| 130 | + scrollableContainer.scrollHeight && |
| 131 | + scrollableContainer.scrollTop && |
| 132 | + scrollableContainer.clientHeight + scrollableContainer.scrollTop >= |
| 133 | + scrollableContainer.scrollHeight |
| 134 | + ) { |
| 135 | + const lastCategory = categories[categories.length - 1]; |
| 136 | + |
| 137 | + if (lastCategory) { |
| 138 | + setActiveCategoryID(lastCategory.id); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + }; |
| 143 | + const ACTIVE_QUICK_JUMP_PILL_CLASSNAME = "activeQuickJumpPill"; |
| 144 | + const clearActiveQuickJumpPills = () => { |
| 145 | + const activeCollectionPills = document.querySelectorAll( |
| 146 | + `.${ACTIVE_QUICK_JUMP_PILL_CLASSNAME}`, |
| 147 | + ); |
| 148 | + activeCollectionPills.forEach((pill) => { |
| 149 | + pill.classList.remove(ACTIVE_QUICK_JUMP_PILL_CLASSNAME); |
| 150 | + }); |
| 151 | + }; |
| 152 | + |
| 153 | + useEffect(() => { |
| 154 | + const wrapper = document.getElementById("scrollable-elm"); |
| 155 | + |
| 156 | + if (wrapper) { |
| 157 | + wrapper.addEventListener( |
| 158 | + "scroll", |
| 159 | + lodash.debounce(setActiveQuickJumpPill, 100), |
| 160 | + ); |
| 161 | + wrapper.addEventListener( |
| 162 | + "resize", |
| 163 | + lodash.debounce(setActiveQuickJumpPill, 100), |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + return () => { |
| 168 | + if (wrapper) { |
| 169 | + wrapper.removeEventListener( |
| 170 | + "scroll", |
| 171 | + lodash.debounce(setActiveQuickJumpPill, 100), |
| 172 | + ); |
| 173 | + wrapper.removeEventListener( |
| 174 | + "resize", |
| 175 | + lodash.debounce(setActiveQuickJumpPill, 100), |
| 176 | + ); |
| 177 | + } |
| 178 | + }; |
| 179 | + }, []); |
| 180 | + |
| 181 | + useEffect(() => { |
| 182 | + if (nameIdeas) { |
| 183 | + setActiveQuickJumpPill(); |
| 184 | + } |
| 185 | + }, [nameIdeas]); |
| 186 | + |
| 187 | + useEffect(() => { |
| 188 | + if (activeCategoryID) { |
| 189 | + clearActiveQuickJumpPills(); |
| 190 | + |
| 191 | + const collectionPill = document.querySelector( |
| 192 | + '[data-collection-pill="' + activeCategoryID + '"]', |
| 193 | + ); |
| 194 | + collectionPill?.classList.add(ACTIVE_QUICK_JUMP_PILL_CLASSNAME); |
| 195 | + } |
| 196 | + }, [activeCategoryID]); |
| 197 | + |
| 198 | + return ( |
| 199 | + <div className="mx-auto py-8 w-full"> |
| 200 | + <div className="flex flex-col gap-8"> |
| 201 | + <div className="flex-1"> |
| 202 | + <div className="container mx-auto px-6"> |
| 203 | + <div className="px-5 md:px-10 lg:px-[60px]"> |
| 204 | + <h1 className="text-xl font-semibold text-center w-full mb-6"> |
| 205 | + 🔎 Search for a name and see name ideas ⬇️ |
| 206 | + </h1> |
| 207 | + </div> |
| 208 | + <div className="flex space-x-2 items-center"> |
| 209 | + <DebounceInput |
| 210 | + id="query" |
| 211 | + type="text" |
| 212 | + name="query" |
| 213 | + autoComplete="off" |
| 214 | + debounceTimeout={300} |
| 215 | + onChange={(e) => setDebouncedValue(e.target.value)} |
| 216 | + className="w-full bg-gray-100 border border-gray-300 rounded-md p-3 px-4" |
| 217 | + /> |
| 218 | + {nameIdeasLoading ? ( |
| 219 | + <div role="status"> |
| 220 | + <svg |
| 221 | + aria-hidden="true" |
| 222 | + className="w-6 h-6 me-2 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" |
| 223 | + viewBox="0 0 100 101" |
| 224 | + fill="none" |
| 225 | + xmlns="http://www.w3.org/2000/svg" |
| 226 | + > |
| 227 | + <path |
| 228 | + d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" |
| 229 | + fill="currentColor" |
| 230 | + /> |
| 231 | + <path |
| 232 | + d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" |
| 233 | + fill="currentFill" |
| 234 | + /> |
| 235 | + </svg> |
| 236 | + <span className="sr-only">Loading...</span> |
| 237 | + </div> |
| 238 | + ) : debouncedValue ? ( |
| 239 | + <svg |
| 240 | + className="w-6 h-6 me-2 text-green-500 dark:text-green-400 flex-shrink-0" |
| 241 | + aria-hidden="true" |
| 242 | + xmlns="http://www.w3.org/2000/svg" |
| 243 | + fill="currentColor" |
| 244 | + viewBox="0 0 20 20" |
| 245 | + > |
| 246 | + <path d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5Zm3.707 8.207-4 4a1 1 0 0 1-1.414 0l-2-2a1 1 0 0 1 1.414-1.414L9 10.586l3.293-3.293a1 1 0 0 1 1.414 1.414Z" /> |
| 247 | + </svg> |
| 248 | + ) : null} |
| 249 | + </div> |
| 250 | + <p className="leading-6 text-sm font-semibold text-gray-600 max-w-[760px] mx-auto text-center mt-6"> |
| 251 | + Use NameGraph SDK to generate multiple name ideas suggestions for |
| 252 | + a single search. This works just as typing something like{" "} |
| 253 | + {"'Batman'"} and getting back multiple name suggestions for |
| 254 | + different categories related to Batman just as{" "} |
| 255 | + {"'Batman Creators'"}, {"'Animated Batman Films'"},{" "} |
| 256 | + {"'Batman Supporting Characters'"} and much more! |
| 257 | + </p> |
| 258 | + </div> |
| 259 | + <div className="mx-auto py-12 text-center"> |
| 260 | + <div className="w-full"> |
| 261 | + <div className="relative space-y-6"> |
| 262 | + {/* |
| 263 | + NameIdeas component uses two states for loading |
| 264 | + state management: reqLoading and allSuggestionsLoading. |
| 265 | +
|
| 266 | + reqLoading is used to manage the loading state of the |
| 267 | + entire NameIdeas component, while allSuggestionsLoading |
| 268 | + is used to manage the loading state of the QuickJumpsByCategory. |
| 269 | +
|
| 270 | + QuickJumpsByCategory loading directly affects reqLoading state. |
| 271 | + It needs to be displayed as soon as we have the categories |
| 272 | + available, so we can calculate the need of navigation arrows |
| 273 | + displaying. This is why we use allSuggestionsLoading state |
| 274 | + to manage the loading state of QuickJumpsByCategory. |
| 275 | +
|
| 276 | + Once QuickJumpsByCategory is ready (which means, once it |
| 277 | + knows the suggestedCategories and it knows wether to |
| 278 | + show navigation arrows or not) we update reqLoading, |
| 279 | + syncing the loading state of the entire NameIdeas. |
| 280 | + */} |
| 281 | + {debouncedValue && (nameIdeasLoading || nameIdeas) ? ( |
| 282 | + <> |
| 283 | + <div |
| 284 | + className={` |
| 285 | + h-8 mb-[72px] |
| 286 | + ${!nameIdeasLoading ? "sticky top-16 left-0 z-20" : ""} |
| 287 | + `} |
| 288 | + > |
| 289 | + <QuickJumpsByCategory |
| 290 | + search={debouncedValue} |
| 291 | + nameIdeas={nameIdeas} |
| 292 | + activeCategoryID={activeCategoryID} |
| 293 | + /> |
| 294 | + </div> |
| 295 | + </> |
| 296 | + ) : null} |
| 297 | + <div className="pb-5"> |
| 298 | + <div |
| 299 | + id="scrollable-elm" |
| 300 | + className="relative z-10 transition mb-6 max-h-[600px] overflow-auto bg-gray-100 shadow" |
| 301 | + > |
| 302 | + {nameIdeas |
| 303 | + ? nameIdeas.categories.map( |
| 304 | + ( |
| 305 | + category: NameGraphFetchTopCollectionMembersResponse, |
| 306 | + idx: number, |
| 307 | + ) => { |
| 308 | + return ( |
| 309 | + <div |
| 310 | + id={getCategoryID(category)} |
| 311 | + key={`${idx}-${window.innerWidth}`} |
| 312 | + className={` |
| 313 | + ${SUGGESTION_CATEGORY_CLASSNAME} |
| 314 | + -mb-4 pt-12 |
| 315 | + ${idx === 0 ? "-mt-12" : ""}`} |
| 316 | + > |
| 317 | + <SuggestionCategory category={category} /> |
| 318 | + </div> |
| 319 | + ); |
| 320 | + }, |
| 321 | + ) |
| 322 | + : null} |
| 323 | + </div> |
| 324 | + </div> |
| 325 | + </div> |
| 326 | + </div> |
| 327 | + </div> |
| 328 | + </div> |
| 329 | + </div> |
| 330 | + </div> |
| 331 | + ); |
| 332 | +} |
0 commit comments