|
| 1 | +import { Flex, Text } from "@radix-ui/themes"; |
| 2 | +import intersperse from "intersperse"; |
| 3 | +import { DateTime } from "luxon"; |
| 4 | +import opening_hours from "opening_hours"; |
| 5 | +import * as React from "react"; |
| 6 | +import { t } from "ttag"; |
| 7 | +import FeatureContext from "~/components/CombinedFeaturePanel/components/FeatureContext"; |
| 8 | +import { useAdminAreas } from "~/lib/fetchers/osm-api/fetchAdminAreas"; |
| 9 | +import { |
| 10 | + type TypeTaggedOSMFeature, |
| 11 | + isOSMFeature, |
| 12 | +} from "~/lib/model/geo/AnyFeature"; |
| 13 | +import { log } from "~/lib/util/logger"; |
| 14 | + |
| 15 | +// helper function |
| 16 | +function getReadableState(oh: opening_hours) { |
| 17 | + const outputs: string[] = []; |
| 18 | + const comment = oh.getComment(); |
| 19 | + if (oh.getUnknown()) { |
| 20 | + const maybeOpen = t`Maybe open`; |
| 21 | + const maybeOpenBut = t`Maybe open – ${comment}`; |
| 22 | + outputs.push(comment ? maybeOpenBut : maybeOpen); |
| 23 | + } else { |
| 24 | + const state = oh.getState(); |
| 25 | + outputs.push(state ? t`Now open.` : t`Now closed.`); |
| 26 | + if (comment) { |
| 27 | + outputs.push(t`(“${comment}”)`); |
| 28 | + } |
| 29 | + } |
| 30 | + return outputs; |
| 31 | +} |
| 32 | + |
| 33 | +export default function OpeningHoursValueListItem(props: { |
| 34 | + value: string; |
| 35 | + tagKey: string; |
| 36 | + osmFeature?: TypeTaggedOSMFeature; |
| 37 | +}) { |
| 38 | + // https://openingh.ypid.de/evaluation_tool/?lng=en |
| 39 | + // https://github.com/opening-hours/opening_hours.js |
| 40 | + const { value, osmFeature, tagKey } = props; |
| 41 | + const feature = React.useContext(FeatureContext); |
| 42 | + |
| 43 | + let lat: number | undefined; |
| 44 | + let lon: number | undefined; |
| 45 | + |
| 46 | + if (isOSMFeature(feature)) { |
| 47 | + [lon, lat] = feature.geometry.coordinates; |
| 48 | + } |
| 49 | + const adminAreas = useAdminAreas({ longitude: lon, latitude: lat }); |
| 50 | + const { featuresByType } = adminAreas; |
| 51 | + const country = [ |
| 52 | + feature?.properties?.["addr:country"], |
| 53 | + featuresByType?.country?.properties?.["ISO3166-1:alpha2"], |
| 54 | + ].find((c) => typeof c === "string"); |
| 55 | + const state = [ |
| 56 | + feature?.properties?.["addr:state"], |
| 57 | + (featuresByType?.state || featuresByType?.city)?.properties?.state_code, |
| 58 | + ].find((c) => typeof c === "string"); |
| 59 | + |
| 60 | + const { outputs, oh, niceString } = React.useMemo(() => { |
| 61 | + try { |
| 62 | + const oh = new opening_hours( |
| 63 | + value, |
| 64 | + lat && lon && country && state |
| 65 | + ? { |
| 66 | + lat, |
| 67 | + lon, |
| 68 | + address: { country_code: country, state }, |
| 69 | + } |
| 70 | + : null, |
| 71 | + { |
| 72 | + locale: navigator.language.slice(0, 2), |
| 73 | + tag_key: tagKey, |
| 74 | + map_value: true, |
| 75 | + mode: 2, |
| 76 | + warnings_severity: 6, |
| 77 | + }, |
| 78 | + ); |
| 79 | + const isOpen = oh.getState(); // for current date |
| 80 | + const nextChangeDate = oh.getNextChange(); |
| 81 | + const outputs = getReadableState(oh); |
| 82 | + |
| 83 | + if (typeof nextChangeDate === "undefined") |
| 84 | + outputs.push(t`(indefinitely)`); |
| 85 | + else { |
| 86 | + const isUnknown = oh.getUnknown(nextChangeDate); |
| 87 | + const nextChangeDateTime = DateTime.fromJSDate(nextChangeDate); |
| 88 | + const nextChangeDateFormatted = nextChangeDateTime.toRelative({ |
| 89 | + base: DateTime.now(), |
| 90 | + }); |
| 91 | + |
| 92 | + if (!isUnknown && !isOpen) { |
| 93 | + outputs.push(t`Will open ${nextChangeDateFormatted}.`); |
| 94 | + } else if (!isUnknown && isOpen) { |
| 95 | + outputs.push(t`Will close ${nextChangeDateFormatted}.`); |
| 96 | + } else if (isUnknown && !isOpen) { |
| 97 | + outputs.push(t`Might open ${nextChangeDateFormatted}.`); |
| 98 | + } else if (isUnknown && isOpen) { |
| 99 | + outputs.push(t`Might close ${nextChangeDateFormatted}.`); |
| 100 | + } |
| 101 | + } |
| 102 | + return { outputs, oh, niceString }; |
| 103 | + } catch (e) { |
| 104 | + log.error(e); |
| 105 | + return { outputs: [] }; |
| 106 | + } |
| 107 | + }, [lat, lon, country, state, value, tagKey]); |
| 108 | + |
| 109 | + const niceLines = oh?.prettifyValue(); |
| 110 | + const shownValue = ((niceString as string) || "") |
| 111 | + .replace(/\bMo\b/g, t`Monday`) |
| 112 | + .replace(/\bTu\b/g, t`Tuesday`) |
| 113 | + .replace(/\bWe\b/g, t`Wednesday`) |
| 114 | + .replace(/\bTh\b/g, t`Thursday`) |
| 115 | + .replace(/\bFr\b/g, t`Friday`) |
| 116 | + .replace(/\bSa\b/g, t`Saturday`) |
| 117 | + .replace(/\bSu\b/g, t`Sunday`) |
| 118 | + |
| 119 | + .replace(/\bJan\b/g, t`January`) |
| 120 | + .replace(/\bFeb\b/g, t`February`) |
| 121 | + .replace(/\bMar\b/g, t`March`) |
| 122 | + .replace(/\bApr\b/g, t`April`) |
| 123 | + .replace(/\bMay\b/g, t`May`) |
| 124 | + .replace(/\bJun\b/g, t`June`) |
| 125 | + .replace(/\bJul\b/g, t`July`) |
| 126 | + .replace(/\bAug\b/g, t`August`) |
| 127 | + .replace(/\bSep\b/g, t`September`) |
| 128 | + .replace(/\bOct\b/g, t`October`) |
| 129 | + .replace(/\bNov\b/g, t`November`) |
| 130 | + .replace(/\bDec\b/g, t`December`) |
| 131 | + |
| 132 | + .replace(/\bPH\b/g, t`public holiday`) |
| 133 | + .replace(/\boff\b/g, t`closed`) |
| 134 | + .replace(/\bSH\b/g, t`school holiday`) |
| 135 | + .replace(/,/g, ", "); |
| 136 | + |
| 137 | + const shownElements = intersperse(shownValue.split(/;|\|\|/), <br />); |
| 138 | + // console.log("outputs: ", outputs); |
| 139 | + |
| 140 | + // |
| 141 | + // if (!outputs.length) { |
| 142 | + // return <>{shownElements}</>; |
| 143 | + // } |
| 144 | + |
| 145 | + const next = oh && DateTime.fromJSDate(oh.getNextChange()); |
| 146 | + const isOpen = oh?.getState(); |
| 147 | + const nextChangeIsToday = next && DateTime.local().hasSame(next, "day"); |
| 148 | + |
| 149 | + return ( |
| 150 | + <> |
| 151 | + {/*<strong> |
| 152 | + <StyledMarkdown inline element="span"> |
| 153 | + {outputs[0]} |
| 154 | + </StyledMarkdown> |
| 155 | + </strong> |
| 156 | + {outputs.length > 1 && ( |
| 157 | + <> |
| 158 | + |
| 159 | + <StyledMarkdown inline element="span"> |
| 160 | + {outputs.slice(1).join(" ")} |
| 161 | + </StyledMarkdown> |
| 162 | + </> |
| 163 | + )} |
| 164 | + |
| 165 | + {osmFeature?.properties["opening_hours:url"] && ( |
| 166 | + <a href={String(osmFeature.properties["opening_hours:url"])}> |
| 167 | + {t`See website`}. |
| 168 | + </a> |
| 169 | + )} |
| 170 | + const isOpen = oh.getState(); // for current date |
| 171 | + const nextChangeDate = oh.getNextChange(); |
| 172 | + <div style={{ marginTop: "0.5rem", opacity: 0.8 }}>{shownElements}</div>*/} |
| 173 | + {isOpen && next && ( |
| 174 | + <Flex gap="1" asChild> |
| 175 | + <Text size="2"> |
| 176 | + <Text color="green"> |
| 177 | + <span>Wahrscheinlich offen</span> |
| 178 | + </Text> |
| 179 | + <span>bis {next.toFormat("HH:mm")}</span> |
| 180 | + </Text> |
| 181 | + </Flex> |
| 182 | + )} |
| 183 | + {!isOpen && next && ( |
| 184 | + <Flex gap="1" asChild> |
| 185 | + <Text size="2"> |
| 186 | + <Text color="red"> |
| 187 | + <span>Wahrscheinlich geschlossen</span> |
| 188 | + </Text> |
| 189 | + {nextChangeIsToday ? ( |
| 190 | + <span> öffnet um {next.toFormat("HH:mm")}</span> |
| 191 | + ) : ( |
| 192 | + <span> öffnet {next.toFormat("ccc HH:mm")}</span> |
| 193 | + )} |
| 194 | + </Text> |
| 195 | + </Flex> |
| 196 | + )} |
| 197 | + </> |
| 198 | + ); |
| 199 | +} |
0 commit comments