Skip to content

Commit 2ce1b80

Browse files
committed
much better input handling
1 parent 87b0a79 commit 2ce1b80

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

src/pages/apps/bsky-follow-suggestions.tsx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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"
33
import Layout from "../../components/layout"
44
import Closer from "../../components/closer"
55
import { ProfileViewDetailed } from "@atproto/api/dist/client/types/app/bsky/actor/defs"
@@ -50,29 +50,33 @@ const Bsky = () => {
5050
// START: UI STATE
5151
// This is similar to the application state,
5252
// 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+
}
5460
const [showDetailsByScore, setShowDetailsByScore] = useState<boolean>(false)
5561
// END: UI STATE
5662

57-
const myHandle = searchParams.get("handle")
58-
5963
// Once you start getting suggestions, keep getting them until there are no more.
6064

6165
useEffect(() => {
6266
if (!done && suggestionsIndex != 0 && suggestionsIndex != -1) {
63-
const timer = setTimeout(() => getSuggestions(myHandle), requestFrequency)
67+
const timer = setTimeout(() => getSuggestions(), requestFrequency)
6468
return () => clearTimeout(timer)
6569
}
6670
}, [suggestionsIndex, done])
6771

6872
// Get a list of suggestions, then aggregate the counts of each handle.
69-
const getSuggestions = async (handle: string | null) => {
73+
const getSuggestions = async () => {
7074
if (done) return
7175

7276
// Get suggested follows from the server for the given handle.
7377
const suggestionCountsCopy = { ...suggestionCounts }
7478
const response = await fetch(
75-
`${process.env.GATSBY_API_URL}/bsky/${handle}/suggestions/${suggestionsIndex}`
79+
`${process.env.GATSBY_API_URL}/bsky/${handleState}/suggestions/${suggestionsIndex}`
7680
)
7781
if (!response.ok) {
7882
clearApplicationState()
@@ -117,7 +121,7 @@ const Bsky = () => {
117121
const suggestionsToDetail = Object.keys(suggestionCounts)
118122
.sort((a, b) => suggestionCounts[b] - suggestionCounts[a])
119123
.filter((suggestion) => suggestionCounts[suggestion] > 1)
120-
.filter((suggestion) => suggestion != myHandle)
124+
.filter((suggestion) => suggestion != handleState)
121125
.slice(0, maxSuggestions - Object.keys(suggestionDetails).length)
122126

123127
// Get details for each suggestion.
@@ -254,20 +258,23 @@ const Bsky = () => {
254258
<input
255259
type="text"
256260
className="form-control"
257-
defaultValue={myHandle || ""}
261+
defaultValue={handleState}
258262
placeholder="enter handle"
259263
aria-label="enter handle"
260264
aria-describedby="button-addon2"
261-
ref={handleRef}
265+
onChange={(value) => {
266+
setHandleState(value.target.value)
267+
setParams("handle", value.target.value)
268+
}}
262269
/>
263270
<button
264271
className="btn btn-outline-secondary"
265272
type="button"
273+
disabled={handleState.length == 0}
266274
onClick={() => {
267275
clearApplicationState()
268276
setError(null)
269-
setParams("handle", handleRef.current?.value || myHandle || "")
270-
getSuggestions(handleRef.current?.value || "")
277+
getSuggestions()
271278
setStarted(true)
272279
}}
273280
>

src/pages/apps/bsky-popularity-contest.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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"
33
import Layout from "../../components/layout"
44
import Closer from "../../components/closer"
55
import { ProfileViewDetailed } from "@atproto/api/dist/client/types/app/bsky/actor/defs"
@@ -49,10 +49,15 @@ const Bsky = () => {
4949
// START: UI STATE
5050
// This is similar to the application state,
5151
// different in that it doesn't need to be reset.
52-
const handleRef = useRef<HTMLInputElement | null>(null)
52+
const [handleState, setHandleState] = useState<string>("")
53+
if (
54+
searchParams.get("handle") != null &&
55+
searchParams.get("handle") !== handleState
56+
) {
57+
setHandleState(searchParams.get("handle") || "")
58+
}
5359
// END: UI STATE
5460

55-
const myHandle = searchParams.get("handle")
5661
const maxStarlettes =
5762
absoluteMaxStarlettes < (profile?.followsCount || 0)
5863
? absoluteMaxStarlettes
@@ -62,7 +67,7 @@ const Bsky = () => {
6267
// Check for "get profile" conditions
6368
useEffect(() => {
6469
if (started && !done) {
65-
const timer = setTimeout(() => getProfile(myHandle), requestFrequency)
70+
const timer = setTimeout(() => getProfile(handleState), requestFrequency)
6671
return () => clearTimeout(timer)
6772
}
6873
}, [started, done])
@@ -98,7 +103,7 @@ const Bsky = () => {
98103

99104
const popularityCopy = { ...popularity }
100105
const response = await fetch(
101-
`${process.env.GATSBY_API_URL}/bsky/${myHandle}/popularity/${popularityIndex}`
106+
`${process.env.GATSBY_API_URL}/bsky/${handleState}/popularity/${popularityIndex}`
102107
)
103108
if (!response.ok) {
104109
clearApplicationState()
@@ -234,19 +239,22 @@ const Bsky = () => {
234239
<input
235240
type="text"
236241
className="form-control"
237-
defaultValue={myHandle || ""}
242+
defaultValue={handleState}
238243
placeholder="enter handle"
239244
aria-label="enter handle"
240245
aria-describedby="button-addon2"
241-
ref={handleRef}
246+
onChange={(value) => {
247+
setHandleState(value.target.value)
248+
setParams("handle", value.target.value)
249+
}}
242250
/>
243251
<button
244252
className="btn btn-outline-secondary"
245253
type="button"
254+
disabled={handleState.length == 0}
246255
onClick={() => {
247256
clearApplicationState()
248257
setError(null)
249-
setParams("handle", handleRef.current?.value || myHandle || "")
250258
setStarted(true)
251259
}}
252260
>

0 commit comments

Comments
 (0)