Skip to content

Commit

Permalink
Enable url to collection tab
Browse files Browse the repository at this point in the history
  • Loading branch information
chunlaw committed Dec 6, 2023
1 parent e6ade74 commit 3e87414
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ const App = () => {
<Routes>
<Route path="/" element={<Navigate to={`/${language}`} />} />
<Route path="/:lang" element={<Main />}>
<Route
path={`collections/:collectionName`}
element={<Home />}
/>
<Route path={`route/:id`} element={<RouteEta />} />
<Route path={`route/:id/:panel`} element={<RouteEta />} />
<Route path={`settings`} element={<Settings />} />
Expand Down
10 changes: 6 additions & 4 deletions src/components/home/HomeTabbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "@mui/icons-material";
import { useTranslation } from "react-i18next";
import AppContext from "../../AppContext";
import { RouteCollection } from "../../typing";

interface HomeTabbarProps {
homeTab: HomeTabType;
Expand Down Expand Up @@ -50,7 +51,7 @@ const HomeTabbar = ({ homeTab, onChangeTab }: HomeTabbarProps) => {
<Tab
key={`collection-${idx}`}
label={collection.name}
value={`collection-${idx}`}
value={collection.name}
disableRipple
/>
))}
Expand All @@ -64,13 +65,13 @@ export type HomeTabType = "saved" | "nearby" | "collections";

export const isHomeTab = (
input: unknown,
collectionLength: number
collections: RouteCollection[]
): input is HomeTabType => {
if (input === "saved" || input === "nearby" || input === "collections") {
return true;
}
for (let i = 0; i < collectionLength; ++i) {
if (input === `collection-${i}`) {
for (let i = 0; i < collections.length; ++i) {
if (input === collections[i].name) {
return true;
}
}
Expand All @@ -81,6 +82,7 @@ const tabbarSx: SxProps<Theme> = {
background: (theme) => theme.palette.background.default,
minHeight: "36px",
[`& .MuiTab-root`]: {
textTransform: "none",
alignItems: "center",
justifyContent: "center",
paddingTop: 0,
Expand Down
5 changes: 3 additions & 2 deletions src/components/home/SwipeableList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ const SwipeableList = React.forwardRef<SwipeableListRef, SwipeableListProps>(
let ret = HOME_TAB.indexOf(defaultHometab.current);
if (ret !== -1) return ret;
for (let i = 0; i < collections.length; ++i) {
if (`collection-${i}` === defaultHometab.current) {
if (collections[i].name === defaultHometab.current) {
return i + HOME_TAB.length;
}
}
Expand All @@ -220,7 +220,7 @@ const SwipeableList = React.forwardRef<SwipeableListRef, SwipeableListProps>(
onChangeTab(
idx < HOME_TAB.length
? HOME_TAB[idx]
: `collection-${idx - HOME_TAB.length}`
: collections[idx - HOME_TAB.length].name
);
}}
>
Expand All @@ -233,6 +233,7 @@ const SwipeableList = React.forwardRef<SwipeableListRef, SwipeableListProps>(
[
onChangeTab,
getViewIdx,
collections,
SavedRouteList,
NearbyRouteList,
SmartCollectionRouteList,
Expand Down
6 changes: 4 additions & 2 deletions src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ import type { HomeTabType } from "../components/home/HomeTabbar";
import BadWeatherCard from "../components/layout/BadWeatherCard";
import SwipeableList from "../components/home/SwipeableList";
import DbRenewReminder from "../components/layout/DbRenewReminder";
import { useParams } from "react-router-dom";

const Home = () => {
const { AppTitle, geolocation, collections } = useContext(AppContext);
const { t, i18n } = useTranslation();
const { collectionName } = useParams();

const swipeableList = useRef(null);
const _homeTab = localStorage.getItem("homeTab");
const _homeTab = collectionName ?? localStorage.getItem("homeTab");
const [homeTab, setHomeTab] = useState<HomeTabType>(
isHomeTab(_homeTab, collections.length) ? _homeTab : "nearby"
isHomeTab(_homeTab, collections) ? _homeTab : "nearby"
);

useEffect(() => {
Expand Down

0 comments on commit 3e87414

Please sign in to comment.