diff --git a/src/components/InputSuggest/SuggestByCurrentLocation/button.tsx b/src/components/InputSuggest/SuggestByCurrentLocation/button.tsx new file mode 100644 index 00000000..9eb948dc --- /dev/null +++ b/src/components/InputSuggest/SuggestByCurrentLocation/button.tsx @@ -0,0 +1,61 @@ +import { useCallback, useState } from "react"; +import styles from "./styles.module.scss"; + +const suggestLocationTexts = { + en: "Suggest by current location", + ja: "現在地から提案", +}; + +const getSuggestLocationText = () => { + const lang = navigator.language; + if (lang.startsWith("ja")) { + return suggestLocationTexts.ja; + } + return suggestLocationTexts.en; +}; + +export const SuggestByCurrentLocationButton: React.FC<{ + onClick: () => void; + onChange: ({ + coordinates, + }: { + coordinates: GeolocationCoordinates | null; + }) => void; +}> = ({ onClick, onChange }) => { + const [geoLocating, setGeoLocating] = useState(false); + + const onGeolocate = useCallback(() => { + setGeoLocating(true); + navigator.geolocation.getCurrentPosition( + (position) => { + onChange({ + coordinates: position.coords, + }); + setGeoLocating(false); + }, + (error) => { + onChange({ + coordinates: null, + }); + setGeoLocating(false); + } + ); + }, [onChange]); + + return ( +
+ +
+ ); +}; diff --git a/src/components/InputSuggest/SuggestByCurrentLocation/index.tsx b/src/components/InputSuggest/SuggestByCurrentLocation/index.tsx index 376dc034..41afbba4 100644 --- a/src/components/InputSuggest/SuggestByCurrentLocation/index.tsx +++ b/src/components/InputSuggest/SuggestByCurrentLocation/index.tsx @@ -4,46 +4,13 @@ import { useCallback, useEffect, useState } from "react"; import styles from "./styles.module.scss"; -const suggestLocationTexts = { - en: "Suggest by current location", - ja: "現在地から提案", -}; - -const getSuggestLocationText = () => { - const lang = navigator.language; - if (lang.startsWith("ja")) { - return suggestLocationTexts.ja; - } - return suggestLocationTexts.en; -}; - export const SuggestByCurrentLocation: React.FC<{ + coordinates: GeolocationCoordinates | null; onSelected?: (value: string) => void; -}> = ({ onSelected }) => { - const [geoLocating, setGeoLocating] = useState(false); - const [geoLocated, setGeoLocated] = useState(false); - const [coordinates, setCoordinates] = useState( - null - ); +}> = ({ coordinates, onSelected }) => { const [address, setAddress] = useState(null); const [suggests, setSuggests] = useState([]); - const onGeolocate = useCallback(() => { - setGeoLocating(true); - navigator.geolocation.getCurrentPosition( - (position) => { - setGeoLocating(false); - setGeoLocated(true); - setCoordinates(position.coords); - }, - (error) => { - setGeoLocating(false); - setGeoLocated(true); - setCoordinates(null); - } - ); - }, []); - useEffect(() => { if (!coordinates) { return; @@ -89,22 +56,9 @@ export const SuggestByCurrentLocation: React.FC<{ }, [address, suggests]); return ( -
-
- -
- {geoLocated && address && ( -
{address}
- )} - {!geoLocating && suggests?.length > 0 && ( +
+ {address &&
{address}
} + {coordinates && suggests?.length > 0 && (
{suggests.map((suggest) => ( +
+ ); +}; diff --git a/src/components/InputSuggest/SuggestByTrend/index.tsx b/src/components/InputSuggest/SuggestByTrend/index.tsx index 37083210..ffe67ec3 100644 --- a/src/components/InputSuggest/SuggestByTrend/index.tsx +++ b/src/components/InputSuggest/SuggestByTrend/index.tsx @@ -1,10 +1,46 @@ +import { useEffect, useState } from "react"; +import styles from "./styles.module.scss"; +import { nextPostJsonWithCache } from "@/utils/nextPostJson"; export const SuggestByTrend: React.FC<{ onSelected?: (value: string) => void; }> = ({ onSelected }) => { + const [trends, setTrends] = useState([ + "ガザ地区の避難所を表示して", + "ウクライナの首都を表示して", + ]); + + useEffect(() => { + const thisEffect = async () => { + const resJson = await nextPostJsonWithCache("/api/ai/trends", {}); + console.log(resJson.trends); + if (!resJson.trends) { + return; + } + const newTrends = resJson.trends.split("\n"); + setTrends(newTrends); + }; + //thisEffect(); + }, []); + return ( -
-
TODO: SuggestByTrend
-
+ <> +
Trends
+
+ {trends.map((trend) => { + return ( + + ); + })} +
+ ); -} \ No newline at end of file +}; diff --git a/src/components/InputSuggest/SuggestByTrend/styles.module.scss b/src/components/InputSuggest/SuggestByTrend/styles.module.scss new file mode 100644 index 00000000..b2358781 --- /dev/null +++ b/src/components/InputSuggest/SuggestByTrend/styles.module.scss @@ -0,0 +1,35 @@ +.trendButtonWrap { + flex: 46%; +} + +.trendButton { + width: 100%; + padding: 6px; + text-align: center; + margin-bottom: 8px; +} + +.suggestListHeader { + width: 100%; + margin: auto; + text-align: center; + color: rgba(236, 236, 241, 1); + background: rgba(52, 53, 65, 0.8); + backdrop-filter: blur(2px); + line-height: 2.5rem; + height: 2.5rem; +} + +.suggestListWrap { + display: flex; + flex-direction: column; + flex-wrap: no-wrap; + gap: 2%; +} + +.suggestListItem { + flex: 46%; + font-size: 0.8em; + margin-top: 12px; + padding: 6px; +} diff --git a/src/components/InputSuggest/index.tsx b/src/components/InputSuggest/index.tsx index d9b87d50..5c86b0cc 100644 --- a/src/components/InputSuggest/index.tsx +++ b/src/components/InputSuggest/index.tsx @@ -1,13 +1,54 @@ +import { useCallback, useState } from "react"; import { SuggestByCurrentLocation } from "./SuggestByCurrentLocation"; +import { SuggestByTrendButton } from "./SuggestByTrend/button"; + +import styles from "./styles.module.scss"; +import { SuggestByCurrentLocationButton } from "./SuggestByCurrentLocation/button"; import { SuggestByTrend } from "./SuggestByTrend"; export const InputSuggest: React.FC<{ onSelected?: (value: string) => void; }> = ({ onSelected }) => { + const [suggestMode, setSuggestMode] = useState<"location" | "trend" | null>( + null + ); + const [coordinates, setCoordinates] = useState( + null + ); + + const onChangeSuggestByCurrentLocation = useCallback( + ({ + coordinates, + }: { + coordinates: GeolocationCoordinates | null; + }) => { + setCoordinates(coordinates); + }, + [] + ); + return ( -
- - +
+
+ { + setSuggestMode("location"); + }} + onChange={onChangeSuggestByCurrentLocation} + /> + { + setSuggestMode("trend"); + }} + /> +
+ {suggestMode === "location" && ( + + )} + {suggestMode === "trend" && }
); -} \ No newline at end of file +}; diff --git a/src/components/InputSuggest/styles.module.scss b/src/components/InputSuggest/styles.module.scss new file mode 100644 index 00000000..3f701403 --- /dev/null +++ b/src/components/InputSuggest/styles.module.scss @@ -0,0 +1,10 @@ +.inputSuggestWrap { + width: 100%; +} + +.inputSuggestButtonsWrap { + width: 100%; + display: flex; + flex-direction: row; + gap: 2%; +}