Skip to content

Commit a82387e

Browse files
wa0x6eCopilot
andauthored
fix: remove usage of global spaces object (#1008)
* fix: use spaceMetadata instead of spaces * fix: remove the global spaces object * fix: improve function name * refactor: use guard * Update src/helpers/spaces.ts Co-authored-by: Copilot <[email protected]> * Update src/helpers/spaces.ts --------- Co-authored-by: Copilot <[email protected]>
1 parent f50f3d2 commit a82387e

File tree

5 files changed

+56
-47
lines changed

5 files changed

+56
-47
lines changed

src/graphql/operations/plugins.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { spaces } from '../../helpers/spaces';
1+
import { spacesMetadata } from '../../helpers/spaces';
22

3-
export default function () {
4-
const plugins = {};
5-
Object.values(spaces).forEach((space: any) => {
6-
Object.keys(space.plugins || {}).forEach(plugin => {
7-
plugins[plugin] = (plugins[plugin] || 0) + 1;
8-
});
9-
});
10-
return Object.entries(plugins).map(plugin => ({
11-
id: plugin[0],
12-
spacesCount: plugin[1]
3+
export default function getPlugins() {
4+
const pluginUsageCount: Record<string, number> = {};
5+
6+
for (const { pluginNames } of Object.values(spacesMetadata)) {
7+
for (const plugin of pluginNames) {
8+
pluginUsageCount[plugin] = (pluginUsageCount[plugin] || 0) + 1;
9+
}
10+
}
11+
12+
return Object.entries(pluginUsageCount).map(([id, spacesCount]) => ({
13+
id,
14+
spacesCount
1315
}));
1416
}

src/graphql/operations/skins.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import { spaces } from '../../helpers/spaces';
1+
import { spacesMetadata } from '../../helpers/spaces';
22

3-
export default function () {
4-
const skins = {};
5-
Object.values(spaces).forEach((space: any) => {
6-
if (space.skin)
7-
skins[space.skin] = skins[space.skin] ? skins[space.skin] + 1 : 1;
8-
});
9-
return Object.entries(skins).map(skin => ({
10-
id: skin[0],
11-
spacesCount: skin[1]
3+
export default function getSkins() {
4+
const skins: Record<string, number> = {};
5+
6+
for (const { skin } of Object.values(spacesMetadata)) {
7+
if (skin) {
8+
skins[skin] = (skins[skin] || 0) + 1;
9+
}
10+
}
11+
12+
return Object.entries(skins).map(([id, spacesCount]) => ({
13+
id,
14+
spacesCount
1215
}));
1316
}

src/graphql/operations/validations.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { spaces } from '../../helpers/spaces';
1+
import { spacesMetadata } from '../../helpers/spaces';
22

3-
export default function () {
4-
const validations = {};
5-
Object.values(spaces).forEach((space: any) => {
6-
if (space.validation)
7-
validations[space.validation.name] =
8-
(validations[space.validation.name] || 0) + 1;
9-
});
10-
return Object.entries(validations).map(validation => ({
11-
id: validation[0],
12-
spacesCount: validation[1]
3+
export default function getValidations() {
4+
const validations: Record<string, number> = {};
5+
6+
for (const { validationName } of Object.values(spacesMetadata)) {
7+
if (validationName) {
8+
validations[validationName] = (validations[validationName] || 0) + 1;
9+
}
10+
}
11+
12+
return Object.entries(validations).map(([id, spacesCount]) => ({
13+
id,
14+
spacesCount
1315
}));
1416
}

src/helpers/spaces.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ const TESTNET_NETWORKS = (
1919
.filter(network => network.testnet)
2020
.map(network => network.key);
2121

22-
export let spaces = {};
23-
2422
export let rankedSpaces: Metadata[] = [];
2523

2624
export let networkSpaceCounts: Record<string, number> = {};
@@ -39,6 +37,8 @@ type Metadata = {
3937
popularity: number;
4038
rank: number | null;
4139
private: boolean;
40+
skin: string | null;
41+
validationName: string | null;
4242
categories: string[];
4343
networks: string[];
4444
counts: {
@@ -100,7 +100,7 @@ function sortSpaces() {
100100
});
101101
}
102102

103-
function mapSpaces() {
103+
function mapSpaces(spaces: Record<string, any>) {
104104
networkSpaceCounts = {};
105105

106106
Object.entries(spaces).forEach(([id, space]: any) => {
@@ -136,6 +136,8 @@ function mapSpaces() {
136136
rank: spacesMetadata[id]?.rank || null,
137137
private: space.private ?? false,
138138
categories: space.categories ?? [],
139+
skin: space.skin || null,
140+
validationName: space.validation?.name || null,
139141
networks,
140142
counts: {
141143
activeProposals: spacesMetadata[id]?.counts?.activeProposals || 0,
@@ -165,7 +167,7 @@ async function loadSpaces() {
165167
`;
166168
const results = await db.queryAsync(query);
167169

168-
spaces = Object.fromEntries(
170+
const spaces = Object.fromEntries(
169171
results.map(space => [
170172
space.id,
171173
{
@@ -188,7 +190,7 @@ async function loadSpaces() {
188190
1000
189191
).toFixed()}s)`
190192
);
191-
mapSpaces();
193+
mapSpaces(spaces);
192194
}
193195

194196
async function getProposals(): Promise<

src/helpers/strategies.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { URL } from 'url';
22
import { capture } from '@snapshot-labs/snapshot-sentry';
33
import snapshot from '@snapshot-labs/snapshot.js';
44
import log from './log';
5-
import { spaces } from './spaces';
5+
import { spacesMetadata } from './spaces';
66

77
export let strategies: any[] = [];
88
export let strategiesObj: any = {};
@@ -26,17 +26,17 @@ async function loadStrategies() {
2626
return true;
2727
}
2828

29-
Object.values(spaces).forEach((space: any) => {
30-
const ids = new Set<string>(
31-
space.strategies.map(strategy => strategy.name)
32-
);
29+
Object.values(spacesMetadata).forEach(({ verified, strategyNames }) => {
30+
const ids = new Set<string>(strategyNames);
3331
ids.forEach(id => {
34-
if (res[id]) {
35-
res[id].spacesCount = (res[id].spacesCount || 0) + 1;
32+
if (!res[id]) {
33+
return;
34+
}
35+
36+
res[id].spacesCount = (res[id].spacesCount || 0) + 1;
3637

37-
if (space.verified) {
38-
res[id].verifiedSpacesCount = (res[id].verifiedSpacesCount || 0) + 1;
39-
}
38+
if (verified) {
39+
res[id].verifiedSpacesCount = (res[id].verifiedSpacesCount || 0) + 1;
4040
}
4141
});
4242
});

0 commit comments

Comments
 (0)