2022-12-31 23:31:41 +01:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
|
import { AppType } from '../../types/app';
|
|
|
|
|
import { AreaType } from '../../types/area';
|
|
|
|
|
import { CategoryType } from '../../types/category';
|
2022-12-22 11:29:51 +09:00
|
|
|
import { ConfigType } from '../../types/config';
|
2022-12-31 23:31:41 +01:00
|
|
|
import { Config, serviceItem } from '../types';
|
2022-12-22 11:29:51 +09:00
|
|
|
|
2022-12-31 23:31:41 +01:00
|
|
|
export function migrateConfig(config: Config): ConfigType {
|
2022-12-22 11:29:51 +09:00
|
|
|
const newConfig: ConfigType = {
|
2022-12-24 17:18:16 +09:00
|
|
|
schemaVersion: 1,
|
2022-12-22 11:29:51 +09:00
|
|
|
configProperties: {
|
|
|
|
|
name: config.name ?? 'default',
|
|
|
|
|
},
|
|
|
|
|
categories: [],
|
|
|
|
|
widgets: [],
|
|
|
|
|
apps: [],
|
|
|
|
|
settings: {
|
|
|
|
|
common: {
|
|
|
|
|
searchEngine: {
|
|
|
|
|
type: 'google',
|
|
|
|
|
properties: {
|
|
|
|
|
enabled: true,
|
|
|
|
|
openInNewTab: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
defaultConfig: 'default',
|
|
|
|
|
},
|
|
|
|
|
customization: {
|
|
|
|
|
colors: {},
|
|
|
|
|
layout: {
|
|
|
|
|
enabledDocker: false,
|
|
|
|
|
enabledLeftSidebar: false,
|
|
|
|
|
enabledPing: false,
|
|
|
|
|
enabledRightSidebar: false,
|
|
|
|
|
enabledSearchbar: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
wrappers: [
|
|
|
|
|
{
|
|
|
|
|
id: 'default',
|
|
|
|
|
position: 1,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-31 23:31:41 +01:00
|
|
|
config.services.forEach((service, index) => {
|
|
|
|
|
const { category: categoryName } = service;
|
|
|
|
|
|
|
|
|
|
if (!categoryName) {
|
|
|
|
|
newConfig.apps.push(
|
|
|
|
|
migrateService(service, index, {
|
|
|
|
|
type: 'wrapper',
|
|
|
|
|
properties: {
|
|
|
|
|
id: 'default',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const category = getConfigAndCreateIfNotExsists(newConfig, categoryName);
|
|
|
|
|
|
|
|
|
|
if (!category) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newConfig.apps.push(
|
|
|
|
|
migrateService(service, index, { type: 'category', properties: { id: category.id } })
|
|
|
|
|
);
|
|
|
|
|
});
|
2022-12-22 11:29:51 +09:00
|
|
|
|
|
|
|
|
return newConfig;
|
|
|
|
|
}
|
2022-12-31 23:31:41 +01:00
|
|
|
|
|
|
|
|
const getConfigAndCreateIfNotExsists = (
|
|
|
|
|
config: ConfigType,
|
|
|
|
|
categoryName: string
|
|
|
|
|
): CategoryType | null => {
|
|
|
|
|
const foundCategory = config.categories.find((c) => c.name === categoryName);
|
|
|
|
|
if (foundCategory) {
|
|
|
|
|
return foundCategory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const category: CategoryType = {
|
|
|
|
|
id: uuidv4(),
|
|
|
|
|
name: categoryName,
|
|
|
|
|
position: 0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config.categories.push(category);
|
|
|
|
|
return category;
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-06 22:46:07 +01:00
|
|
|
const getShapeForColumnCount = (index: number, columnCount: number) => ({
|
|
|
|
|
location: {
|
|
|
|
|
x: index % columnCount,
|
|
|
|
|
y: Math.floor(index / columnCount),
|
|
|
|
|
},
|
|
|
|
|
size: {
|
|
|
|
|
width: 1,
|
|
|
|
|
height: 1,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-31 23:31:41 +01:00
|
|
|
const migrateService = (
|
|
|
|
|
oldService: serviceItem,
|
|
|
|
|
serviceIndex: number,
|
|
|
|
|
areaType: AreaType
|
|
|
|
|
): AppType => ({
|
|
|
|
|
id: uuidv4(),
|
|
|
|
|
name: oldService.name,
|
|
|
|
|
url: oldService.url,
|
|
|
|
|
behaviour: {
|
|
|
|
|
isOpeningNewTab: oldService.newTab ?? true,
|
|
|
|
|
externalUrl: oldService.openedUrl ?? '',
|
|
|
|
|
},
|
|
|
|
|
network: {
|
|
|
|
|
enabledStatusChecker: oldService.ping ?? true,
|
|
|
|
|
okStatus: oldService.status?.map((str) => parseInt(str, 10)) ?? [200],
|
|
|
|
|
},
|
|
|
|
|
appearance: {
|
2023-01-07 19:14:40 +01:00
|
|
|
iconUrl: migrateIcon(oldService.icon),
|
2022-12-31 23:31:41 +01:00
|
|
|
},
|
|
|
|
|
integration: {
|
|
|
|
|
type: null,
|
|
|
|
|
properties: [],
|
|
|
|
|
},
|
|
|
|
|
area: areaType,
|
|
|
|
|
shape: {
|
2023-01-06 22:46:07 +01:00
|
|
|
lg: getShapeForColumnCount(serviceIndex, 12),
|
|
|
|
|
md: getShapeForColumnCount(serviceIndex, 6),
|
|
|
|
|
sm: getShapeForColumnCount(serviceIndex, 3),
|
2022-12-31 23:31:41 +01:00
|
|
|
},
|
|
|
|
|
});
|
2023-01-07 19:14:40 +01:00
|
|
|
|
|
|
|
|
const migrateIcon = (iconUrl: string) => {
|
|
|
|
|
if (iconUrl.startsWith('https://cdn.jsdelivr.net/gh/walkxhub/dashboard-icons/png/')) {
|
|
|
|
|
console.log('migrating icon:');
|
|
|
|
|
const icon = iconUrl.split('/').at(-1);
|
|
|
|
|
console.log(`${iconUrl} -> ${icon}`);
|
|
|
|
|
return `https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/png/${icon}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return iconUrl;
|
|
|
|
|
};
|