Skip to content

Commit 4724a62

Browse files
committed
feat: enhance DatasetTypes component to include type information in schema handling
1 parent 12b5b4a commit 4724a62

File tree

4 files changed

+71
-25
lines changed

4 files changed

+71
-25
lines changed

src/components/DatasetTypes.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { processSchemaPathsToTypes } from '@/modules/datasets/utils/datasetTypeUtils';
22

3+
34
interface DatasetTypesProps {
4-
schemaPaths?: Array<{ path?: string | null }>;
5+
schemaPaths?: Array<{ path?: string | null; type?: string | null }>;
56
isLoading?: boolean;
67
layout?: 'horizontal' | 'vertical';
78
maxDisplay?: number;
@@ -56,14 +57,21 @@ export function DatasetTypes({
5657
className={`flex ${layout === 'vertical' ? 'flex-col' : 'flex-wrap'} gap-1 ${layout === 'vertical' ? 'max-w-40' : ''}`}
5758
>
5859
{typesToShow.map((type) => (
59-
<span
60+
<div
6061
key={type.name}
61-
className="inline-block w-fit cursor-pointer rounded-lg border px-3 py-2 text-sm font-medium whitespace-nowrap transition-transform duration-200 hover:scale-105"
62+
className="inline-block w-fit cursor-pointer rounded-lg border px-2 py-1 text-sm font-medium whitespace-nowrap transition-transform duration-200 hover:scale-105"
6263
style={type.style}
63-
title={type.name}
64+
title={type.type ? `${type.name} (${type.type})` : type.name}
6465
>
65-
{type.name}
66-
</span>
66+
<div className="flex flex-col items-center gap-0.5">
67+
<span>{type.name}</span>
68+
{type.type && (
69+
<span className="rounded bg-black/10 px-1 py-0.5 text-xs font-normal opacity-70">
70+
{type.type}
71+
</span>
72+
)}
73+
</div>
74+
</div>
6775
))}
6876
{showMoreCount && remainingCount > 0 && (
6977
<span

src/modules/datasets/dataset/protectedDataQuery.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { graphql } from '@/graphql/dataprotector/gql';
22

3+
34
export const datasetSchemaQuery = graphql(`
45
query DatasetSchema($datasetAddress: ID!) {
56
protectedData(id: $datasetAddress) {
67
schema {
78
path
9+
type
810
}
911
}
1012
}
@@ -17,6 +19,7 @@ export const optimizedProtectedDatasQueryString = graphql(`
1719
id
1820
schema {
1921
path
22+
type
2023
}
2124
}
2225
}

src/modules/datasets/hooks/useDatasetsSchemas.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ export function useDatasetsSchemas(datasetAddresses: string[], chainId: number)
2020
);
2121

2222
// Transform data into a map for easy lookup
23-
const schemasMap = new Map<string, Array<{ path?: string | null }>>();
23+
const schemasMap = new Map<string, Array<{ path?: string | null; type?: string | null }>>();
2424

2525
if (result?.protectedDatas) {
26-
result.protectedDatas.forEach((protectedData: any) => {
27-
if (protectedData.id) {
26+
result.protectedDatas.forEach((protectedData) => {
27+
if (protectedData?.id) {
2828
schemasMap.set(protectedData.id, protectedData.schema || []);
2929
}
3030
});
@@ -54,7 +54,7 @@ export function useDatasetsSchemas(datasetAddresses: string[], chainId: number)
5454
});
5555

5656
const results = await Promise.allSettled(schemaPromises);
57-
const schemasMap = new Map();
57+
const schemasMap = new Map<string, Array<{ path?: string | null; type?: string | null }>>();
5858

5959
results.forEach((result, index) => {
6060
if (result.status === 'fulfilled') {
Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface DatasetTypeInfo {
22
name: string;
3+
type?: string; // Data type like 'string', 'boolean', etc.
34
style: {
45
backgroundColor: string;
56
borderColor: string;
@@ -12,45 +13,79 @@ export function hashString(str: string): number {
1213
let hash = 0;
1314
for (let i = 0; i < str.length; i++) {
1415
const char = str.charCodeAt(i);
15-
hash = ((hash << 5) - hash) + char;
16+
hash = (hash << 5) - hash + char;
1617
hash = hash & hash; // Convert to 32bit integer
1718
}
1819
return Math.abs(hash);
1920
}
2021

2122
// Function to create a dynamic type for any path
22-
export function createDynamicType(path: string): DatasetTypeInfo {
23+
export function createDynamicType(
24+
path: string,
25+
type?: string
26+
): DatasetTypeInfo {
2327
const hash = hashString(path);
24-
const hue = hash % 360;
25-
28+
29+
// Create a more varied hash by combining multiple factors
30+
const pathLength = path.length;
31+
const firstChar = path.charCodeAt(0);
32+
const lastChar = path.charCodeAt(path.length - 1);
33+
const complexHash =
34+
(hash + pathLength * 17 + firstChar * 31 + lastChar * 13) % 360;
35+
36+
// Define distinct color palette with good separation
37+
const distinctColors = [
38+
20, // Orange-red
39+
45, // Orange
40+
70, // Yellow-orange
41+
95, // Yellow-green
42+
120, // Green
43+
145, // Blue-green
44+
170, // Cyan
45+
195, // Light blue (but shifted to avoid dark blue)
46+
280, // Purple
47+
305, // Magenta
48+
330, // Pink-red
49+
350, // Red-orange
50+
];
51+
52+
// Use modulo to select from our distinct color palette
53+
const colorIndex = complexHash % distinctColors.length;
54+
const selectedHue = distinctColors[colorIndex];
55+
56+
// Add slight variation based on the original hash to avoid exact duplicates
57+
const hueVariation = (hash % 20) - 10; // -10 to +10 degrees variation
58+
const finalHue = (selectedHue + hueVariation + 360) % 360;
59+
2660
// Convert path to a readable name (e.g., "targetPrivacyPassV2_prod" -> "Target Privacy Pass V2 Prod")
2761
const name = path
2862
.replace(/([A-Z])/g, ' $1') // Add space before capital letters
2963
.replace(/_/g, ' ') // Replace underscores with spaces
30-
.replace(/^./, str => str.toUpperCase()) // Capitalize first letter
31-
.replace(/\s+./g, str => str.toUpperCase()) // Capitalize first letter of each word
64+
.replace(/^./, (str) => str.toUpperCase()) // Capitalize first letter
65+
.replace(/\s+./g, (str) => str.toUpperCase()) // Capitalize first letter of each word
3266
.trim();
3367

3468
return {
3569
name,
70+
type,
3671
style: {
37-
backgroundColor: `hsla(${hue}, 60%, 50%, 0.08)`,
38-
borderColor: `hsla(${hue}, 60%, 50%, 0.2)`,
39-
color: `hsl(${hue}, 60%, 40%)`
40-
}
72+
backgroundColor: `hsla(${finalHue}, 80%, 60%, 0.15)`,
73+
borderColor: `hsla(${finalHue}, 80%, 60%, 0.4)`,
74+
color: `hsl(${finalHue}, 80%, 65%)`,
75+
},
4176
};
42-
}
43-
44-
// Function to process schema paths into types
45-
export function processSchemaPathsToTypes(schemaPaths?: Array<{ path?: string | null }>): DatasetTypeInfo[] {
77+
} // Function to process schema paths into types
78+
export function processSchemaPathsToTypes(
79+
schemaPaths?: Array<{ path?: string | null; type?: string | null }>
80+
): DatasetTypeInfo[] {
4681
if (!schemaPaths || schemaPaths.length === 0) {
4782
return [];
4883
}
4984

5085
return schemaPaths
5186
.map((item) => {
5287
if (!item.path) return null;
53-
return createDynamicType(item.path);
88+
return createDynamicType(item.path, item.type || undefined);
5489
})
5590
.filter(Boolean) as DatasetTypeInfo[];
5691
}

0 commit comments

Comments
 (0)