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

[tools]: Add environment filter/search options to tools #959

Merged
merged 2 commits into from
Oct 2, 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
6 changes: 5 additions & 1 deletion pages/tools/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ export default function Sidebar({

const filters = [
{ label: 'Language', accessorKey: 'languages' },
{ label: 'Dialect', accessorKey: 'drafts' },
{ label: 'Tooling Type', accessorKey: 'toolingTypes' },
{ label: 'Environment', accessorKey: 'environments' },
{ label: 'Dialect', accessorKey: 'drafts' },
{ label: 'License', accessorKey: 'licenses' },
];

Expand All @@ -50,6 +51,9 @@ export default function Sidebar({
toolingTypes: formData
.getAll('toolingTypes')
.map((value) => value as string),
environments: formData
.getAll('environments')
.map((value) => value as string),
};
postAnalytics({ eventType: 'query', eventPayload: newTransform });
return newTransform;
Expand Down
20 changes: 19 additions & 1 deletion pages/tools/hooks/useToolsTransform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Transform {
languages: string[];
drafts: JSONSchemaDraft[];
toolingTypes: string[];
environments: string[];
}

export type TransformUpdate =
Expand All @@ -34,6 +35,7 @@ const buildQueryString = (transform: Transform) => {
languages: transform.languages.join(','),
drafts: transform.drafts.join(','),
toolingTypes: transform.toolingTypes.join(','),
environments: transform.environments.join(','),
}).toString();
};

Expand All @@ -50,6 +52,7 @@ export default function useToolsTransform(tools: JSONSchemaTool[]) {
licenses: [],
drafts: [],
toolingTypes: [],
environments: [],
});

useEffect(() => {
Expand Down Expand Up @@ -78,6 +81,9 @@ export default function useToolsTransform(tools: JSONSchemaTool[]) {
toolingTypes: parseArrayParam(
query.toolingTypes,
) as Transform['toolingTypes'],
environments: parseArrayParam(
query.environments,
) as Transform['environments'],
};

const queryString = buildQueryString(updatedTransform);
Expand Down Expand Up @@ -124,6 +130,7 @@ export default function useToolsTransform(tools: JSONSchemaTool[]) {
licenses: [],
drafts: [],
toolingTypes: [],
environments: [],
};

const queryString = buildQueryString(initialTransform);
Expand Down Expand Up @@ -183,6 +190,7 @@ const filterTools = (
languages: lowerCaseArray(transform.languages),
licenses: lowerCaseArray(transform.licenses),
toolingTypes: lowerCaseArray(transform.toolingTypes),
environments: lowerCaseArray(transform.environments),
drafts: transform.drafts,
};

Expand All @@ -204,14 +212,24 @@ const filterTools = (
lowerCaseTransform.toolingTypes.includes(type.toLowerCase()),
);

const matchesEnvironment =
!lowerCaseTransform.environments.length ||
(tool.environments || []).some((environment) =>
lowerCaseTransform.environments.includes(environment.toLowerCase()),
);

const matchesDraft =
!lowerCaseTransform.drafts.length ||
(tool.supportedDialects?.draft || []).some((draft) =>
lowerCaseTransform.drafts.includes(draft),
);

return (
matchesLanguage && matchesLicense && matchesToolingType && matchesDraft
matchesLanguage &&
matchesLicense &&
matchesToolingType &&
matchesEnvironment &&
matchesDraft
);
});
};
Expand Down
4 changes: 3 additions & 1 deletion pages/tools/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export type FilterCriteriaFields =
| 'languages'
| 'drafts'
| 'toolingTypes'
| 'licenses';
| 'licenses'
| 'environments';

export async function getStaticProps() {
const toolingData = yaml.load(
Expand Down Expand Up @@ -55,6 +56,7 @@ export async function getStaticProps() {
]) as JSONSchemaDraft[],
toolingTypes: getDistinctEntries(toolingData, '$..toolingTypes[*]'),
licenses: getDistinctEntries(toolingData, '$..license'),
environments: getDistinctEntries(toolingData, '$..environments[*]'),
};

filterCriteria.drafts.sort((a, b) => {
Expand Down