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

[Bug] Fix custom map style input #2564

Merged
merged 3 commits into from
Jul 15, 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
2 changes: 1 addition & 1 deletion src/components/src/modal-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ export default function ModalContainerFactory(
onConfirm: this._onAddCustomMapStyle,
confirmButton: {
large: true,
disabled: !mapStyle.inputStyle.style,
disabled: !mapStyle.inputStyle.url,
children: 'modal.button.addStyle'
}
};
Expand Down
5 changes: 4 additions & 1 deletion src/components/src/modals/add-map-style-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {media} from '@kepler.gl/styles';
import {transformRequest} from '@kepler.gl/utils';
import {injectIntl, IntlShape} from 'react-intl';
import {FormattedMessage} from '@kepler.gl/localization';
import {NO_BASEMAP_ICON} from '@kepler.gl/constants';
import maplibregl from 'maplibre-gl';
import {InputStyle, MapState} from '@kepler.gl/types';

Expand Down Expand Up @@ -194,7 +195,9 @@ function AddMapStyleModalFactory() {
<InputLight
type="text"
value={inputStyle.url || ''}
onChange={({target: {value}}) => this.props.inputMapStyle({url: value})}
onChange={({target: {value}}) =>
this.props.inputMapStyle({url: value, id: 'Custom Style', icon: NO_BASEMAP_ICON})
}
placeholder="e.g. https://basemaps.cartocdn.com/gl/positron-gl-style/style.json"
/>
</StyledModalSection>
Expand Down
11 changes: 4 additions & 7 deletions src/reducers/src/map-style-updaters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Console from 'global/console';
// Utils
import {
getDefaultLayerGroupVisibility,
isValidStyleUrl,
getStyleDownloadUrl,
mergeLayerGroupVisibility,
editTopMapStyle,
Expand Down Expand Up @@ -601,9 +600,7 @@ function getLoadMapStyleTasks(mapStyles, mapboxApiAccessToken, mapboxApiUrl, onS
// @ts-expect-error
.map(({id, url, accessToken}) => ({
id,
url: isValidStyleUrl(url)
? getStyleDownloadUrl(url, accessToken || mapboxApiAccessToken, mapboxApiUrl)
: url
url: getStyleDownloadUrl(url, accessToken || mapboxApiAccessToken, mapboxApiUrl)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ternary wasn't necessary because getStyleDownloadUrl already guards for urls.

}))
.map(LOAD_MAP_STYLE_TASK)
).bimap(
Expand Down Expand Up @@ -698,12 +695,12 @@ export const inputMapStyleUpdater = (

// differentiate between either a url to hosted style json that needs an icon url,
// or an icon already available client-side as a data uri
const isValidUrl = isValidStyleUrl(updated.url);
const isUpdatedIconDataUri = updated.icon?.startsWith('data:image');
const isValid = isValidUrl || Boolean(updated.uploadedFile);
const isValid = Boolean(updated.uploadedFile);
const isMapboxStyleUrl = updated.url?.startsWith('mapbox://') || updated.url?.includes('mapbox.com');

const icon =
isValidUrl && !isUpdatedIconDataUri
!isUpdatedIconDataUri && isMapboxStyleUrl
? getStyleImageIcon({
mapState,
styleUrl: updated.url || '',
Expand Down
1 change: 0 additions & 1 deletion src/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export {
getDefaultLayerGroupVisibility,
editTopMapStyle,
editBottomMapStyle,
isValidStyleUrl,
akre54 marked this conversation as resolved.
Show resolved Hide resolved
getStyleDownloadUrl,
getStyleImageIcon,
scaleMapStyleByResolution,
Expand Down
10 changes: 0 additions & 10 deletions src/utils/src/map-style-utils/mapbox-gl-style-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import {
} from '@kepler.gl/constants';
import {BaseMapStyle, LayerGroup, MapState} from '@kepler.gl/types';

const mapUrlRg = /^mapbox:\/\/styles\/[-a-z0-9]{2,256}\/[-a-z0-9]{2,256}/;
const httpRg = /^(?=(http:|https:))/;

export function getDefaultLayerGroupVisibility({layerGroups = []}: {layerGroups: LayerGroup[]}) {
return layerGroups.reduce(
(accu, layer) => ({
Expand Down Expand Up @@ -102,13 +99,6 @@ export const editBottomMapStyle = memoize(({id, mapStyle, visibleLayerGroups}) =
};
}, resolver);

// valid style url
// mapbox://styles/uberdata/cjfyl03kp1tul2smf5v2tbdd4
// lowercase letters, numbers and dashes only.
export function isValidStyleUrl(url) {
return typeof url === 'string' && Boolean(url.match(mapUrlRg) || url.match(httpRg));
}

export function getStyleDownloadUrl(styleUrl, accessToken, mapboxApiUrl) {
if (styleUrl.startsWith('http')) {
return styleUrl;
Expand Down
2 changes: 1 addition & 1 deletion test/node/reducers/map-style-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ test('#mapStyleReducer -> EDIT_CUSTOM_MAP_STYLE', t => {

t.deepEqual(
nextState.inputStyle,
{...inputMapStyleToEdit, error: false, isValid: true, style: null, uploadedFile: null},
{...inputMapStyleToEdit, error: false, isValid: false, style: null, uploadedFile: null},
'should set the inputStyle'
);

Expand Down
Loading