Skip to content

Commit

Permalink
feat: add setting for maps provider
Browse files Browse the repository at this point in the history
  • Loading branch information
istudyatuni committed Oct 15, 2023
1 parent 4ab3c2c commit 373e646
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"dev": "vite",
"build": "vite build",
"preview": "vite build && vite preview",
"format": "prettier --write --plugin-search-dir=. './**/*.js' './**/*.svelte' './**/*.css' './**/*.json'"
"format": "prettier --write --plugin-search-dir=. './**/*.js' './**/*.svelte' './**/*.css' './**/*.json'",
"migrate-locales": "python scripts/migrate_locales.py"
},
"dependencies": {
"svelte-i18n": "^3.3.13",
Expand Down
3 changes: 3 additions & 0 deletions public/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
},
"language": {
"title": "Language"
},
"maps": {
"title": "Maps"
}
},
"weather": {
Expand Down
3 changes: 3 additions & 0 deletions public/locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
},
"language": {
"title": "Язык"
},
"maps": {
"title": "Карты"
}
},
"weather": {
Expand Down
15 changes: 8 additions & 7 deletions src/components/Main.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import SettingsIcon from '~icons/tabler/settings'
import { settings } from 'src/stores'
import { formatMapLink } from 'src/utils/maps'
</script>

<script>
Expand All @@ -14,19 +16,18 @@
// and vice versa
let settingsNotOpened = true
$: mapLinkParams = new URLSearchParams({
mlat: $settings.current_city.lat,
mlon: $settings.current_city.lon,
})
$: map_link = formatMapLink(
$settings.maps_provider,
$settings.current_city.lat,
$settings.current_city.lon
)
</script>

<div class="grid-item">
<div class="inline">
<div class="city">
<h4 class="city-name">{$settings.current_city.name}</h4>
<a
class="map"
href="http://www.openstreetmap.org/?{mapLinkParams.toString()}">
<a class="map" href={map_link}>
<MapIcon />
</a>
</div>
Expand Down
12 changes: 12 additions & 0 deletions src/components/Settings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import Select from 'src/components/inputs/Select.svelte'
import { settings } from 'src/stores'
import { maps_providers } from 'src/utils/maps'
</script>

<script>
Expand All @@ -19,6 +20,10 @@
value: e[0],
name: e[1],
}))
let mapProvicersSelectValues = Object.values(maps_providers).map((e) => ({
name: e.name,
value: e.key,
}))
</script>

<h3>{$_('settings.title')}</h3>
Expand All @@ -44,6 +49,13 @@
on:change={() => (reloadRequired = true)} />
</div>

<div>
<Select
title={$_('settings.maps.title')}
options={mapProvicersSelectValues}
bind:value={$settings.maps_provider} />
</div>

<style>
.reload-required {
color: var(--second-fg-color);
Expand Down
4 changes: 3 additions & 1 deletion src/stores/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { localStore } from 'svelte-storages'
import { migrateStore } from 'src/stores/migration'

const initial = {
version: 1,
version: 2,
theme: 'system',
locale: undefined,
current_city: {
Expand All @@ -13,6 +13,8 @@ const initial = {
},
// get client timezone
tz: Intl.DateTimeFormat().resolvedOptions().timeZone,
/** @type {import('src/utils/types').OsmProvider} */
maps_provider: 'osm',
}

/** Settings store */
Expand Down
34 changes: 34 additions & 0 deletions src/utils/maps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/** @type {[{
* key: import('src/utils/types').OsmProvider,
* name: String,
* base_url: String
* }]}
*/
export const maps_providers = [
{
key: 'osm',
name: 'OpenStreetMap',
base_url: 'http://www.openstreetmap.org/?',
},
{
key: 'google',
name: 'Google',
base_url: 'https://maps.google.com/?',
},
]

/**
* @param {import('src/utils/types').OsmProvider} provider
* @return {String}
*/
export function formatMapLink(provider, lat, lon) {
let params = new URLSearchParams()
if (provider === 'osm') {
params.append('mlat', lat)
params.append('mlon', lon)
} else if (provider === 'google') {
params.append('q', [lat, lon])
}
let base = maps_providers.find((p) => p.key === provider).base_url
return base + params.toString()
}
1 change: 1 addition & 0 deletions src/utils/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type OsmProvider = 'osm' | 'google'

0 comments on commit 373e646

Please sign in to comment.