Skip to content

Commit

Permalink
WIP トレンドから提案
Browse files Browse the repository at this point in the history
  • Loading branch information
yuiseki committed Jan 7, 2024
1 parent d7174a0 commit 072102c
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 61 deletions.
61 changes: 61 additions & 0 deletions src/components/InputSuggest/SuggestByCurrentLocation/button.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className={styles.currentLocationButtonWrap}>
<button
className={styles.currentLocationButton}
onClick={() => {
onGeolocate();
onClick();
}}
disabled={geoLocating}
>
{geoLocating
? `${getSuggestLocationText()}...`
: getSuggestLocationText()}
</button>
</div>
);
};
56 changes: 5 additions & 51 deletions src/components/InputSuggest/SuggestByCurrentLocation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<GeolocationCoordinates | null>(
null
);
}> = ({ coordinates, onSelected }) => {
const [address, setAddress] = useState<string | null>(null);
const [suggests, setSuggests] = useState<string[]>([]);

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;
Expand Down Expand Up @@ -89,22 +56,9 @@ export const SuggestByCurrentLocation: React.FC<{
}, [address, suggests]);

return (
<div>
<div>
<button
onClick={onGeolocate}
disabled={geoLocating}
className={styles.geolocateButton}
>
{geoLocating
? `${getSuggestLocationText()}...`
: getSuggestLocationText()}
</button>
</div>
{geoLocated && address && (
<div className={styles.suggestAddress}>{address}</div>
)}
{!geoLocating && suggests?.length > 0 && (
<div className={styles.geolocationWrap}>
{address && <div className={styles.suggestAddress}>{address}</div>}
{coordinates && suggests?.length > 0 && (
<div className={styles.suggestListWrap}>
{suggests.map((suggest) => (
<button
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
.geolocateButton {
width: 50%;
.currentLocationButtonWrap {
flex: 46%;
}

.currentLocationButton {
width: 100%;
padding: 6px;
text-align: center;
margin-bottom: 8px;
}

.geolocationWrap {
flex: 46%;
}

.suggestAddress {
width: 100%;
margin: auto;
Expand Down
26 changes: 26 additions & 0 deletions src/components/InputSuggest/SuggestByTrend/button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import styles from "./styles.module.scss";

const suggestLocationTexts = {
en: "Suggest by trend",
ja: "トレンドから提案",
};

const getSuggestTrendText = () => {
const lang = navigator.language;
if (lang.startsWith("ja")) {
return suggestLocationTexts.ja;
}
return suggestLocationTexts.en;
};

export const SuggestByTrendButton: React.FC<{
onClick?: () => void;
}> = ({ onClick }) => {
return (
<div className={styles.trendButtonWrap}>
<button className={styles.trendButton} onClick={onClick} disabled={true}>
{getSuggestTrendText()}
</button>
</div>
);
};
44 changes: 40 additions & 4 deletions src/components/InputSuggest/SuggestByTrend/index.tsx
Original file line number Diff line number Diff line change
@@ -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<string[]>([
"ガザ地区の避難所を表示して",
"ウクライナの首都を表示して",
]);

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 (
<div>
<div>TODO: SuggestByTrend</div>
</div>
<>
<div className={styles.suggestListHeader}>Trends</div>
<div className={styles.suggestListWrap}>
{trends.map((trend) => {
return (
<button
className={styles.suggestListItem}
key={trend}
onClick={() => {
onSelected && onSelected(trend);
}}
>
{trend}
</button>
);
})}
</div>
</>
);
}
};
35 changes: 35 additions & 0 deletions src/components/InputSuggest/SuggestByTrend/styles.module.scss
Original file line number Diff line number Diff line change
@@ -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;
}
49 changes: 45 additions & 4 deletions src/components/InputSuggest/index.tsx
Original file line number Diff line number Diff line change
@@ -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<GeolocationCoordinates | null>(
null
);

const onChangeSuggestByCurrentLocation = useCallback(
({
coordinates,
}: {
coordinates: GeolocationCoordinates | null;
}) => {
setCoordinates(coordinates);
},
[]
);

return (
<div>
<SuggestByCurrentLocation onSelected={onSelected} />
<SuggestByTrend onSelected={onSelected} />
<div className={styles.inputSuggestWrap}>
<div className={styles.inputSuggestButtonsWrap}>
<SuggestByCurrentLocationButton
onClick={() => {
setSuggestMode("location");
}}
onChange={onChangeSuggestByCurrentLocation}
/>
<SuggestByTrendButton
onClick={() => {
setSuggestMode("trend");
}}
/>
</div>
{suggestMode === "location" && (
<SuggestByCurrentLocation
coordinates={coordinates}
onSelected={onSelected}
/>
)}
{suggestMode === "trend" && <SuggestByTrend onSelected={onSelected} />}
</div>
);
}
};
10 changes: 10 additions & 0 deletions src/components/InputSuggest/styles.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.inputSuggestWrap {
width: 100%;
}

.inputSuggestButtonsWrap {
width: 100%;
display: flex;
flex-direction: row;
gap: 2%;
}

1 comment on commit 072102c

@vercel
Copy link

@vercel vercel bot commented on 072102c Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.