fix more warnings, store transformed text in LRUCache of 1000

This commit is contained in:
Martti Malmi 2024-01-10 19:54:01 +02:00
parent 8e37e0fbed
commit e6a42db658
18 changed files with 203 additions and 182 deletions

View File

@ -0,0 +1,16 @@
import {ExternalStore} from "@snort/shared";
class CommunityLeadersStore extends ExternalStore<Array<string>> {
#leaders: Array<string> = [];
setLeaders(arr: Array<string>) {
this.#leaders = arr;
this.notifyChange();
}
takeSnapshot(): string[] {
return [...this.#leaders];
}
}
export const LeadersStore = new CommunityLeadersStore();

View File

@ -0,0 +1,6 @@
import {ParsedFragment} from "@snort/system";
import {LRUCache} from "typescript-lru-cache";
export const TextCache = new LRUCache<string, Array<ParsedFragment>>({
maxSize: 1000
});

View File

@ -0,0 +1,114 @@
import {ReactNode} from "react";
import {FormattedMessage} from "react-intl";
import Icon from "@/Components/Icons/Icon";
import {Newest} from "@/Utils/Login";
export type RootTab =
| "following"
| "followed-by-friends"
| "conversations"
| "trending-notes"
| "trending-people"
| "suggested"
| "tags"
| "global";
export function rootTabItems(base: string, pubKey: string | undefined, tags: Newest<Array<string>>) {
const menuItems = [
{
tab: "following",
path: `${base}/notes`,
show: Boolean(pubKey),
element: (
<>
<Icon name="user-v2"/>
<FormattedMessage defaultMessage="Following" id="cPIKU2"/>
</>
),
},
{
tab: "trending-notes",
path: `${base}/trending/notes`,
show: true,
element: (
<>
<Icon name="fire"/>
<FormattedMessage defaultMessage="Trending Notes" id="Ix8l+B"/>
</>
),
},
{
tab: "conversations",
path: `${base}/conversations`,
show: Boolean(pubKey),
element: (
<>
<Icon name="message-chat-circle"/>
<FormattedMessage defaultMessage="Conversations" id="1udzha"/>
</>
),
},
{
tab: "followed-by-friends",
path: `${base}/followed-by-friends`,
show: Boolean(pubKey),
element: (
<>
<Icon name="user-v2"/>
<FormattedMessage defaultMessage="Followed by friends" id="voxBKC"/>
</>
),
},
{
tab: "suggested",
path: `${base}/suggested`,
show: Boolean(pubKey),
element: (
<>
<Icon name="thumbs-up"/>
<FormattedMessage defaultMessage="Suggested Follows" id="C8HhVE"/>
</>
),
},
{
tab: "trending-hashtags",
path: `${base}/trending/hashtags`,
show: true,
element: (
<>
<Icon name="hash"/>
<FormattedMessage defaultMessage="Trending Hashtags" id="XXm7jJ"/>
</>
),
},
{
tab: "global",
path: `${base}/global`,
show: true,
element: (
<>
<Icon name="globe"/>
<FormattedMessage defaultMessage="Global" id="EWyQH5"/>
</>
),
},
{
tab: "tags",
path: `${base}/topics`,
show: tags.item.length > 0,
element: (
<>
<Icon name="hash"/>
<FormattedMessage defaultMessage="Topics" id="kc79d3"/>
</>
),
},
] as Array<{
tab: RootTab;
path: string;
show: boolean;
element: ReactNode;
}>;
return menuItems;
}

View File

@ -1,122 +1,12 @@
import "./RootTabs.css";
import { Menu, MenuItem } from "@szhsin/react-menu";
import { ReactNode, useEffect, useMemo, useState } from "react";
import { FormattedMessage } from "react-intl";
import { useLocation, useNavigate } from "react-router-dom";
import {Menu, MenuItem} from "@szhsin/react-menu";
import {useEffect, useMemo, useState} from "react";
import {useLocation, useNavigate} from "react-router-dom";
import {RootTab, rootTabItems} from "@/Components/Feed/RootTabItems";
import Icon from "@/Components/Icons/Icon";
import useLogin from "@/Hooks/useLogin";
import { Newest } from "@/Utils/Login";
export type RootTab =
| "following"
| "followed-by-friends"
| "conversations"
| "trending-notes"
| "trending-people"
| "suggested"
| "tags"
| "global";
export function rootTabItems(base: string, pubKey: string | undefined, tags: Newest<Array<string>>) {
const menuItems = [
{
tab: "following",
path: `${base}/notes`,
show: Boolean(pubKey),
element: (
<>
<Icon name="user-v2" />
<FormattedMessage defaultMessage="Following" id="cPIKU2" />
</>
),
},
{
tab: "trending-notes",
path: `${base}/trending/notes`,
show: true,
element: (
<>
<Icon name="fire" />
<FormattedMessage defaultMessage="Trending Notes" id="Ix8l+B" />
</>
),
},
{
tab: "conversations",
path: `${base}/conversations`,
show: Boolean(pubKey),
element: (
<>
<Icon name="message-chat-circle" />
<FormattedMessage defaultMessage="Conversations" id="1udzha" />
</>
),
},
{
tab: "followed-by-friends",
path: `${base}/followed-by-friends`,
show: Boolean(pubKey),
element: (
<>
<Icon name="user-v2" />
<FormattedMessage defaultMessage="Followed by friends" id="voxBKC" />
</>
),
},
{
tab: "suggested",
path: `${base}/suggested`,
show: Boolean(pubKey),
element: (
<>
<Icon name="thumbs-up" />
<FormattedMessage defaultMessage="Suggested Follows" id="C8HhVE" />
</>
),
},
{
tab: "trending-hashtags",
path: `${base}/trending/hashtags`,
show: true,
element: (
<>
<Icon name="hash" />
<FormattedMessage defaultMessage="Trending Hashtags" id="XXm7jJ" />
</>
),
},
{
tab: "global",
path: `${base}/global`,
show: true,
element: (
<>
<Icon name="globe" />
<FormattedMessage defaultMessage="Global" id="EWyQH5" />
</>
),
},
{
tab: "tags",
path: `${base}/topics`,
show: tags.item.length > 0,
element: (
<>
<Icon name="hash" />
<FormattedMessage defaultMessage="Topics" id="kc79d3" />
</>
),
},
] as Array<{
tab: RootTab;
path: string;
show: boolean;
element: ReactNode;
}>;
return menuItems;
}
export function RootTabs({ base = "/" }: { base: string }) {
const navigate = useNavigate();

View File

@ -1,7 +1,8 @@
import { ReactNode, useEffect, useState } from "react";
import { IntlProvider as ReactIntlProvider } from "react-intl";
import { DefaultLocale, useLocale } from "@/Components/IntlProvider/IntlProviderUtils";
import { DefaultLocale } from "@/Components/IntlProvider/IntlProviderUtils";
import {useLocale} from "@/Components/IntlProvider/useLocale";
import enMessages from "@/translations/en.json";
const getMessages = (locale: string) => {

View File

@ -1,37 +1,5 @@
import { ExternalStore } from "@snort/shared";
import { useSyncExternalStore } from "react";
import useLogin from "@/Hooks/useLogin";
export const DefaultLocale = "en-US";
class LangStore extends ExternalStore<string | null> {
setLang(s: string) {
localStorage.setItem("lang", s);
this.notifyChange();
}
takeSnapshot() {
return localStorage.getItem("lang");
}
}
const LangOverride = new LangStore();
export function useLocale() {
const { language } = useLogin(s => ({ language: s.appData.item.preferences.language }));
const loggedOutLang = useSyncExternalStore(
c => LangOverride.hook(c),
() => LangOverride.snapshot(),
);
const locale = language ?? loggedOutLang ?? getLocale();
return {
locale,
lang: locale.toLowerCase().split(/[_-]+/)[0],
setOverride: (s: string) => LangOverride.setLang(s),
};
}
export const getLocale = () => {
return (navigator.languages && navigator.languages[0]) ?? navigator.language ?? DefaultLocale;
};

View File

@ -0,0 +1,14 @@
import {ExternalStore} from "@snort/shared";
class LangStore extends ExternalStore<string | null> {
setLang(s: string) {
localStorage.setItem("lang", s);
this.notifyChange();
}
takeSnapshot() {
return localStorage.getItem("lang");
}
}
export const LangOverride = new LangStore();

View File

@ -0,0 +1,19 @@
import {useSyncExternalStore} from "react";
import {getLocale} from "@/Components/IntlProvider/IntlProviderUtils";
import {LangOverride} from "@/Components/IntlProvider/langStore";
import useLogin from "@/Hooks/useLogin";
export function useLocale() {
const {language} = useLogin(s => ({language: s.appData.item.preferences.language}));
const loggedOutLang = useSyncExternalStore(
c => LangOverride.hook(c),
() => LangOverride.snapshot(),
);
const locale = language ?? loggedOutLang ?? getLocale();
return {
locale,
lang: locale.toLowerCase().split(/[_-]+/)[0],
setOverride: (s: string) => LangOverride.setLang(s),
};
}

View File

@ -2,7 +2,7 @@ import classNames from "classnames";
import { ReactNode } from "react";
import { Link } from "react-router-dom";
import { useLocale } from "@/Components/IntlProvider/IntlProviderUtils";
import {useLocale} from "@/Components/IntlProvider/useLocale";
import PageSpinner from "@/Components/PageSpinner";
import NostrBandApi from "@/External/NostrBand";
import useCachedFetch from "@/Hooks/useCachedFetch";

View File

@ -7,7 +7,7 @@ import { ErrorOrOffline } from "@/Components/ErrorOrOffline";
import Note from "@/Components/Event/EventComponent";
import { DisplayAs, DisplayAsSelector } from "@/Components/Feed/DisplayAsSelector";
import ImageGridItem from "@/Components/Feed/ImageGridItem";
import { useLocale } from "@/Components/IntlProvider/IntlProviderUtils";
import {useLocale} from "@/Components/IntlProvider/useLocale";
import PageSpinner from "@/Components/PageSpinner";
import { SpotlightThreadModal } from "@/Components/Spotlight/SpotlightThreadModal";
import ShortNote from "@/Components/Trending/ShortNote";

View File

@ -3,12 +3,12 @@ import "./Nip05.css";
import { HexKey } from "@snort/system";
import { useUserProfile } from "@snort/system-react";
export function useIsVerified(pubkey?: HexKey, bypassCheck?: boolean) {
function useIsVerified(pubkey?: HexKey, bypassCheck?: boolean) {
const profile = useUserProfile(pubkey);
return { isVerified: bypassCheck || profile?.isNostrAddressValid };
}
export interface Nip05Params {
interface Nip05Params {
nip05?: string;
pubkey: HexKey;
verifyNip?: boolean;

View File

@ -1,23 +1,10 @@
import { ExternalStore, unwrap } from "@snort/shared";
import { EventKind, parseNostrLink } from "@snort/system";
import { useEffect, useSyncExternalStore } from "react";
import {unwrap} from "@snort/shared";
import {EventKind, parseNostrLink} from "@snort/system";
import {useEffect, useSyncExternalStore} from "react";
import { useLinkList } from "./useLists";
import {LeadersStore} from "@/Cache/CommunityLeadersStore";
class CommunityLeadersStore extends ExternalStore<Array<string>> {
#leaders: Array<string> = [];
setLeaders(arr: Array<string>) {
this.#leaders = arr;
this.notifyChange();
}
takeSnapshot(): string[] {
return [...this.#leaders];
}
}
const LeadersStore = new CommunityLeadersStore();
import {useLinkList} from "./useLists";
export function useCommunityLeaders() {
const link = parseNostrLink(unwrap(CONFIG.communityLeaders).list);

View File

@ -1,12 +1,10 @@
import { bech32ToHex } from "@snort/shared";
import { EventKind, RequestBuilder } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react";
import { useMemo } from "react";
import {bech32ToHex} from "@snort/shared";
import {EventKind, RequestBuilder} from "@snort/system";
import {useRequestBuilder} from "@snort/system-react";
import {useMemo} from "react";
import { getNewest } from "@/Utils";
// Snort backend publishes rates
const SnortPubkey = "npub1sn0rtcjcf543gj4wsg7fa59s700d5ztys5ctj0g69g2x6802npjqhjjtws";
import {getNewest} from "@/Utils";
import {SnortPubkey} from "@/Utils/Const";
export function useRates(symbol: string, leaveOpen = true) {
const sub = useMemo(() => {

View File

@ -1,6 +1,6 @@
import { ParsedFragment, transformText } from "@snort/system";
import {transformText} from "@snort/system";
const TextCache = new Map<string, Array<ParsedFragment>>();
import {TextCache} from "@/Cache/TextCache";
export function transformTextCached(id: string, content: string, tags: Array<Array<string>>) {
if (content.length > 0) {

View File

@ -5,7 +5,8 @@ import React, { useCallback, useMemo } from "react";
import { FormattedMessage } from "react-intl";
import { useLocation, useNavigate } from "react-router-dom";
import { rootTabItems, RootTabs } from "@/Components/Feed/RootTabs";
import {rootTabItems} from "@/Components/Feed/RootTabItems";
import { RootTabs } from "@/Components/Feed/RootTabs";
import Icon from "@/Components/Icons/Icon";
import DisplayName from "@/Components/User/DisplayName";
import useLogin from "@/Hooks/useLogin";

View File

@ -3,7 +3,8 @@ import "./index.css";
import { Outlet, RouteObject } from "react-router-dom";
import Icon from "@/Components/Icons/Icon";
import { AllLanguageCodes, useLocale } from "@/Components/IntlProvider/IntlProviderUtils";
import { AllLanguageCodes } from "@/Components/IntlProvider/IntlProviderUtils";
import {useLocale} from "@/Components/IntlProvider/useLocale";
import { Discover } from "./discover";
import { Moderation } from "./moderation";

View File

@ -2,7 +2,8 @@ import "./Preferences.css";
import { FormattedMessage, useIntl } from "react-intl";
import { AllLanguageCodes, useLocale } from "@/Components/IntlProvider/IntlProviderUtils";
import { AllLanguageCodes } from "@/Components/IntlProvider/IntlProviderUtils";
import {useLocale} from "@/Components/IntlProvider/useLocale";
import useLogin from "@/Hooks/useLogin";
import { unwrap } from "@/Utils";
import { DefaultImgProxy } from "@/Utils/Const";

View File

@ -160,3 +160,8 @@ export const MaxUsernameLength = 100;
* Max about length - profile/settings
*/
export const MaxAboutLength = 1000;
/*
* Snort backend publishes rates
*/
export const SnortPubkey = "npub1sn0rtcjcf543gj4wsg7fa59s700d5ztys5ctj0g69g2x6802npjqhjjtws";