|
1 | | -import React, { useEffect, useRef, useState } from "react" |
2 | | -import { useLocation, navigate } from "@reach/router" |
| 1 | +import React, { useEffect, useState } from "react" |
| 2 | +import { useLocation } from "@reach/router" |
3 | 3 | import Layout from "../../components/layout" |
4 | 4 | import Closer from "../../components/closer" |
5 | 5 | import { ProfileViewDetailed } from "@atproto/api/dist/client/types/app/bsky/actor/defs" |
@@ -50,29 +50,33 @@ const Bsky = () => { |
50 | 50 | // START: UI STATE |
51 | 51 | // This is similar to the application state, |
52 | 52 | // different in that it doesn't need to be reset. |
53 | | - const handleRef = useRef<HTMLInputElement | null>(null) |
| 53 | + const [handleState, setHandleState] = useState<string>("") |
| 54 | + if ( |
| 55 | + searchParams.get("handle") != null && |
| 56 | + searchParams.get("handle") !== handleState |
| 57 | + ) { |
| 58 | + setHandleState(searchParams.get("handle") || "") |
| 59 | + } |
54 | 60 | const [showDetailsByScore, setShowDetailsByScore] = useState<boolean>(false) |
55 | 61 | // END: UI STATE |
56 | 62 |
|
57 | | - const myHandle = searchParams.get("handle") |
58 | | - |
59 | 63 | // Once you start getting suggestions, keep getting them until there are no more. |
60 | 64 |
|
61 | 65 | useEffect(() => { |
62 | 66 | if (!done && suggestionsIndex != 0 && suggestionsIndex != -1) { |
63 | | - const timer = setTimeout(() => getSuggestions(myHandle), requestFrequency) |
| 67 | + const timer = setTimeout(() => getSuggestions(), requestFrequency) |
64 | 68 | return () => clearTimeout(timer) |
65 | 69 | } |
66 | 70 | }, [suggestionsIndex, done]) |
67 | 71 |
|
68 | 72 | // Get a list of suggestions, then aggregate the counts of each handle. |
69 | | - const getSuggestions = async (handle: string | null) => { |
| 73 | + const getSuggestions = async () => { |
70 | 74 | if (done) return |
71 | 75 |
|
72 | 76 | // Get suggested follows from the server for the given handle. |
73 | 77 | const suggestionCountsCopy = { ...suggestionCounts } |
74 | 78 | const response = await fetch( |
75 | | - `${process.env.GATSBY_API_URL}/bsky/${handle}/suggestions/${suggestionsIndex}` |
| 79 | + `${process.env.GATSBY_API_URL}/bsky/${handleState}/suggestions/${suggestionsIndex}` |
76 | 80 | ) |
77 | 81 | if (!response.ok) { |
78 | 82 | clearApplicationState() |
@@ -117,7 +121,7 @@ const Bsky = () => { |
117 | 121 | const suggestionsToDetail = Object.keys(suggestionCounts) |
118 | 122 | .sort((a, b) => suggestionCounts[b] - suggestionCounts[a]) |
119 | 123 | .filter((suggestion) => suggestionCounts[suggestion] > 1) |
120 | | - .filter((suggestion) => suggestion != myHandle) |
| 124 | + .filter((suggestion) => suggestion != handleState) |
121 | 125 | .slice(0, maxSuggestions - Object.keys(suggestionDetails).length) |
122 | 126 |
|
123 | 127 | // Get details for each suggestion. |
@@ -254,20 +258,23 @@ const Bsky = () => { |
254 | 258 | <input |
255 | 259 | type="text" |
256 | 260 | className="form-control" |
257 | | - defaultValue={myHandle || ""} |
| 261 | + defaultValue={handleState} |
258 | 262 | placeholder="enter handle" |
259 | 263 | aria-label="enter handle" |
260 | 264 | aria-describedby="button-addon2" |
261 | | - ref={handleRef} |
| 265 | + onChange={(value) => { |
| 266 | + setHandleState(value.target.value) |
| 267 | + setParams("handle", value.target.value) |
| 268 | + }} |
262 | 269 | /> |
263 | 270 | <button |
264 | 271 | className="btn btn-outline-secondary" |
265 | 272 | type="button" |
| 273 | + disabled={handleState.length == 0} |
266 | 274 | onClick={() => { |
267 | 275 | clearApplicationState() |
268 | 276 | setError(null) |
269 | | - setParams("handle", handleRef.current?.value || myHandle || "") |
270 | | - getSuggestions(handleRef.current?.value || "") |
| 277 | + getSuggestions() |
271 | 278 | setStarted(true) |
272 | 279 | }} |
273 | 280 | > |
|
0 commit comments