Skip to content

Commit 4b4907c

Browse files
committed
moving audio player to routepage
1 parent 508747a commit 4b4907c

File tree

3 files changed

+76
-94
lines changed

3 files changed

+76
-94
lines changed

src/App.jsx

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
import React, { useEffect, useState, useMemo, useRef, useContext } from "react";
1+
import React, { useEffect, useState, useMemo } from "react";
22
import { Routes, Navigate, Route } from "react-router-dom";
33
import TagPage from "./components/tags/TagPage";
44
import LatLongContext from "./context/latitude-longitude-context";
5-
import AudioContext from "./context/audio-context";
65
import CacheContext from "./context/cache-context";
76
import PermissionContext from "./context/permission-context";
87
import Info from "./components/info";
98
import TagsList from "./components/tags/TagsList";
109
import RoutePage from "./components/routes/RoutePage";
11-
import ApiEndpointContext from "./context/api-endpoint-context";
1210

1311
function App() {
1412
const [geolocationAvailable, setGeolocationAvailable] = useState();
1513
const [lat, setLat] = useState(null);
1614
const [long, setLong] = useState(null);
1715
const [heading, setHeading] = useState(null);
1816
const [speed, setSpeed] = useState(null);
19-
const [source, setSource] = useState(null);
2017
const [cache, setCache] = useState({});
21-
const audioRef = useRef();
2218

2319
const cacheContext = useMemo(
2420
() => ({
@@ -38,27 +34,13 @@ function App() {
3834
[lat, long, heading, speed]
3935
);
4036

41-
const audio = useMemo(
42-
() => ({
43-
setSource,
44-
}),
45-
[setSource]
46-
);
4737
const geolocationAvailableContext = useMemo(
4838
() => ({
4939
geolocationAvailable,
5040
}),
5141
[geolocationAvailable]
5242
);
5343

54-
useEffect(() => {
55-
if (audioRef.current) {
56-
audioRef.current.pause();
57-
audioRef.current.load();
58-
audioRef.current.play();
59-
}
60-
}, [source]);
61-
6244
const updateLocation = () => {
6345
if (lat === null || long === null) {
6446
navigator.geolocation.getCurrentPosition((position) => {
@@ -113,39 +95,25 @@ function App() {
11395
requestPermissions();
11496
}, []);
11597

116-
const { fileUrl } = useContext(ApiEndpointContext);
11798

11899
return (
119100
<div className="App h-full min-h-screen w-screen p-3 text-zinc-800 dark:text-white bg-zinc-100 dark:bg-zinc-800 overflow-hidden touch-none">
120101
<LatLongContext.Provider value={contextLatLong}>
121102
<CacheContext.Provider value={cacheContext}>
122-
<AudioContext.Provider value={audio}>
123-
<PermissionContext.Provider value={geolocationAvailableContext}>
124-
<Routes>
125-
<Route path="/" element={<TagsList />} />
126-
<Route path="tag/:id" element={<TagPage />} />
127-
<Route path="route/:id" element={<RoutePage />} />
128-
<Route
129-
path="info"
130-
element={<Info geolocationAvailable={geolocationAvailable} />}
131-
/>
132-
<Route path="*" element={<Navigate to="/" />} />
133-
</Routes>
134-
</PermissionContext.Provider>
135-
</AudioContext.Provider>
103+
<PermissionContext.Provider value={geolocationAvailableContext}>
104+
<Routes>
105+
<Route path="/" element={<TagsList />} />
106+
<Route path="tag/:id" element={<TagPage />} />
107+
<Route path="route/:id" element={<RoutePage />} />
108+
<Route
109+
path="info"
110+
element={<Info geolocationAvailable={geolocationAvailable} />}
111+
/>
112+
<Route path="*" element={<Navigate to="/" />} />
113+
</Routes>
114+
</PermissionContext.Provider>
136115
</CacheContext.Provider>
137116
</LatLongContext.Provider>
138-
{audioRef && source && (
139-
// eslint-disable-next-line jsx-a11y/media-has-caption
140-
<div className="fixed left-3 bottom-0 right-3 bg-zinc-200 dark:bg-zinc-700">
141-
<audio
142-
className="w-full"
143-
ref={audioRef}
144-
controls
145-
src={`${fileUrl}${source}`}
146-
/>
147-
</div>
148-
)}
149117
</div>
150118
);
151119
}

src/components/points-of-interest/PointOfInterest.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
} from "../../util/helper";
77
import PodcastWrapper from "./PodcastWrapper";
88
import PermissionContext from "../../context/permission-context";
9-
import AudioContext from "../../context/audio-context";
109
import Image from "../Image";
1110
import { ReactComponent as CirclePlay } from "../../icons/circle-play-solid.svg";
1211
import { ReactComponent as ClosedCap } from "../../icons/closed-captioning-solid.svg";
@@ -27,9 +26,9 @@ function PointOfInterest({
2726
index,
2827
destinationChanged,
2928
nextUnlockableId,
29+
setSource
3030
}) {
3131
const { lat, long } = useContext(LatLongContext);
32-
const { setSource } = useContext(AudioContext);
3332
const [proximity, setProximity] = useState(null);
3433
const [viewSubtitles, setViewSubtitles] = useState(false);
3534
const [unlocked, setUnlocked] = useState(false);

src/components/routes/RoutePage.jsx

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable jsx-a11y/media-has-caption */
12
import { React, useEffect, useState, useContext, useRef } from "react";
23
import { useParams } from "react-router-dom";
34
import useFetch from "../../util/useFetch";
@@ -11,7 +12,7 @@ import {
1112
import PermissionContext from "../../context/permission-context";
1213
import BackButton from "../BackButton";
1314
import { ReactComponent as LocationArrow } from "../../icons/location-arrow-solid.svg";
14-
import AudioContext from "../../context/audio-context";
15+
import ApiEndpointContext from "../../context/api-endpoint-context";
1516

1617
function RoutePage() {
1718
const { id } = useParams();
@@ -25,12 +26,12 @@ function RoutePage() {
2526
const [destinationLatitude, setDestinationLatitude] = useState(0);
2627
const [destinationLongitude, setDestinationLongitude] = useState(0);
2728
const [destinationDistance, setDestinationDistance] = useState(null);
28-
const [destination, setDestination] = useState(null);
2929
const [destinationIndex, setDestinationIndex] = useState(0);
3030
const [nextUnlockableId, setNextUnlockableId] = useState(null);
31+
const [source, setSource] = useState(null);
3132
const { userLatitude, userLongitude } = useContext(LatLongContext);
3233
const { geolocationAvailable } = useContext(PermissionContext);
33-
const { audio } = useContext(AudioContext);
34+
const audioRef = useRef();
3435
const isIOS =
3536
navigator.userAgent.match(/(iPod|iPhone|iPad)/) &&
3637
navigator.userAgent.match(/AppleWebKit/);
@@ -42,6 +43,14 @@ function RoutePage() {
4243
}
4344
}, [data]);
4445

46+
useEffect(() => {
47+
if (audioRef.current) {
48+
audioRef.current.pause();
49+
audioRef.current.load();
50+
audioRef.current.play();
51+
}
52+
}, [source]);
53+
4554
useEffect(() => {
4655
if (
4756
destinationLatitude &&
@@ -132,9 +141,7 @@ function RoutePage() {
132141
(poi) => destinationPoint[0].id === poi.id
133142
);
134143
setDestinationIndex(index);
135-
setDestination(destinationPoint[0]);
136144
setNextUnlockableId(destinationPoint[0].id);
137-
138145
setDestinationLatitude(destinationPoint[0].latitude);
139146
setDestinationLongitude(destinationPoint[0].longitude);
140147
}
@@ -144,6 +151,9 @@ function RoutePage() {
144151
}, [pointsOfInterest]);
145152

146153
if (selectedRoute === null) return null;
154+
155+
const { fileUrl } = useContext(ApiEndpointContext);
156+
147157
return (
148158
<div className="flex flex-col place-items-start pb-28">
149159
<BackButton>Afslut</BackButton>
@@ -157,55 +167,60 @@ function RoutePage() {
157167
index={index + 1}
158168
destinationChanged={destinationChanged}
159169
nextUnlockableId={nextUnlockableId}
170+
setSource={setSource}
160171
/>
161172
))}
162173
</div>
163174
{/* TODO: Make room for audio player below when playing */}
164-
<div className="fixed left-3 bottom-3 right-3 bg-zinc-200 dark:bg-zinc-700 flex gap-3 rounded-lg p-3 pb-15 divide-x dark:divide-zinc-200/5">
165-
<div>
166-
<div className="text-sm text-bold">
167-
Afstand til del
168-
<span className="ml-1 px-2 font-bold rounded-full bg-emerald-700 text-zinc-100 text-sm">
169-
{destinationIndex + 1}
170-
</span>
175+
<div className="fixed flex flex-col left-3 bottom-3 right-3 bg-zinc-200 dark:bg-zinc-700 gap-3 rounded-lg p-3 pb-15 divide-x dark:divide-zinc-200/5">
176+
<div className="flex flex-row justify-between">
177+
<div>
178+
<div className="text-sm text-bold">
179+
Afstand til del
180+
<span className="ml-1 px-2 font-bold rounded-full bg-emerald-700 text-zinc-100 text-sm">
181+
{destinationIndex + 1}
182+
</span>
183+
</div>
184+
<div className="">
185+
{destinationDistance && `${destinationDistance} meter`}
186+
</div>
171187
</div>
172-
<div className="">
173-
{destinationDistance && `${destinationDistance} meter`}
188+
<div className="pl-3">
189+
{/* TODO: Make this check for compass */}
190+
{(!orientation || !angle) && (
191+
<button
192+
className="bg-zinc-700 dark:bg-zinc-200 dark:text-zinc-800 rounded text-sm py-1 px-3"
193+
type="button"
194+
onClick={() => startWaypointer()}
195+
>
196+
Vis mig vej
197+
</button>
198+
)}
199+
{/* TODO: Make this check for compass */}
200+
{orientation && angle && (
201+
<div>
202+
<div className="flex justify-between">
203+
<span className="text-sm text-bold mr-5">Retning</span>
204+
<span className="w-1/2">
205+
<LocationArrow
206+
className="inline w-5"
207+
style={{
208+
transform: `rotate(${-rotation}deg)`,
209+
}}
210+
/>
211+
</span>
212+
</div>
213+
</div>
214+
)}
174215
</div>
175216
</div>
176-
<div className="pl-3">
177-
{/* TODO: Make this check for compass */}
178-
{(!orientation || !angle) && (
179-
<button
180-
className="bg-zinc-700 dark:bg-zinc-200 dark:text-zinc-800 rounded text-sm py-1 px-3"
181-
type="button"
182-
onClick={() => startWaypointer()}
183-
>
184-
Vis mig vej
185-
</button>
186-
)}
187-
{/* TODO: Make this check for compass */}
188-
{orientation && angle && (
189-
<div>
190-
<div className="flex justify-between mb-3">
191-
<span className="text-sm text-bold">Retning</span>
192-
<span className="w-1/2">
193-
<LocationArrow
194-
className="inline w-5"
195-
style={{
196-
transform: `rotate(${-rotation}deg)`,
197-
}}
198-
/>
199-
</span>
200-
</div>
201-
{/* <div className="text-xs text-zinc-500">
202-
Lat: {userLatitude}/{latitude}
203-
</div>
204-
<div className="text-xs text-zinc-500">
205-
Long: {userLongitude}/{longitude}
206-
</div> */}
207-
</div>
208-
)}
217+
<div>
218+
<audio
219+
className="w-full"
220+
ref={audioRef}
221+
controls
222+
src={`${fileUrl}${source}`}
223+
/>
209224
</div>
210225
</div>
211226
<div ref={bottomRef} />

0 commit comments

Comments
 (0)