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

useKey #428

Closed
wants to merge 2 commits into from
Closed
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
19 changes: 10 additions & 9 deletions src/js/components/user/Badge.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import { JSX } from 'preact';

import Key from '../../nostr/Key';
import { useKey } from '@/hooks/useKey';

import SocialNetwork from '../../nostr/SocialNetwork';
import { translate as t } from '../../translations/Translation.mjs';
import Icons from '../../utils/Icons';

export default function Badge(props): JSX.Element | null {
const myPub = Key.getPubKey();
const hexAddress = Key.toNostrHexAddress(props.pub);
if (hexAddress === myPub) {
const { hexKey, isMe, myPubKey } = useKey(props.pub);

Check failure on line 11 in src/js/components/user/Badge.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
if(!props.pub) return null; // If no pub key, don't render anything

Check failure on line 12 in src/js/components/user/Badge.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `·`

if (isMe) {
return (
<span class="mx-2 text-iris-blue tooltip" data-tip={t('you')}>
{Icons.checkmark}
</span>
);
}
if (!hexAddress) {
return null;
}
const following = SocialNetwork.isFollowing(myPub, hexAddress);

const following = SocialNetwork.isFollowing(myPubKey, hexKey);
if (following) {
return (
<span class="ml-2 text-iris-blue tooltip" data-tip={t('following')}>
{Icons.checkmark}
</span>
);
} else {
const count = SocialNetwork.followedByFriendsCount(hexAddress);
const count = SocialNetwork.followedByFriendsCount(hexKey);
if (count > 0) {
const className = count > 10 ? 'text-iris-orange' : '';
return (
Expand Down
30 changes: 30 additions & 0 deletions src/js/hooks/useKey.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useEffect, useState } from "preact/hooks"

Check failure on line 1 in src/js/hooks/useKey.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `"preact/hooks"` with `'preact/hooks';`

import Key from "@/nostr/Key";

Check failure on line 3 in src/js/hooks/useKey.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `"@/nostr/Key"` with `'@/nostr/Key'`
import { BECH32, ID, STR } from "@/utils/UniqueIds";

Check failure on line 4 in src/js/hooks/useKey.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `"@/utils/UniqueIds"` with `'@/utils/UniqueIds'`

function createKeyData(str: string | undefined, prefix: string = 'npub') {
const myPubKey = Key.getPubKey();
const uid = ID(str || myPubKey);
const hexKey = STR(uid);
const bech32Key = BECH32(uid, prefix);
return {
key: str,
uid,
bech32Key,
hexKey,
isMe: hexKey === myPubKey,
myPubKey

Check failure on line 17 in src/js/hooks/useKey.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
};
}

export function useKey(str: string | undefined, prefix: string = 'npub') {
const [keyData, setKeyData] = useState(createKeyData(str, prefix));

useEffect(() => {
const data = createKeyData(str, prefix);
setKeyData(data);
}, [str, prefix]);

return keyData;
}

Check failure on line 30 in src/js/hooks/useKey.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎`
17 changes: 15 additions & 2 deletions src/js/utils/UniqueIds.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import Key from "@/nostr/Key";

Check failure on line 1 in src/js/utils/UniqueIds.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `"@/nostr/Key"` with `'@/nostr/Key'`

// should this be a class instead? convert all strings to internal representation, enable comparison
export type UID = number;


Check failure on line 6 in src/js/utils/UniqueIds.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
// save space by mapping strs to internal unique ids
export class UniqueIds {
static strToUniqueId = new Map<string, UID>();
static uniqueIdToStr = new Map<UID, string>();
static currentUniqueId = 0;

static id(str: string): UID {
if (str.startsWith('npub')) {
throw new Error('use hex instead of npub ' + str);
if(!str) throw new Error('ID(str) is undefined');

Check failure on line 14 in src/js/utils/UniqueIds.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `·`

if (str.startsWith('npub') || str.startsWith('note') || str.startsWith('nsec')) {
str = Key.toNostrHexAddress(str) as string; // Convert to hex
if(!str) throw new Error('ID(str) is invalid or empty');
}

const existing = UniqueIds.strToUniqueId.get(str);
if (existing) {
return existing;
Expand All @@ -32,7 +39,13 @@
static has(str: string): boolean {
return UniqueIds.strToUniqueId.has(str);
}

static bech32(id: number, prefix: string = 'npub'): string {
return Key.toNostrBech32Address(UniqueIds.str(id), prefix) || '';
}

}

export const STR = UniqueIds.str;
export const ID = UniqueIds.id;
export const BECH32 = UniqueIds.bech32;
Loading