Skip to content

Make foreign keys clickable using tag2link #270

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
37 changes: 29 additions & 8 deletions lib/featureDiff/DiffColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import React from 'react';
import PropTypes from 'prop-types';
import { config } from '../config';

export const DiffColumn = function({ diff, prop, type, propClass }) {
return (
<td className={propClass}>
<DiffColumnContent diff={diff} prop={prop} type={type} />
</td>
);
};
export class DiffColumn extends React.Component {
render() {
return (
<td className={this.props.propClass}>
<DiffColumnContent {...this.props} {...this.context} />
</td>
);
}
}

const DiffColumnContent = function({ diff, prop, type }) {
const DiffColumnContent = function({ diff, prop, type, tag2link }) {
let renderOutput;
const value = diff[prop][type],
propIsWikidata = typeof prop == 'string' && /wikidata$/.test(prop);
Expand Down Expand Up @@ -54,6 +56,21 @@ const DiffColumnContent = function({ diff, prop, type }) {
}
});
renderOutput = <span>{renderArray}</span>;
} else if (tag2link[prop] && typeof value === 'string') {
// This is a foreign key which is defined in the tag2link DB.
// So, we render a clickable link.
renderOutput = (
<span>
<a
target="_blank"
rel="noopener noreferrer"
className="cmap-wikidata-link"
href={tag2link[prop].replace(/\$1/g, value)}
>
{value}
</a>
</span>
);
} else {
// Standard tag, no processing needed
renderOutput = <span>{value}</span>;
Expand All @@ -67,3 +84,7 @@ DiffColumn.propTypes = {
type: PropTypes.string.isRequired,
propClass: PropTypes.string
};

DiffColumn.contextTypes = {
tag2link: PropTypes.objectOf(PropTypes.string)
};
17 changes: 10 additions & 7 deletions lib/featureDiff/DiffTable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import { DiffRows } from './DiffRows';
import { Tag2LinkContext } from '../tag2link';

//Renders the markup for a table

Expand Down Expand Up @@ -30,13 +31,15 @@ export const DiffTable = function({ diff, ignoreList, header }) {
</tr>
</thead>
)}
<DiffRows
diff={diff}
sortedProps={sortedProps}
types={types}
isAddedFeature={isAddedFeature}
ignoreList={ignoreList}
/>
<Tag2LinkContext>
<DiffRows
diff={diff}
sortedProps={sortedProps}
types={types}
isAddedFeature={isAddedFeature}
ignoreList={ignoreList}
/>
</Tag2LinkContext>
</table>
);
};
Expand Down
81 changes: 81 additions & 0 deletions lib/tag2link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// @ts-check
import React from 'react';
import PropTypes from 'prop-types';

const URL = 'https://cdn.jsdelivr.net/gh/JOSM/tag2link@master/index.json';

const RANKS = ['deprecated', 'normal', 'preferred'];

/** @type {Promise<State> | undefined} */
let promise;

/**
* @param {Tag2LinkItem[]} input
* @returns {State}
*/
function convertSourceData(input) {
/** @type {Record<string, string>} */
const output = {};

const allKeys = new Set(input.map(item => item.key));

for (const key of allKeys) {
// find the item with the best rank
const bestDefinition = input
.filter(item => item.key === key)
.sort((a, b) => RANKS.indexOf(b.rank) - RANKS.indexOf(a.rank))[0];

output[key.replace('Key:', '')] = bestDefinition.url;
}

return { tag2link: output };
}

/**
* @typedef {{
* key: `Key:${string}`;
* url: string;
* source: string;
* rank: "normal" | "preferred";
* }} Tag2LinkItem
*
* @typedef {{
* tag2link: Record<string, string>;
* }} State
*
* @typedef {React.PropsWithChildren} Props
*/

/** @type {React.Component<Props, State>} */
export class Tag2LinkContext extends React.Component {
/** @param {Props} props */
constructor(props) {
super(props);
this.state = { tag2link: {} };
}

componentDidMount() {
if (!promise) {
promise = fetch(URL)
.then(r => r.json())
.then(convertSourceData);
}
promise.then(state => this.setState(state));
}

getChildContext() {
return this.state;
}

render() {
return this.props.children;
}
}

Tag2LinkContext.propTypes = {
children: PropTypes.node
};

Tag2LinkContext.childContextTypes = {
tag2link: PropTypes.objectOf(PropTypes.string)
};