generated from mantinedev/next-pages-template
-
-
Notifications
You must be signed in to change notification settings - Fork 253
/
board.ts
183 lines (161 loc) · 5.74 KB
/
board.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { TRPCError } from '@trpc/server';
import fs from 'fs';
import { z } from 'zod';
import Consola from 'consola';
import { getDefaultBoardAsync } from '~/server/db/queries/userSettings';
import { configExists } from '~/tools/config/configExists';
import { getConfig } from '~/tools/config/getConfig';
import { getFrontendConfig } from '~/tools/config/getFrontendConfig';
import { generateDefaultApp } from '~/tools/shared/app';
import { adminProcedure, createTRPCRouter, protectedProcedure } from '../trpc';
import { writeConfig } from '~/tools/config/writeConfig';
import { configNameSchema } from '~/validations/boards';
export const boardRouter = createTRPCRouter({
all: protectedProcedure.query(async ({ ctx }) => {
const files = fs.readdirSync('./data/configs').filter((file) => file.endsWith('.json'));
const defaultBoard = await getDefaultBoardAsync(ctx.session.user.id, 'default');
return await Promise.all(
files.map(async (file) => {
const name = file.replace('.json', '');
const config = await getFrontendConfig(name);
const countApps = config.apps.length;
return {
name: name,
allowGuests: config.settings.access.allowGuests,
countApps: countApps,
countWidgets: config.widgets.length,
countCategories: config.categories.length,
isDefaultForUser: name === defaultBoard,
};
}),
);
}),
addAppsForContainers: adminProcedure
.input(
z.object({
boardName: configNameSchema,
apps: z.array(
z.object({
name: z.string(),
icon: z.string().optional(),
port: z.number().optional(),
}),
),
}),
)
.mutation(async ({ input }) => {
if (!configExists(input.boardName)) {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Board not found',
});
}
const config = getConfig(input.boardName);
const lowestWrapper = config?.wrappers.sort((a, b) => a.position - b.position)[0];
const newConfig = {
...config,
apps: [
...config.apps,
...input.apps.map((container) => {
const defaultApp = generateDefaultApp(lowestWrapper.id);
const address = container.port
? `http://localhost:${container.port}`
: 'http://localhost';
return {
...defaultApp,
name: container.name,
url: address,
appearance: {
...defaultApp.appearance,
iconUrl: container.icon,
},
behaviour: {
...defaultApp.behaviour,
externalUrl: address,
},
};
}),
],
};
const targetPath = `data/configs/${input.boardName}.json`;
fs.writeFileSync(targetPath, JSON.stringify(newConfig, null, 2), 'utf8');
}),
renameBoard: protectedProcedure
.input(z.object({
oldName: z.string(),
newName: z.string().min(1),
}))
.mutation(async ({ input }) => {
if (input.oldName === 'default') {
Consola.error(`Attempted to rename default configuration. Aborted deletion.`);
throw new TRPCError({
code: 'CONFLICT',
message: 'Cannot rename default board',
});
}
if (!configExists(input.oldName)) {
Consola.error(`Specified configuration ${input.oldName} does not exist on file system`);
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Board not found',
});
}
if (configExists(input.newName)) {
Consola.error(`Target name of rename conflicts with existing board`);
throw new TRPCError({
code: 'CONFLICT',
message: 'Board conflicts with existing board',
});
}
const config = getConfig(input.oldName);
config.configProperties.name = input.newName;
writeConfig(config);
Consola.info(`Deleting ${input.oldName} from the file system`);
const targetPath = `data/configs/${input.oldName}.json`;
fs.unlinkSync(targetPath);
Consola.info(`Deleted ${input.oldName} from file system`);
}),
duplicateBoard: protectedProcedure
.input(z.object({
boardName: z.string(),
}))
.mutation(async ({ input }) => {
if (!configExists(input.boardName)) {
Consola.error(`Tried to duplicate ${input.boardName} but this configuration does not exist.`);
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Board not found',
});
}
const targetName = attemptGenerateDuplicateName(input.boardName, 10);
Consola.info(`Target duplication name ${targetName} does not exist`);
const config = getConfig(input.boardName);
config.configProperties.name = targetName;
writeConfig(config);
Consola.info(`Wrote config to name '${targetName}'`)
}),
});
const duplicationName = /^(\w+)\s{1}\(([0-9]+)\)$/;
const attemptGenerateDuplicateName = (baseName: string, maxAttempts: number) => {
for (let i = 0; i < maxAttempts; i++) {
const newName = generateDuplicateName(baseName, i);
if (configExists(newName)) {
continue;
}
return newName;
}
Consola.error(`Duplication name ${baseName} conflicts with an existing configuration`);
throw new TRPCError({
code: 'CONFLICT',
message: 'Board conflicts with an existing board',
});
}
const generateDuplicateName = (baseName: string, increment: number) => {
const result = duplicationName.exec(baseName);
if (result && result.length === 3) {
const originalName = result.at(1);
const counter = Number(result.at(2));
return `${originalName} (${counter + 1 + increment})`;
}
return `${baseName} (2)`;
}