mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-09 23:15:46 +01:00
🐛 Fix issue with old migration and add migration for location
This commit is contained in:
@@ -316,7 +316,11 @@
|
||||
"type": "weather",
|
||||
"properties": {
|
||||
"displayInFahrenheit": false,
|
||||
"location": "Paris"
|
||||
"location": {
|
||||
"name": "Paris",
|
||||
"latitude": 48.85341,
|
||||
"longitude": 2.3488
|
||||
}
|
||||
},
|
||||
"area": {
|
||||
"type": "category",
|
||||
|
||||
@@ -38,7 +38,7 @@ export async function getServerSideProps({
|
||||
};
|
||||
}
|
||||
|
||||
const config = getFrontendConfig(configName as string);
|
||||
const config = await getFrontendConfig(configName as string);
|
||||
setCookie('config-name', configName, {
|
||||
req,
|
||||
res,
|
||||
|
||||
@@ -47,7 +47,7 @@ export async function getServerSideProps({
|
||||
}
|
||||
|
||||
const translations = await getServerSideTranslations(dashboardNamespaces, locale, req, res);
|
||||
const config = getFrontendConfig(configName as string);
|
||||
const config = await getFrontendConfig(configName as string);
|
||||
|
||||
return {
|
||||
props: {
|
||||
|
||||
@@ -34,10 +34,7 @@ export const weatherRouter = createTRPCRouter({
|
||||
results: z.array(citySchema),
|
||||
})
|
||||
)
|
||||
.query(async ({ input }) => {
|
||||
const res = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${input.query}`);
|
||||
return res.json();
|
||||
}),
|
||||
.query(async ({ input }) => fetchCity(input.query)),
|
||||
at: publicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
@@ -56,3 +53,12 @@ export const weatherRouter = createTRPCRouter({
|
||||
|
||||
export type City = z.infer<typeof citySchema>;
|
||||
export type Weather = z.infer<typeof weatherSchema>;
|
||||
|
||||
const outputSchema = z.object({
|
||||
results: z.array(citySchema),
|
||||
});
|
||||
|
||||
export const fetchCity = async (query: string) => {
|
||||
const res = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${query}`);
|
||||
return outputSchema.parse(await res.json());
|
||||
};
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
import Consola from 'consola';
|
||||
|
||||
import { ConfigType } from '../../types/config';
|
||||
import fs from 'fs';
|
||||
import { BackendConfigType, ConfigType } from '../../types/config';
|
||||
import { getConfig } from './getConfig';
|
||||
import { fetchCity } from '~/server/api/routers/weather';
|
||||
|
||||
export const getFrontendConfig = (name: string): ConfigType => {
|
||||
const config = getConfig(name);
|
||||
export const getFrontendConfig = async (name: string): Promise<ConfigType> => {
|
||||
let config = getConfig(name);
|
||||
|
||||
const anyWeatherWidgetWithStringLocation = config.widgets.some(
|
||||
(widget) => widget.type === 'weather' && typeof widget.properties.location === 'string'
|
||||
);
|
||||
|
||||
if (anyWeatherWidgetWithStringLocation) {
|
||||
config = await migrateLocation(config);
|
||||
}
|
||||
|
||||
Consola.info(`Requested frontend content of configuration '${name}'`);
|
||||
// If not, return the config
|
||||
@@ -41,3 +50,39 @@ export const getFrontendConfig = (name: string): ConfigType => {
|
||||
})),
|
||||
};
|
||||
};
|
||||
|
||||
const migrateLocation = async (config: BackendConfigType) => {
|
||||
Consola.log('Migrating config file to new location schema...', config.configProperties.name);
|
||||
|
||||
const configName = config.configProperties.name;
|
||||
const migratedConfig = {
|
||||
...config,
|
||||
widgets: await Promise.all(
|
||||
config.widgets.map(async (widget) =>
|
||||
widget.type !== 'weather' || typeof widget.properties.location !== 'string'
|
||||
? widget
|
||||
: {
|
||||
...widget,
|
||||
properties: {
|
||||
...widget.properties,
|
||||
location: await fetchCity(widget.properties.location)
|
||||
.then(({ results }) => ({
|
||||
name: results[0].name,
|
||||
latitude: results[0].latitude,
|
||||
longitude: results[0].longitude,
|
||||
}))
|
||||
.catch(() => ({
|
||||
name: '',
|
||||
latitude: 0,
|
||||
longitude: 0,
|
||||
})),
|
||||
},
|
||||
}
|
||||
)
|
||||
),
|
||||
};
|
||||
|
||||
fs.writeFileSync(`./data/configs/${configName}.json`, JSON.stringify(migratedConfig, null, 2));
|
||||
|
||||
return migratedConfig;
|
||||
};
|
||||
|
||||
@@ -2,13 +2,11 @@ import Consola from 'consola';
|
||||
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
import { Config, serviceItem } from '../types';
|
||||
import { ConfigAppIntegrationType, ConfigAppType, IntegrationType } from '../../types/app';
|
||||
import { AreaType } from '../../types/area';
|
||||
import { CategoryType } from '../../types/category';
|
||||
import { BackendConfigType } from '../../types/config';
|
||||
import { SearchEngineCommonSettingsType } from '../../types/settings';
|
||||
import { IWidget } from '../../widgets/widgets';
|
||||
import { ICalendarWidget } from '../../widgets/calendar/CalendarTile';
|
||||
import { IDashDotTile } from '../../widgets/dashDot/DashDotTile';
|
||||
import { IDateWidget } from '../../widgets/date/DateTile';
|
||||
@@ -16,6 +14,8 @@ import { ITorrentNetworkTraffic } from '../../widgets/download-speed/TorrentNetw
|
||||
import { ITorrent } from '../../widgets/torrent/TorrentTile';
|
||||
import { IUsenetWidget } from '../../widgets/useNet/UseNetTile';
|
||||
import { IWeatherWidget } from '../../widgets/weather/WeatherTile';
|
||||
import { IWidget } from '../../widgets/widgets';
|
||||
import { Config, serviceItem } from '../types';
|
||||
|
||||
export function migrateConfig(config: Config): BackendConfigType {
|
||||
const newConfig: BackendConfigType = {
|
||||
@@ -208,7 +208,11 @@ const migrateModules = (config: Config): IWidget<string, any>[] => {
|
||||
type: 'weather',
|
||||
properties: {
|
||||
displayInFahrenheit: oldModule.options?.freedomunit?.value ?? false,
|
||||
location: oldModule.options?.location?.value ?? 'Paris',
|
||||
location: {
|
||||
name: oldModule.options?.location?.value ?? '',
|
||||
latitude: 0,
|
||||
longitude: 0,
|
||||
},
|
||||
},
|
||||
area: {
|
||||
type: 'wrapper',
|
||||
|
||||
Reference in New Issue
Block a user