Skip to content

Commit

Permalink
refactor: onSelected -> onSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
yuiseki committed Nov 9, 2024
1 parent 084a4a9 commit 3037cad
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 43 deletions.
42 changes: 18 additions & 24 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TextInput } from "@/components/TextInput";
import { DialogueElement } from "@/types/DialogueElement";
import { nextPostJson, nextPostJsonWithCache } from "@/utils/nextPostJson";
import { sleep } from "@/utils/sleep";
import { useCallback, useEffect, useRef, useState } from "react";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { MapProvider, MapRef } from "react-map-gl/maplibre";
import { FeatureCollection } from "geojson";
import { getOverpassResponseJsonWithCache } from "@/utils/getOverpassResponse";
Expand Down Expand Up @@ -41,6 +41,7 @@ export default function Home() {
const [geojsonWithStyleList, setGeojsonWithStyleList] = useState<
Array<{ id: string; style: TridentMapsStyle; geojson: FeatureCollection }>
>([]);

const [location, setLocation] = useState<string | undefined>(undefined);

// base maps style state
Expand All @@ -66,24 +67,6 @@ export default function Home() {
// input ref and state
const [inputText, setInputText] = useState("");

const onChangeFloatingChatButton = useCallback(
(showing: boolean) => {
setShowingFloatingChat(showing);
if (showing) {
scrollToBottom();
}
},
[scrollToBottom]
);

// base maps style change
const onSelectMapStyleJsonUrl = useCallback(
(e: React.ChangeEvent<HTMLSelectElement>) => {
setMapStyleJsonUrl(e.target.value);
},
[setMapStyleJsonUrl]
);

const insertNewDialogue = useCallback(
(newDialogueElement: DialogueElement, lazy?: boolean) => {
if (!lazy) {
Expand Down Expand Up @@ -244,7 +227,7 @@ export default function Home() {
[inputText, insertNewDialogue, pastMessages, scrollToBottom]
);

const onSelectedSuggestions = useCallback(
const onSelectSuggestions = useCallback(
async (value: string) => {
setResponding(true);
await onSubmit(value);
Expand Down Expand Up @@ -355,7 +338,11 @@ export default function Home() {
<div className="tridentMapWrap">
<MapStyleSelector
mapStyleJsonUrl={mapStyleJsonUrl}
onSelectMapStyleJsonUrl={onSelectMapStyleJsonUrl}
onSelectMapStyleJsonUrl={(
e: React.ChangeEvent<HTMLSelectElement>
) => {
setMapStyleJsonUrl(e.target.value);
}}
/>
<MapProvider>
<BaseMap
Expand All @@ -378,7 +365,14 @@ export default function Home() {
})}
</BaseMap>
</MapProvider>
<FloatingChatButton onChange={onChangeFloatingChatButton}>
<FloatingChatButton
onChange={(showing: boolean) => {
setShowingFloatingChat(showing);
if (showing) {
scrollToBottom();
}
}}
>
<div className="logsOuterWrap" ref={dialogueRef}>
<div className="tridentMapTitle">
{mapTitle ? mapTitle : "Loading..."}
Expand All @@ -400,7 +394,7 @@ export default function Home() {
<LocationProvider locationInfo={{ location: location }}>
{dialogueList.length === 1 && inputText.length === 0 && (
<InputSuggest
onSelected={onSelectedSuggestions}
onSelect={onSelectSuggestions}
onChangeLocation={(v) => {
setLocation(v);
}}
Expand All @@ -415,7 +409,7 @@ export default function Home() {
onUpdateSuggestions={() => {
scrollToBottom();
}}
onSelected={onSelectedSuggestions}
onSelect={onSelectSuggestions}
/>
)}
</LocationProvider>
Expand Down
6 changes: 3 additions & 3 deletions src/components/InputPredict/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import styles from "./styles.module.scss";
export const InputPredict: React.FC<{
pastMessages?: any[];
onUpdateSuggestions?: () => void;
onSelected?: (value: string) => void;
}> = ({ pastMessages, onUpdateSuggestions, onSelected }) => {
onSelect?: (value: string) => void;
}> = ({ pastMessages, onUpdateSuggestions, onSelect }) => {
const locationInfo = useContext(LocationContext);
const [requesting, setRequesting] = useState(false);
const [suggests, setSuggests] = useState<string[] | undefined>(undefined);
Expand Down Expand Up @@ -50,7 +50,7 @@ export const InputPredict: React.FC<{
className={styles.suggestListItem}
key={suggest}
onClick={() => {
onSelected && onSelected(suggest);
onSelect && onSelect(suggest);
}}
>
{suggest}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import styles from "./styles.module.scss";
export const SuggestByCurrentLocation: React.FC<{
coordinates: GeolocationCoordinates | null;
onChangeLocation?: (location: string) => void;
onSelected?: (value: string) => void;
}> = ({ coordinates, onChangeLocation, onSelected }) => {
onSelect?: (value: string) => void;
}> = ({ coordinates, onChangeLocation, onSelect }) => {
const [location, setLocation] = useState<string | null>(null);
const [suggests, setSuggests] = useState<string[]>([]);

Expand Down Expand Up @@ -70,8 +70,8 @@ export const SuggestByCurrentLocation: React.FC<{
key={suggest}
className={styles.suggestListItem}
onClick={() => {
if (onSelected) {
onSelected(suggest);
if (onSelect) {
onSelect(suggest);
}
}}
>
Expand Down
6 changes: 3 additions & 3 deletions src/components/InputSuggest/SuggestByTrend/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import styles from "./styles.module.scss";
import { nextPostJsonWithCache } from "@/utils/nextPostJson";

export const SuggestByTrend: React.FC<{
onSelected?: (value: string) => void;
}> = ({ onSelected }) => {
onSelect?: (value: string) => void;
}> = ({ onSelect }) => {
const [trends, setTrends] = useState<string[]>([
"ガザ地区の避難所を表示して",
"ウクライナの首都を表示して",
Expand Down Expand Up @@ -33,7 +33,7 @@ export const SuggestByTrend: React.FC<{
className={styles.suggestListItem}
key={trend}
onClick={() => {
onSelected && onSelected(trend);
onSelect && onSelect(trend);
}}
>
{trend}
Expand Down
14 changes: 5 additions & 9 deletions src/components/InputSuggest/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { SuggestByCurrentLocationButton } from "./SuggestByCurrentLocation/butto
import { SuggestByTrend } from "./SuggestByTrend";

export const InputSuggest: React.FC<{
onSelected?: (value: string) => void;
onSelect?: (value: string) => void;
onChangeLocation?: (location: string) => void;
}> = ({ onSelected, onChangeLocation }) => {
}> = ({ onSelect, onChangeLocation }) => {
const [suggestMode, setSuggestMode] = useState<"location" | "trend" | null>(
null
);
Expand All @@ -18,11 +18,7 @@ export const InputSuggest: React.FC<{
);

const onChangeSuggestByCurrentLocation = useCallback(
({
coordinates,
}: {
coordinates: GeolocationCoordinates | null;
}) => {
({ coordinates }: { coordinates: GeolocationCoordinates | null }) => {
setCoordinates(coordinates);
},
[]
Expand All @@ -46,11 +42,11 @@ export const InputSuggest: React.FC<{
{suggestMode === "location" && (
<SuggestByCurrentLocation
coordinates={coordinates}
onSelected={onSelected}
onSelect={onSelect}
onChangeLocation={onChangeLocation}
/>
)}
{suggestMode === "trend" && <SuggestByTrend onSelected={onSelected} />}
{suggestMode === "trend" && <SuggestByTrend onSelect={onSelect} />}
</div>
);
};

0 comments on commit 3037cad

Please sign in to comment.