Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(integration): make sure no duplicate protocols #230

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading