-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// copied from https://github.com/openstreetmap/iD/blob/575046c/modules/services/tag2Link.js | ||
|
||
import { useEffect, useState } from 'react'; | ||
|
||
type RawTag2Link = { | ||
key: `Key:${string}`; | ||
url: string; | ||
source: `${'osmwiki' | 'wikidata'}:P${number}`; | ||
rank: 'preferred' | 'normal' | 'deprecated'; | ||
}; | ||
|
||
const RANKS = ['deprecated', 'normal', 'preferred']; | ||
|
||
async function getTag2Link() { | ||
const array: RawTag2Link[] = await fetch( | ||
'https://cdn.jsdelivr.net/gh/JOSM/tag2link@master/index.json', | ||
).then((r) => r.json()); | ||
|
||
const map = new Map<string, string>(); | ||
|
||
const allKeys = new Set(array.map((item) => item.key)); | ||
|
||
for (const key of allKeys) { | ||
// find an item with the best rank | ||
const bestDefinition = array | ||
.filter((item) => item.key === key) | ||
.sort((a, b) => RANKS.indexOf(b.rank) - RANKS.indexOf(a.rank))[0]; | ||
|
||
map.set(key.replace('Key:', ''), bestDefinition.url); | ||
} | ||
|
||
return map; | ||
} | ||
|
||
let promise: ReturnType<typeof getTag2Link> | undefined; | ||
|
||
export function useTag2link() { | ||
const [value, setValue] = useState<Map<string, string>>(); | ||
|
||
useEffect(() => { | ||
(promise ||= getTag2Link()).then(setValue).catch(console.error); | ||
}, []); | ||
|
||
return value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export function getUrlHost(url: string) { | ||
try { | ||
return new URL(url).host.replace(/^www\./, ''); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
export const MaybeLink: React.FC<{ | ||
value: string; | ||
formatter: string | undefined; | ||
}> = ({ formatter, value }) => { | ||
if (!formatter) return value; | ||
|
||
const url = formatter.replaceAll('$1', value); | ||
return ( | ||
<a | ||
href={url} | ||
title={`View on ${getUrlHost(formatter)}`} | ||
target="_blank" | ||
rel="noreferrer" | ||
> | ||
{value} | ||
</a> | ||
); | ||
}; |