Skip to content

Commit

Permalink
fix(integration): make sure no duplicate protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
lukicenturi committed Jul 18, 2024
1 parent 80f258c commit 9f2e47b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
31 changes: 28 additions & 3 deletions composables/integrations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { convertKeys } from '~/utils/api';
import { IntegrationData } from '~/types/integrations';
import { IntegrationData, type IntegrationItem } from '~/types/integrations';
import LocalIntegrationData from '~/public/integrations/all.json';

export const useIntegrationsData = createSharedComposable(() => {
Expand Down Expand Up @@ -31,14 +31,39 @@ export const useIntegrationsData = createSharedComposable(() => {
}
};

const filterDuplicateData = (data: IntegrationData): IntegrationData => {
const uniqueProtocols: Record<string, IntegrationItem> = {};

data.protocols.forEach((protocol) => {
const firstWord = protocol.label.split(' ')[0]; // Get the first word of label

// Check if we already have an entry with this first word and same image
if (!(firstWord in uniqueProtocols) || uniqueProtocols[firstWord].image !== protocol.image) {
// If not, add it to uniqueProtocols
uniqueProtocols[firstWord] = protocol;
}
else {
// If there's a duplicate, modify the label of the existing one
uniqueProtocols[firstWord].label = firstWord;
}
});

const uniqueProtocolList = Object.values(uniqueProtocols);

return {
...data,
protocols: uniqueProtocolList,
};
};

const data = asyncComputed<IntegrationData>(async () => {
if (isDev)
return LocalIntegrationData;
return filterDuplicateData(LocalIntegrationData);

const remoteData = await getRemoteIntegrationData();

if (remoteData)
return remoteData;
return filterDuplicateData(remoteData);

return defaultData();
}, defaultData());
Expand Down
2 changes: 2 additions & 0 deletions types/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export const IntegrationItem = z.object({
label: z.string(),
});

export type IntegrationItem = z.infer<typeof IntegrationItem>;

export const IntegrationData = z.object({
blockchains: z.array(IntegrationItem),
exchanges: z.array(IntegrationItem),
Expand Down

0 comments on commit 9f2e47b

Please sign in to comment.