💄 Redesign stop search menu to follow the passage display style

This commit is contained in:
2023-03-05 13:46:25 +01:00
parent f09ba4cc58
commit 4a2fadb5b3
5 changed files with 390 additions and 118 deletions

View File

@@ -0,0 +1,43 @@
import { createContext, JSX } from 'solid-js';
import { createStore } from "solid-js/store";
import { Stop } from './types';
export interface AppContextStore {
getDisplayedStops: () => Stop[];
setDisplayedStops: (stops: Stop[]) => void;
};
export const AppContextContext = createContext<AppContextStore>();
export function AppContextProvider(props: { children: JSX.Element }) {
type Store = {
displayedStops: Stop[];
};
const [store, setStore] = createStore<Store>({
displayedStops: [],
});
const getDisplayedStops = (): Stop[] => {
return store.displayedStops;
}
const setDisplayedStops = (stops: Stop[]): void => {
console.log("setDisplayedStops=", stops);
// setStore((s: Store) => {
setStore('displayedStops', stops);
// return s;
// });
}
return (
<AppContextContext.Provider value={{
getDisplayedStops, setDisplayedStops,
}}>
{props.children}
</AppContextContext.Provider>
);
};

View File

@@ -5,19 +5,12 @@ import { IconButton, Menu, MenuTrigger, MenuContent, MenuItem } from "@hope-ui/s
import { format } from "date-fns";
import { BusinessDataContext, BusinessDataStore } from "./businessData";
import { SearchContext, SearchStore } from "./search";
import { AppContextContext, AppContextStore } from "./appContext";
import { getTransportModeSrc } from "./utils";
import { getTransportModeSrc, PositionedPanel } from "./utils";
import { PassagesPanel } from "./passagesPanel";
import { IconHamburgerMenu } from './extra/iconHamburgerMenu';
type PositionnedPanel = {
position: number;
// TODO: Should be PassagesPanelComponent ?
panel: JSX.Element;
};
import "./passagesDisplay.scss";
@@ -27,8 +20,8 @@ interface PassagesDisplayStore {
disablePassagesRefresh: () => void;
togglePassagesRefresh: () => void;
getPanels: () => Array<PositionnedPanel>;
setPanels: (panels: Array<PositionnedPanel>) => void;
getPanels: () => PositionedPanel[];
setPanels: (panels: PositionedPanel[]) => void;
getDisplayedPanelId: () => number;
setDisplayedPanelId: (panelId: number) => void;
@@ -40,7 +33,7 @@ function PassagesDisplayProvider(props: { children: JSX.Element }) {
type Store = {
refreshEnabled: boolean;
panels: Array<PositionnedPanel>;
panels: PositionedPanel[];
displayedPanelId: number;
};
@@ -62,10 +55,10 @@ function PassagesDisplayProvider(props: { children: JSX.Element }) {
setStore('refreshEnabled', !store.refreshEnabled);
}
const getPanels = (): Array<PositionnedPanel> => {
const getPanels = (): PositionedPanel[] => {
return store.panels;
}
const setPanels = (panels: Array<PositionnedPanel>): void => {
const setPanels = (panels: PositionedPanel[]): void => {
setStore('panels', panels);
}
@@ -88,7 +81,6 @@ function PassagesDisplayProvider(props: { children: JSX.Element }) {
);
}
// TODO: Sort transport modes by weight
const Header: VoidComponent<{ title: string }> = (props) => {
@@ -193,17 +185,16 @@ const Footer: VoidComponent<{}> = () => {
const Body: ParentComponent<{ maxPassagesPerPanel: number, syncPeriodMsec: number, panelSwitchPeriodMsec: number }> = (props) => {
const businessDataStore: BusinessDataStore | undefined = useContext(BusinessDataContext);
const appContextStore: AppContextStore | undefined = useContext(AppContextContext);
const passagesDisplayStore: PassagesDisplayStore | undefined = useContext(PassagesDisplayContext);
// TODO: Use props instead
const searchStore: SearchStore | undefined = useContext(SearchContext);
if (businessDataStore === undefined || passagesDisplayStore === undefined || searchStore === undefined) {
if (businessDataStore === undefined || appContextStore === undefined || passagesDisplayStore === undefined) {
return <div />;
}
const { getLineDestinations, passages, getPassagesLineIds, clearPassages, refreshPassages } = businessDataStore;
const { isPassagesRefreshEnabled, getDisplayedPanelId, setDisplayedPanelId, getPanels, setPanels } = passagesDisplayStore;
const { getDisplayedStops } = searchStore;
const { getDisplayedStops } = appContextStore;
setInterval(() => {
let nextPanelId = getDisplayedPanelId() + 1;
@@ -249,7 +240,7 @@ const Body: ParentComponent<{ maxPassagesPerPanel: number, syncPeriodMsec: numbe
setPanels([]);
let newPanels = [];
let positioneds: PositionnedPanel[] = [];
let positioneds: PositionedPanel[] = [];
let index = 0;
let lineIds: string[] = [];

View File

@@ -1,13 +1,16 @@
import { createContext, JSX } from 'solid-js';
import { batch, createContext, JSX } from 'solid-js';
import { createStore } from 'solid-js/store';
import { Marker as LeafletMarker } from 'leaflet';
import { Stop } from './types';
import { Stop, Stops } from './types';
export type ByStopIdMarkers = Record<number, LeafletMarker[] | undefined>;
export interface SearchStore {
getFoundStops: () => Stop[];
setFoundStops: (stops: Stop[]) => void;
getDisplayedStops: () => Stop[];
setDisplayedStops: (stops: Stop[]) => void;
@@ -19,12 +22,20 @@ export const SearchContext = createContext<SearchStore>();
export function SearchProvider(props: { children: JSX.Element }) {
type Store = {
stops: Record<number, Stop | undefined>;
foundStops: Stop[];
markers: ByStopIdMarkers;
displayedStops: Stop[];
};
const [store, setStore] = createStore<Store>({ stops: {}, markers: {}, displayedStops: [] });
const [store, setStore] = createStore<Store>({ foundStops: [], markers: {}, displayedStops: [] });
const getFoundStops = (): Stop[] => {
return store.foundStops;
}
const setFoundStops = (stops: Stop[]): void => {
setStore('foundStops', stops);
}
const getDisplayedStops = (): Stop[] => {
return store.displayedStops;
@@ -42,7 +53,7 @@ export function SearchProvider(props: { children: JSX.Element }) {
}
return (
<SearchContext.Provider value={{ getDisplayedStops, setDisplayedStops, addMarkers }}>
<SearchContext.Provider value={{ getFoundStops, setFoundStops, getDisplayedStops, setDisplayedStops, addMarkers }}>
{props.children}
</SearchContext.Provider>
);

View File

@@ -1,20 +1,160 @@
import { createEffect, createResource, createSignal, For, JSX, onMount, Show, useContext, VoidComponent } from 'solid-js';
import { Box, Button, Input, InputLeftAddon, InputGroup, HStack, List, ListItem, Progress, ProgressIndicator, VStack } from "@hope-ui/solid";
import 'leaflet/dist/leaflet.css';
import { createContext, createEffect, createResource, For, JSX, onMount, ParentComponent, Show, useContext, VoidComponent } from 'solid-js';
import { createStore } from "solid-js/store";
import { createScrollPosition } from "@solid-primitives/scroll";
import { Input, InputLeftAddon, InputGroup } from "@hope-ui/solid";
import {
featureGroup as leafletFeatureGroup, LatLngLiteral as LeafletLatLngLiteral, Map as LeafletMap,
Marker as LeafletMarker, tileLayer as leafletTileLayer
} from 'leaflet';
import { BusinessDataContext, BusinessDataStore } from "./businessData";
import { SearchContext, SearchStore } from './search';
import { Stop } from './types';
import { renderLineTransportMode, renderLinePicto, TransportModeWeights } from './utils';
import { PositionedPanel, renderLineTransportMode, renderLinePicto, TransportModeWeights } from './utils';
import styles from './stopManager.module.css';
import { AppContextContext, AppContextStore } from "./appContext";
import { BusinessDataContext, BusinessDataStore } from "./businessData";
import "leaflet/dist/leaflet.css";
import "./stopsSearchMenu.scss";
type ByStopIdMarkers = Record<number, LeafletMarker[] | undefined>;
interface SearchStore {
getSearchText: () => string;
setSearchText: (text: string, businessDataStore: BusinessDataStore) => Promise<void>;
isSearchInProgress: () => boolean;
getFoundStops: () => Stop[];
setFoundStops: (stops: Stop[]) => void;
getDisplayedPanelId: () => number;
setDisplayedPanelId: (panelId: number) => void;
addMarkers: (stopId: number, markers: LeafletMarker[]) => void;
getPanels: () => PositionedPanel[];
setPanels: (panels: PositionedPanel[]) => void;
};
const SearchContext = createContext<SearchStore>();
function SearchProvider(props: { children: JSX.Element }) {
type Store = {
searchText: string;
searchInProgress: boolean;
foundStops: Stop[];
markers: ByStopIdMarkers;
displayedPanelId: number;
panels: PositionedPanel[];
};
const [store, setStore] = createStore<Store>({
searchText: "", searchInProgress: false, foundStops: [], markers: {}, displayedPanelId: 0, panels: []
});
const getSearchText = (): string => {
return store.searchText;
}
const setSearchText = async (text: string, businessDataStore: BusinessDataStore): Promise<void> => {
setStore('searchInProgress', true);
setStore('searchText', text);
const { searchStopByName } = businessDataStore;
console.log("store.searchText=", store.searchText);
const stopsById = await searchStopByName(store.searchText);
console.log("stopsById=", stopsById);
setFoundStops(Object.values(stopsById));
setStore('searchInProgress', false);
}
const isSearchInProgress = (): boolean => {
return store.searchInProgress;
}
const getFoundStops = (): Stop[] => {
return store.foundStops;
}
const setFoundStops = (stops: Stop[]): void => {
setStore('foundStops', stops);
}
const getDisplayedPanelId = (): number => {
return store.displayedPanelId;
}
const setDisplayedPanelId = (panelId: number): void => {
setStore('displayedPanelId', panelId);
}
const addMarkers = (stopId: number, markers: L.Marker[]): void => {
setStore('markers', stopId, markers);
}
const getPanels = (): PositionedPanel[] => {
return store.panels;
}
const setPanels = (panels: PositionedPanel[]): void => {
setStore('panels', panels);
}
return (
<SearchContext.Provider value={{
getSearchText, setSearchText, isSearchInProgress,
getFoundStops, setFoundStops,
getDisplayedPanelId, setDisplayedPanelId,
addMarkers,
getPanels, setPanels
}}>
{props.children}
</SearchContext.Provider>
);
}
const Header: VoidComponent<{ title: string, minCharsNb: number }> = (props) => {
const businessDataStore: BusinessDataStore | undefined = useContext(BusinessDataContext);
const searchStore: SearchStore | undefined = useContext(SearchContext);
if (businessDataStore === undefined || searchStore === undefined)
return <div />;
const { isSearchInProgress, setSearchText } = searchStore;
const onStopNameInput: JSX.EventHandler<HTMLInputElement, InputEvent> = async (event): Promise<void> => {
/* TODO: Add a tempo before fetching stop for giving time to user to finish his request */
const stopName = event.currentTarget.value;
if (stopName.length >= props.minCharsNb) {
console.log(`Fetching data for "${stopName}" stop name`);
await setSearchText(stopName, businessDataStore);
}
}
return (
<div class="header">
<div class="title">
<svg viewBox="0 0 1260 50">
<text x="0" y="50%" dominant-baseline="middle" font-size="50" style="fill: #ffffff">
{props.title}
</text>
</svg>
</div>
<div class="inputGroup">
<InputGroup >
<InputLeftAddon>🚉 🚏</InputLeftAddon>
<Input onInput={onStopNameInput} readOnly={isSearchInProgress()} placeholder="Stop name..." />
</InputGroup>
</div>
</div >
);
};
const StopRepr: VoidComponent<{ stop: Stop }> = (props) => {
@@ -40,27 +180,31 @@ const StopRepr: VoidComponent<{ stop: Stop }> = (props) => {
const [lineReprs] = createResource<JSX.Element[], string[]>(props.stop.lines, fetchLinesRepr);
return (
<HStack height="100%">
{props.stop.name}
<div class="stop">
<div class="name">{props.stop.name}</div>
<For each={lineReprs()}>{(line: JSX.Element) => line}</For>
</HStack>
</div>
);
}
type ByTransportModeReprs = {
mode: JSX.Element | undefined;
lines: Record<string, JSX.Element | JSX.Element[] | undefined>;
};
const StopAreaRepr: VoidComponent<{ stop: Stop }> = (props) => {
const businessDataStore: BusinessDataStore | undefined = useContext(BusinessDataContext);
if (businessDataStore === undefined)
const appContextStore: AppContextStore | undefined = useContext(AppContextContext);
if (businessDataStore === undefined || appContextStore === undefined)
return <div />;
const { getLine } = businessDataStore;
const { setDisplayedStops } = appContextStore;
type ByTransportModeReprs = {
mode: JSX.Element | undefined;
[key: string]: JSX.Element | JSX.Element[] | undefined;
};
const fetchLinesRepr = async (stop: Stop): Promise<JSX.Element[]> => {
const fetchLinesRepr = async (stop: Stop): Promise<JSX.Element> => {
const lineIds = new Set(stop.lines);
const stops = stop.stops;
for (const stop of stops) {
@@ -74,38 +218,122 @@ const StopAreaRepr: VoidComponent<{ stop: Stop }> = (props) => {
if (!(line.transportMode in byModeReprs)) {
byModeReprs[line.transportMode] = {
mode: <div class="transportMode">{renderLineTransportMode(line)}</div>,
lines: {}
};
}
byModeReprs[line.transportMode][line.shortName] = renderLinePicto(line, styles);
byModeReprs[line.transportMode].lines[line.shortName] = renderLinePicto(line);
}
}
const reprs = [];
const sortedTransportModes = Object.keys(byModeReprs).sort((x, y) => TransportModeWeights[x] < TransportModeWeights[y] ? 1 : -1);
for (const transportMode of sortedTransportModes) {
const lines = byModeReprs[transportMode];
const repr = [lines.mode];
delete lines.mode;
for (const lineId of Object.keys(lines).sort((x, y) => x.localeCompare(y))) {
repr.push(lines[lineId]);
}
reprs.push(repr);
}
return reprs;
}
const sortedTransportModes = Object.keys(byModeReprs).sort((x, y) => TransportModeWeights[x] <
TransportModeWeights[y] ? 1 : -1);
return (
<div class="lineRepr">
<For each={sortedTransportModes}>{(transportMode) => {
const reprs = byModeReprs[transportMode];
const lineNames = Object.keys(reprs.lines).sort((x, y) => x.localeCompare(y));
return <>
{reprs.mode}
<div class="linesRepresentationMatrix">
<For each={lineNames}>{(lineName) => reprs.lines[lineName]}</For>
</div>
</>
}}
</For>
</div >
);
}
const [lineReprs] = createResource(props.stop, fetchLinesRepr);
return (
<HStack height="100%">
{props.stop.name}
<For each={lineReprs()}>{(line) => line}</For>
</HStack>
<div class="stop" onClick={() => setDisplayedStops([props.stop])}>
<div class="name">{props.stop.name}</div>
{lineReprs()}
</div>
);
}
const StopsPanel: ParentComponent<{ stops: Stop[], show: boolean }> = (props) => {
return (
<div classList={{ ["stopPanel"]: true, ["displayed"]: props.show }}>
<For each={props.stops.sort((x, y) => x.name.localeCompare(y.name))}>
{(stop) => {
return <Show when={stop.stops !== undefined} fallback={<StopRepr stop={stop} />}>
<StopAreaRepr stop={stop} />
</Show>;
}}
</For>
</div>
);
}
const Map: VoidComponent<{ stops: Stop[] }> = (props) => {
const StopsPanels: ParentComponent<{ maxStopsPerPanel: number }> = (props) => {
const searchStore: SearchStore | undefined = useContext(SearchContext);
if (searchStore === undefined) {
return <div />;
}
const { getDisplayedPanelId, getFoundStops, getPanels, setDisplayedPanelId, setPanels } = searchStore;
let stopsPanelsRef: HTMLDivElement | undefined = undefined
const stopsPanelsScroll = createScrollPosition(() => stopsPanelsRef);
const yStopsPanelsScroll = () => stopsPanelsScroll.y;
createEffect(() => {
yStopsPanelsScroll();
for (const panel of Object.values(getPanels())) {
if (panel.panel) {
const panelDiv = panel.panel();
const panelDivClientRect = panelDiv.getBoundingClientRect();
if (panelDivClientRect.y > 0) {
setDisplayedPanelId(panel.position);
break;
}
}
}
});
return (
<div ref={stopsPanelsRef} class="stopsPanels">
{() => {
setPanels([]);
let newPanels = [];
let positioneds: PositionedPanel[] = [];
let stops: Stop[] = [];
for (const stop of getFoundStops()) {
if (stops.length < props.maxStopsPerPanel) {
stops.push(stop);
}
else {
const panelId = newPanels.length;
const panel = <StopsPanel stops={stops} show={panelId == getDisplayedPanelId()} />;
newPanels.push(panel);
positioneds.push({ position: panelId, panel: panel });
stops = [stop];
}
}
if (stops.length) {
const panelId = newPanels.length;
const panel = <StopsPanel stops={stops} show={panelId == getDisplayedPanelId()} />;
newPanels.push(panel);
positioneds.push({ position: panelId, panel: panel });
}
setPanels(positioneds);
return newPanels;
}}
</div>
);
}
const Map: VoidComponent<{}> = () => {
const mapCenter: LeafletLatLngLiteral = { lat: 48.853, lng: 2.35 };
@@ -113,7 +341,7 @@ const Map: VoidComponent<{ stops: Stop[] }> = (props) => {
if (searchStore === undefined)
return <div />;
const { addMarkers } = searchStore;
const { addMarkers, getFoundStops } = searchStore;
let mapDiv: any;
@@ -148,7 +376,7 @@ const Map: VoidComponent<{ stops: Stop[] }> = (props) => {
/* TODO: Avoid to clear all layers... */
stopsLayerGroup.clearLayers();
for (const stop of props.stops) {
for (const stop of getFoundStops()) {
const markers = setMarker(stop);
addMarkers(stop.id, markers);
for (const marker of markers) {
@@ -162,67 +390,53 @@ const Map: VoidComponent<{ stops: Stop[] }> = (props) => {
}
});
return <div ref={mapDiv} id='main-map' style={{ width: "100%", height: "100%" }} />;
return <div ref={mapDiv} class="map" id="main-map" />;
}
export const StopsManager: VoidComponent = () => {
const Footer: VoidComponent<{}> = () => {
const businessDataStore: BusinessDataStore | undefined = useContext(BusinessDataContext);
const searchStore: SearchStore | undefined = useContext(SearchContext);
if (businessDataStore === undefined || searchStore === undefined)
if (searchStore === undefined) {
return <div />;
const { searchStopByName } = businessDataStore;
const { setDisplayedStops } = searchStore;
// TODO: Use props instead
const [minCharactersNb] = createSignal<number>(4);
const [inProgress, setInProgress] = createSignal<boolean>(false);
const [foundStops, setFoundStops] = createSignal<Stop[]>([]);
const onStopNameInput: JSX.EventHandler<HTMLInputElement, InputEvent> = async (event): Promise<void> => {
/* TODO: Add a tempo before fetching stop for giving time to user to finish his request */
const stopName = event.currentTarget.value;
if (stopName.length >= minCharactersNb()) {
console.log(`Fetching data for ${stopName}`);
setInProgress(true);
const stopsById = await searchStopByName(stopName);
setFoundStops(Object.values(stopsById));
setInProgress(false);
}
}
const { getDisplayedPanelId, getPanels } = searchStore;
return (
<VStack h="100%">
<InputGroup w="50%" h="5%">
<InputLeftAddon>🚉 🚏</InputLeftAddon>
<Input onInput={onStopNameInput} readOnly={inProgress()} placeholder="Stop name..." />
</InputGroup>
<Progress size="xs" w="50%" indeterminate={inProgress()}>
<ProgressIndicator striped animated />
</Progress>
<Box w="100%" h="40%" borderWidth="1px" borderColor="var(--idfm-black)" borderRadius="$lg" overflow="scroll" marginBottom="2px">
<List width="100%" height="100%">
<For each={foundStops().sort((x, y) => x.name.localeCompare(y.name))}>
{(stop) =>
<ListItem borderWidth="1px" mb="0px" color="var(--idfm-black)" borderRadius="$lg">
<Button fullWidth="true" color="var(--idfm-black)" bg="white" onClick={() => setDisplayedStops([stop])}>
<Box w="100%" h="100%">
<Show when={stop.stops !== undefined} fallback={<StopRepr stop={stop} />}>
<StopAreaRepr stop={stop} />
</Show>
</Box>
</Button>
</ListItem>
}
<div class="footer">
<For each={getPanels()}>
{(panel) => {
const position = panel.position;
return (
<div>
<svg viewBox="0 0 29 29">
<circle cx="50%" cy="50%" r="13" stroke="#ffffff" stroke-width="3"
style={{ fill: `var(--idfm-${position == getDisplayedPanelId() ? "white" : "black"})` }}
/>
</svg>
</div>
);
}}
</For>
</List>
</Box>
<Box borderWidth="1px" borderColor="var(--idfm-black)" borderRadius="$lg" h="55%" w="100%" overflow="scroll">
<Map stops={foundStops()} />
</Box>
</VStack>
</div>
);
}
export const StopsSearchMenu: VoidComponent = () => {
const MAX_STOPS_PER_PANEL = 5;
return (
<div class="stopSearchMenu">
<SearchProvider>
<Header title="Recherche de l'arrêt..." minCharsNb={4} />
<div class="body">
<StopsPanels maxStopsPerPanel={MAX_STOPS_PER_PANEL} />
<Map />
</div>
<Footer />
</SearchProvider>
</div>
);
};

View File

@@ -2,6 +2,13 @@ import { JSX } from 'solid-js';
import { Line } from './types';
// Thanks to https://dev.to/ycmjason/how-to-create-range-in-javascript-539i
export function* range(start: number, end: number): Generator<number> {
for (let i = start; i <= end; i++) {
yield i;
}
}
const validTransportModes = ["bus", "tram", "metro", "rer", "transilien", "funicular", "ter", "unknown"];
export const TransportModeWeights: Record<string, number> = {
@@ -116,3 +123,9 @@ export function renderLinePicto(line: Line): JSX.Element {
return renderTrainLinePicto(line);
}
}
export type PositionedPanel = {
position: number;
// TODO: Should be PassagesPanelComponent ?
panel: JSX.Element;
};