Skip to content

Commit

Permalink
Add a new search condition to filter whether the keyboard supports bu…
Browse files Browse the repository at this point in the history
…ilding firmware or not.
  • Loading branch information
yoichiro committed Nov 27, 2023
1 parent 14bcba5 commit a7db264
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/actions/catalog.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const CATALOG_SEARCH_UPDATE_KEYWORD = `${CATALOG_SEARCH_ACTIONS}/UpdateKe
export const CATALOG_SEARCH_CLEAR_FEATURES = `${CATALOG_SEARCH_ACTIONS}/ClearFeatures`;
export const CATALOG_SEARCH_RESET_FEATURES = `${CATALOG_SEARCH_ACTIONS}/ResetFeatures`;
export const CATALOG_SEARCH_UPDATE_ORGANIZATION = `${CATALOG_SEARCH_ACTIONS}/UpdateOrganization`;
export const CATALOG_SEARCH_UPDATE_BUILD_SUPPORT = `${CATALOG_SEARCH_ACTIONS}/UpdateBuildSupport`;
export const CatalogSearchActions = {
updateFeatures: (
value: IKeyboardFeatures | IConditionNotSelected,
Expand Down Expand Up @@ -77,6 +78,12 @@ export const CatalogSearchActions = {
value: organizationId,
};
},
updateBuildSupport: (buildSupport: boolean) => {
return {
type: CATALOG_SEARCH_UPDATE_BUILD_SUPPORT,
value: buildSupport,
};
},
};

export const CATALOG_KEYBOARD_ACTIONS = `@CatalogKeyboard`;
Expand Down
33 changes: 32 additions & 1 deletion src/actions/storage.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,14 +1081,18 @@ export const storageActionsThunk = {
const features = catalog.search.features;
const keyword = catalog.search.keyword;
const organizationId = catalog.search.organizationId;
const buildSupport = catalog.search.buildSupport;
let searchKeyboardsByFeaturesResult =
await storage.instance!.searchKeyboards(features, organizationId);
if (isSuccessful(searchKeyboardsByFeaturesResult)) {
// Keyword search
const definitionDocs =
searchKeyboardsByFeaturesResult.value.documents.filter((doc) =>
doc.name.toLowerCase().includes(keyword.toLowerCase())
);
const filteredDocs = definitionDocs.filter((doc) => {

// Feature search
let filteredDocs = definitionDocs.filter((doc) => {
if (features.length === 0) return true;

let allMatch = true;
Expand All @@ -1099,6 +1103,29 @@ export const storageActionsThunk = {
return allMatch;
});

// Build support search
if (buildSupport) {
const buildableFirmwaresResult =
await storage.instance!.fetchAllBuildableFirmwares();
if (isError(buildableFirmwaresResult)) {
console.error(buildableFirmwaresResult.cause);
dispatch(
NotificationActions.addError(
buildableFirmwaresResult.error,
buildableFirmwaresResult.cause
)
);
return;
}
const buildableFirmwareIds = buildableFirmwaresResult.value.map(
(buildableFirmware) => buildableFirmware.keyboardDefinitionId
);
filteredDocs = filteredDocs.filter((doc) => {
return buildableFirmwareIds.includes(doc.id);
});
}

// Sort by image
filteredDocs.sort((a, b) => {
const countA = a.imageUrl ? 1 : 0; // sort higher with a image
const countB = b.imageUrl ? 1 : 0; // sort higher with a image
Expand All @@ -1110,6 +1137,7 @@ export const storageActionsThunk = {
}
});

// Fetch related organizations
const organizationIds: string[] = filteredDocs
.filter((doc) => doc.authorType === 'organization')
.map((doc) => doc.organizationId)
Expand Down Expand Up @@ -1162,6 +1190,9 @@ export const storageActionsThunk = {
if (organizationId) {
query.organizationId = organizationId;
}
if (buildSupport) {
query.buildSupport = 'true';
}
history.replaceState(null, 'Remap', `/catalog?${qs.stringify(query)}`);
dispatch(CatalogAppActions.updatePhase('list'));
},
Expand Down
3 changes: 3 additions & 0 deletions src/components/catalog/Catalog.container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ const mapDispatchToProps = (_dispatch: any) => {
)
);
}
if (params.buildSupport) {
_dispatch(CatalogSearchActions.updateBuildSupport(true));
}
if (params.features) {
(params.features as string).split(',').forEach((feature: string) => {
if (ALL_KEY_COUNT_TYPE.includes(feature as IKeyboardKeyCountType)) {
Expand Down
1 change: 1 addition & 0 deletions src/components/catalog/search/CatalogSearch.container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const mapStateToProps = (state: RootState) => {
searchResult: state.entities.searchResultKeyboardDocuments,
keyword: state.catalog.search.keyword,
organizationMap: state.entities.searchResultOrganizationMap,
buildSupport: state.catalog.search.buildSupport,
};
};
export type CatalogSearchStateType = ReturnType<typeof mapStateToProps>;
Expand Down
2 changes: 2 additions & 0 deletions src/components/catalog/search/CatalogSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class CatalogSearch extends React.Component<
<CatalogSearchForm
keyword={this.props.keyword!}
features={this.props.features!}
buildSupport={this.props.buildSupport!}
/>
)}
</Grid>
Expand All @@ -139,6 +140,7 @@ class CatalogSearch extends React.Component<
open={this.state.showSearchDialog}
keyword={this.props.keyword!}
features={this.props.features!}
buildSupport={this.props.buildSupport!}
onClose={this.closeSearchDialog.bind(this)}
onSubmit={this.submitSearchDialog.bind(this)}
/>
Expand Down
3 changes: 3 additions & 0 deletions src/components/catalog/search/CatalogSearchDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import { IKeyboardFeatures } from '../../../store/state';
type CatalogSearchDialogState = {
features: IKeyboardFeatures[];
keyword: string;
buildSupport: boolean;
};
type CatalogSearchDialogProps = {
open: boolean;
features: IKeyboardFeatures[];
keyword: string;
buildSupport: boolean;
onClose: (
// eslint-disable-next-line no-unused-vars
originalKeyword: string,
Expand Down Expand Up @@ -67,6 +69,7 @@ export default class CatalogSearchDialog extends React.Component<
onSubmit={this.props.onSubmit.bind(this)}
keyword={this.props.keyword}
features={this.props.features}
buildSupport={this.props.buildSupport}
/>
</Dialog>
);
Expand Down
3 changes: 3 additions & 0 deletions src/components/catalog/search/CatalogSearchForm.container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const mapDispatchToProps = (_dispatch: any) => {
updateOrganizationId: (organizationId: string | undefined) => {
_dispatch(CatalogSearchActions.updateOrganizationId(organizationId));
},
updateBuildSupport: (buildSupport: boolean) => {
_dispatch(CatalogSearchActions.updateBuildSupport(buildSupport));
},
};
};
export type CatalogSearchFormActionsType = ReturnType<
Expand Down
23 changes: 23 additions & 0 deletions src/components/catalog/search/CatalogSearchForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type CatalogSearchFormState = {};
type OwnProps = {
features: IKeyboardFeatures[];
keyword: string;
buildSupport: boolean;
onSubmit?: () => void;
};

Expand Down Expand Up @@ -138,6 +139,11 @@ export default class CatalogSearchForm extends React.Component<
this.props.updateFeatures!(value, ALL_WIRELESS_TYPE);
}

onChangeBuildSupport(event: SelectChangeEvent) {
const value = event.target.value === 'buildSupport';
this.props.updateBuildSupport!(value);
}

// eslint-disable-next-line no-undef
onChangeKeyword(event: React.ChangeEvent<HTMLInputElement>) {
this.props.updateKeyword!(event.target.value);
Expand Down Expand Up @@ -209,6 +215,23 @@ export default class CatalogSearchForm extends React.Component<
</Select>
</FormControl>
</div>
<div className="catalog-search-condition">
<FormControl fullWidth={true}>
<InputLabel id="catalog-search-build">
Build Firmware Support
</InputLabel>
<Select
labelId="catalog-search-build"
label="Build Firmware Support"
value={this.props.buildSupport ? 'buildSupport' : '---'}
onChange={this.onChangeBuildSupport.bind(this)}
size="small"
>
<MenuItem value="---">---</MenuItem>
<MenuItem value="buildSupport">Supported</MenuItem>
</Select>
</FormControl>
</div>
<div className="catalog-search-condition">
<FormControl fullWidth={true}>
<InputLabel id="catalog-search-key-count">
Expand Down
31 changes: 31 additions & 0 deletions src/services/provider/Firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,37 @@ export class FirebaseProvider implements IStorage, IAuth {
}
}

async fetchAllBuildableFirmwares(): Promise<IResult<IBuildableFirmware[]>> {
try {
const querySnapshot = await this.db
.collection('build')
.doc('v1')
.collection('firmwares')
.where('enabled', '==', true)
.get();
return successResultOf(
querySnapshot.docs.map((doc) => {
return {
keyboardDefinitionId: doc.data()!.keyboardDefinitionId,
uid: doc.data()!.uid,
enabled: doc.data()!.enabled,
defaultBootloaderType: doc.data()!.defaultBootloaderType,
qmkFirmwareVersion: doc.data()!.qmkFirmwareVersion,
keyboardDirectoryName: doc.data()!.keyboardDirectoryName || '',
createdAt: doc.data()!.createdAt.toDate(),
updatedAt: doc.data()!.updatedAt.toDate(),
};
})
);
} catch (error) {
console.error(error);
return errorResultOf(
`Fetching all buildable firmwares failed: ${error}`,
error
);
}
}

async fetchBuildableFirmwareFiles(
keyboardDefinitionId: string,
fileType: IBuildableFirmwareFileType
Expand Down
1 change: 1 addition & 0 deletions src/services/storage/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,5 +527,6 @@ export interface IStorage {
taskId: string,
description: string
): Promise<IEmptyResult>;
fetchAllBuildableFirmwares(): Promise<IResult<IBuildableFirmware[]>>;
}
/* eslint-enable no-unused-vars */
5 changes: 5 additions & 0 deletions src/store/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ import {
CATALOG_SEARCH_ACTIONS,
CATALOG_SEARCH_CLEAR_FEATURES,
CATALOG_SEARCH_RESET_FEATURES,
CATALOG_SEARCH_UPDATE_BUILD_SUPPORT,
CATALOG_SEARCH_UPDATE_FEATURES,
CATALOG_SEARCH_UPDATE_KEYWORD,
CATALOG_SEARCH_UPDATE_ORGANIZATION,
Expand Down Expand Up @@ -1080,6 +1081,10 @@ const catalogSearchReducer = (
draft.catalog.search.organizationId = action.value;
break;
}
case CATALOG_SEARCH_UPDATE_BUILD_SUPPORT: {
draft.catalog.search.buildSupport = action.value;
break;
}
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/store/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ export type RootState = {
features: IKeyboardFeatures[];
keyword: string;
organizationId: string | undefined;
buildSupport: boolean;
};
keyboard: {
keymaps: {
Expand Down Expand Up @@ -663,6 +664,7 @@ export const INIT_STATE: RootState = {
features: [],
keyword: '',
organizationId: undefined,
buildSupport: false,
},
keyboard: {
keymaps: [],
Expand Down

0 comments on commit a7db264

Please sign in to comment.