Initial commit

This commit is contained in:
Evert Prants 2023-09-17 14:03:38 +03:00
commit fb8778128b
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
26 changed files with 2021 additions and 0 deletions

24
.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

5
.prettierrc Normal file
View File

@ -0,0 +1,5 @@
{
"semi": true,
"singleQuote": true,
"trailingComma": "all"
}

3
.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"recommendations": ["svelte.svelte-vscode"]
}

47
README.md Normal file
View File

@ -0,0 +1,47 @@
# Svelte + TS + Vite
This template should help get you started developing with Svelte and TypeScript in Vite.
## Recommended IDE Setup
[VS Code](https://code.visualstudio.com/) + [Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode).
## Need an official Svelte framework?
Check out [SvelteKit](https://github.com/sveltejs/kit#readme), which is also powered by Vite. Deploy anywhere with its serverless-first approach and adapt to various platforms, with out of the box support for TypeScript, SCSS, and Less, and easily-added support for mdsvex, GraphQL, PostCSS, Tailwind CSS, and more.
## Technical considerations
**Why use this over SvelteKit?**
- It brings its own routing solution which might not be preferable for some users.
- It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app.
This template contains as little as possible to get started with Vite + TypeScript + Svelte, while taking into account the developer experience with regards to HMR and intellisense. It demonstrates capabilities on par with the other `create-vite` templates and is a good starting point for beginners dipping their toes into a Vite + Svelte project.
Should you later need the extended capabilities and extensibility provided by SvelteKit, the template has been structured similarly to SvelteKit so that it is easy to migrate.
**Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?**
Setting `compilerOptions.types` shuts out all other types not explicitly listed in the configuration. Using triple-slash references keeps the default TypeScript setting of accepting type information from the entire workspace, while also adding `svelte` and `vite/client` type information.
**Why include `.vscode/extensions.json`?**
Other templates indirectly recommend extensions via the README, but this file allows VS Code to prompt the user to install the recommended extension upon opening the project.
**Why enable `allowJs` in the TS template?**
While `allowJs: false` would indeed prevent the use of `.js` files in the project, it does not prevent the use of JavaScript syntax in `.svelte` files. In addition, it would force `checkJs: false`, bringing the worst of both worlds: not being able to guarantee the entire codebase is TypeScript, and also having worse typechecking for the existing JavaScript. In addition, there are valid use cases in which a mixed codebase may be relevant.
**Why is HMR not preserving my local component state?**
HMR state preservation comes with a number of gotchas! It has been disabled by default in both `svelte-hmr` and `@sveltejs/vite-plugin-svelte` due to its often surprising behavior. You can read the details [here](https://github.com/rixo/svelte-hmr#svelte-hmr).
If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR.
```ts
// store.ts
// An extremely simple external store
import { writable } from 'svelte/store'
export default writable(0)
```

13
index.html Normal file
View File

@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Evert's Home Weather</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>

25
package.json Normal file
View File

@ -0,0 +1,25 @@
{
"name": "homeweather-fe",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-check --tsconfig ./tsconfig.json"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^2.4.2",
"@tsconfig/svelte": "^5.0.0",
"sass": "^1.67.0",
"svelte": "^4.0.5",
"svelte-check": "^3.4.6",
"tslib": "^2.6.0",
"typescript": "^5.0.2",
"vite": "^4.4.5"
},
"dependencies": {
"svelte-i18n": "^3.7.4"
}
}

1398
pnpm-lock.yaml Normal file

File diff suppressed because it is too large Load Diff

1
public/vite.svg Normal file
View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

62
src/App.svelte Normal file
View File

@ -0,0 +1,62 @@
<script lang="ts">
import { _, isLoading } from 'svelte-i18n';
import Gauge from './lib/Gauge.svelte';
import Refresh from './lib/Refresh.svelte';
import Wind from './lib/Wind.svelte';
import type { Weather } from './lib/weather.interface';
import { weatherInfo } from './lib/weather.store';
import LangSwitcher from './lib/LangSwitcher.svelte';
let weatherData: Partial<Weather> = {};
weatherInfo.subscribe((data) => {
weatherData = data;
});
</script>
<main>
{#if $isLoading}<span>Please wait...</span>{:else}
<h1>{$_('weather')}</h1>
<div class="wrapper">
<div class="section">
<Gauge label={$_('outTemp')} unit="°C"
>{weatherData.outdoorTemperature?.toFixed(1) || '...'}</Gauge
>
<Gauge label={$_('outHumid')} unit="%RH"
>{weatherData.outdoorHumidity?.toFixed(0) || '...'}</Gauge
>
</div>
<div class="section">
<Gauge label={$_('inTemp')} unit="°C"
>{weatherData.indoorTemperature?.toFixed(1) || '...'}</Gauge
>
<Gauge label={$_('inHumid')} unit="%RH"
>{weatherData.indoorHumidity?.toFixed(0) || '...'}</Gauge
>
</div>
<div class="section">
<Wind data={weatherData} />
</div>
<div class="section">
<Gauge label={$_('absPressure')} unit="hPa"
>{weatherData.absPressure?.toFixed(1) || '...'}</Gauge
>
</div>
<LangSwitcher />
<Refresh />
</div>
{/if}
</main>
<style>
.wrapper {
display: flex;
flex-direction: column;
gap: 1rem;
}
.section {
display: flex;
gap: 2rem;
justify-content: center;
}
</style>

78
src/app.css Normal file
View File

@ -0,0 +1,78 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;
color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
}
*, *::after, *::before {
box-sizing: border-box;
}
a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}
body {
margin: 0;
display: flex;
min-width: 320px;
min-height: 100vh;
}
h1 {
font-size: 3.2em;
line-height: 1.1;
}
#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}
button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}

14
src/assets/i18n/en.json Normal file
View File

@ -0,0 +1,14 @@
{
"weather": "Weather",
"loading": "Loading...",
"outTemp": "Outdoor Temperature",
"outHumid": "Outdoor Humidity",
"inTemp": "Indoor Temperature",
"inHumid": "Indoor Humidity",
"absPressure": "Absolute Pressure",
"wind": "Wind",
"windSpeed": "Speed",
"windGust": "Gust",
"rain": "Rain (24h)",
"lang": "Eesti keeles"
}

14
src/assets/i18n/et.json Normal file
View File

@ -0,0 +1,14 @@
{
"weather": "Ilm",
"loading": "Laadimine...",
"outTemp": "Temperatuur (väljas)",
"outHumid": "Õhuniiskus (väljas)",
"inTemp": "Temperatuur (sees)",
"inHumid": "Õhuniiskus (sees)",
"absPressure": "Õhurõhk",
"wind": "Tuul",
"windSpeed": "Kiirus",
"windGust": "Puhanguti",
"rain": "Vihm (24h)",
"lang": "in English"
}

16
src/i18n.ts Normal file
View File

@ -0,0 +1,16 @@
import { register, init, getLocaleFromNavigator } from 'svelte-i18n';
register('en', () => import('./assets/i18n/en.json'))
register('et', () => import('./assets/i18n/et.json'))
const getLocale = () => {
const fromNavigator = getLocaleFromNavigator();
const applied = ['et', 'en'].find((entry) => fromNavigator.startsWith(entry));
if (applied) return applied;
return 'en';
}
const res = init({
fallbackLocale: 'en',
initialLocale: getLocale(),
});

26
src/lib/Gauge.svelte Normal file
View File

@ -0,0 +1,26 @@
<script lang="ts">
export let label = '';
export let unit = '';
</script>
<div class="numeric">
<span class="numeric-label">{label}</span>
<div class="numeric-inner">
<span class="numeric-gauge"><slot /></span>
<span class="numeric-unit">{unit}</span>
</div>
</div>
<style lang="scss">
.numeric {
&-inner {
display: flex;
align-items: flex-start;
justify-content: center;
font-size: 1.2rem;
}
&-gauge {
font-size: 2rem;
}
}
</style>

View File

@ -0,0 +1,10 @@
<script lang="ts">
import { locale, _ } from 'svelte-i18n';
let currentLang = 'en';
locale.subscribe((lang) => (currentLang = lang));
const changeLanguage = () => locale.set(currentLang === 'en' ? 'et' : 'en');
</script>
<button on:click={changeLanguage}>{$_('lang')}</button>

32
src/lib/Refresh.svelte Normal file
View File

@ -0,0 +1,32 @@
<script lang="ts">
import { _ } from 'svelte-i18n';
import { onMount } from 'svelte';
import { weatherInfo } from './weather.store';
let loading = true;
function fetchData() {
loading = true;
fetch('https://home.lunasqu.ee/api/weather/')
.then((res) => res.json())
.then((data) => {
weatherInfo.set(data);
loading = false;
});
}
onMount(() => {
const timer = setInterval(() => fetchData(), 60000);
fetchData();
return () => clearInterval(timer);
});
</script>
<div class="loading">
{#if loading}<span>{$_('loading')}</span>{/if}
</div>
<style lang="scss">
.loading {
color: #ddd;
}
</style>

49
src/lib/Wind.svelte Normal file
View File

@ -0,0 +1,49 @@
<script lang="ts">
import { _ } from 'svelte-i18n';
import Gauge from './Gauge.svelte';
import WindDirection from './WindDirection.svelte';
import type { Weather } from './weather.interface';
export let data: Partial<Weather> = {};
let convertedSpeed: string;
let convertedGust: string;
let convertedRain: string;
$: {
convertedSpeed = ((data.windSpeed || 0) / 3.6).toFixed(1);
convertedGust = ((data.gustSpeed || 0) / 3.6).toFixed(1);
convertedRain = (data.totalRain || 0).toFixed(1);
}
</script>
<div class="wind">
<span class="wind-label">{$_('wind')}</span>
<WindDirection direction={data.windDirection} />
<div class="wind-blocks">
<div class="wind-block">
<Gauge label={$_('windSpeed')} unit="m/s">{convertedSpeed}</Gauge>
</div>
<div class="wind-block">
<Gauge label={$_('windGust')} unit="m/s">{convertedGust}</Gauge>
</div>
<div class="wind-block">
<Gauge label={$_('rain')} unit="mm">{convertedRain}</Gauge>
</div>
</div>
</div>
<style lang="scss">
.wind {
display: flex;
flex-direction: column;
align-items: center;
gap: 1rem;
&-blocks {
display: flex;
justify-content: center;
gap: 1.5rem;
}
}
</style>

View File

@ -0,0 +1,129 @@
<script lang="ts">
const originPoint = -90;
const WIND_DIRS = {
N: originPoint,
NNE: originPoint + 45 / 2,
NE: originPoint + 45,
ENE: originPoint + 45 + 45 / 2,
E: originPoint + 90,
ESE: originPoint + 90 + 45 / 2,
SE: originPoint + 90 + 45,
SSE: originPoint + 90 + 45 + 45 / 2,
S: originPoint + 180,
SSW: originPoint + 180 + 45 / 2,
SW: originPoint + 180 + 45,
WSW: originPoint + 180 + 45 + 45 / 2,
W: originPoint - 90,
WNW: originPoint - (90 - 45 / 2),
NW: originPoint - (90 - 45),
NNW: originPoint - (90 - 45 - 45 / 2),
};
function calcPos(dir: string) {
const pad = 16;
const rem = 192 - pad;
const offset = pad * 0.4;
const angle = WIND_DIRS[dir];
const x = Math.cos(angle * (Math.PI / 180)) * (rem / 2) + rem / 2 + offset;
const y = Math.sin(angle * (Math.PI / 180)) * (rem / 2) + rem / 2 + offset;
return `translate(${x}px, ${y}px)`;
}
export let direction: string = 'N';
</script>
<div class="wind-radial-wrapper">
<div class="wind-radial-cardinals">
{#each Object.keys(WIND_DIRS) as dir}
<span
class="wind-cardinal wind-cardinal--{dir.toLowerCase()}"
style={`--pos: ${calcPos(dir)}`}>{dir}</span
>
{/each}
</div>
<div class="wind-radial">
<div
class="wind-direction"
style={`--angle: ${WIND_DIRS[direction || 'N']}deg`}
/>
</div>
</div>
<style lang="scss">
.wind {
&-radial {
position: relative;
width: 8rem;
height: 8rem;
&-wrapper {
display: flex;
align-items: center;
justify-content: center;
width: 12rem;
height: 12rem;
}
&-cardinals {
position: absolute;
width: 12rem;
height: 12rem;
}
}
&-direction {
position: absolute;
width: 1px;
height: 1px;
top: 50%;
left: 50%;
transform: rotate(var(--angle));
&::before {
content: '';
display: block;
position: absolute;
width: 0;
height: 0;
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
border-right: 10px solid #fff;
left: -5px;
top: -10px;
@media (prefers-color-scheme: light) {
border-right: 10px solid #000;
}
}
&::after {
content: '';
display: block;
position: absolute;
background-color: #fff;
width: 4rem;
height: 4px;
top: -2px;
@media (prefers-color-scheme: light) {
background-color: #000;
}
}
}
&-cardinal {
position: absolute;
width: 8px;
height: 8px;
display: flex;
align-items: center;
justify-content: center;
transform: var(--pos);
transform-origin: center center;
user-select: none;
&:nth-child(2n) {
color: rgb(184, 184, 184);
}
}
}
</style>

View File

@ -0,0 +1,17 @@
export interface Weather {
date: string;
indoorHumidity: number;
outdoorHumidity: number;
indoorTemperature: number;
outdoorTemperature: number;
outdoorDewPoint: number;
windChillTemp: number;
windSpeed: number;
gustSpeed: number;
windDirection: string;
rainDiff: number;
totalRain: number;
absPressure: number;
id: number;
fresh: boolean;
}

4
src/lib/weather.store.ts Normal file
View File

@ -0,0 +1,4 @@
import { writable } from 'svelte/store';
import type { Weather } from './weather.interface';
export const weatherInfo = writable<Partial<Weather>>({});

9
src/main.ts Normal file
View File

@ -0,0 +1,9 @@
import './app.css'
import './i18n'
import App from './App.svelte'
const app = new App({
target: document.getElementById('app'),
})
export default app

2
src/vite-env.d.ts vendored Normal file
View File

@ -0,0 +1,2 @@
/// <reference types="svelte" />
/// <reference types="vite/client" />

7
svelte.config.js Normal file
View File

@ -0,0 +1,7 @@
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'
export default {
// Consult https://svelte.dev/docs#compile-time-svelte-preprocess
// for more information about preprocessors
preprocess: vitePreprocess(),
}

20
tsconfig.json Normal file
View File

@ -0,0 +1,20 @@
{
"extends": "@tsconfig/svelte/tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"resolveJsonModule": true,
/**
* Typecheck JS in `.svelte` and `.js` files by default.
* Disable checkJs if you'd like to use dynamic types in JS.
* Note that setting allowJs false does not prevent the use
* of JS in `.svelte` files.
*/
"allowJs": true,
"checkJs": true,
"isolatedModules": true
},
"include": ["src/**/*.d.ts", "src/**/*.ts", "src/**/*.js", "src/**/*.svelte"],
"references": [{ "path": "./tsconfig.node.json" }]
}

9
tsconfig.node.json Normal file
View File

@ -0,0 +1,9 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler"
},
"include": ["vite.config.ts"]
}

7
vite.config.ts Normal file
View File

@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte()],
})