Skip to content

Commit

Permalink
Mint discoverability
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Mar 8, 2024
1 parent 8ec799d commit d3f84f9
Showing 1 changed file with 142 additions and 42 deletions.
184 changes: 142 additions & 42 deletions src/routes/settings/ManageFederations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
setValue,
SubmitHandler
} from "@modular-forms/solid";
import { FederationBalance } from "@mutinywallet/mutiny-wasm";
import { FederationBalance, TagItem } from "@mutinywallet/mutiny-wasm";
import { useSearchParams } from "@solidjs/router";
import {
createResource,
Expand All @@ -29,6 +29,7 @@ import {
FancyCard,
InfoBox,
KeyValue,
LabelCircle,
LargeHeader,
MiniStringShower,
MutinyWalletGuard,
Expand All @@ -54,6 +55,22 @@ export type MutinyFederationIdentity = {
invite_code: string;
};

export type Metadata = {
name: string;
picture?: string;
about?: string;
};

export type DiscoveredFederation = {
id: string;
invite_codes: string[];
pubkey: string;
created_at: number;
event_id: string;
metadata: Metadata | undefined;
recommendations: TagItem[]; // fixme, not the best type to use here
};

type RefetchType = (
info?: unknown
) =>
Expand Down Expand Up @@ -88,12 +105,27 @@ function AddFederationForm(props: { refetch?: RefetchType }) {
const handleSubmit: SubmitHandler<FederationForm> = async (
f: FederationForm
) => {
const federation_code = f.federation_code.trim();
await onSelect(federation_code);
};

const [federations] = createResource(async () => {
try {
const federations: DiscoveredFederation[] =
await state.mutiny_wallet?.discover_federations();

Check failure on line 115 in src/routes/settings/ManageFederations.tsx

View workflow job for this annotation

GitHub Actions / Build APK

Property 'discover_federations' does not exist on type 'MutinyWallet'. Did you mean 'list_federations'?

Check failure on line 115 in src/routes/settings/ManageFederations.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Property 'discover_federations' does not exist on type 'MutinyWallet'. Did you mean 'list_federations'?

Check failure on line 115 in src/routes/settings/ManageFederations.tsx

View workflow job for this annotation

GitHub Actions / Build iOS

Property 'discover_federations' does not exist on type 'MutinyWallet'. Did you mean 'list_federations'?
return federations;
} catch (e) {
console.error(e);
return [];
}
});

const onSelect = async (inviteCode: string) => {
setSuccess("");
setError(undefined);
try {
const federation_code = f.federation_code.trim();
const newFederation =
await state.mutiny_wallet?.new_federation(federation_code);
await state.mutiny_wallet?.new_federation(inviteCode);
console.log("New federation added:", newFederation);
setSuccess(
i18n.t("settings.manage_federations.federation_added_success")
Expand All @@ -110,47 +142,115 @@ function AddFederationForm(props: { refetch?: RefetchType }) {
};

return (
<Form onSubmit={handleSubmit}>
<VStack>
<Field
name="federation_code"
validate={[
required(
i18n.t(
"settings.manage_federations.federation_code_required"
<>
<Form onSubmit={handleSubmit}>
<VStack>
<Field
name="federation_code"
validate={[
required(
i18n.t(
"settings.manage_federations.federation_code_required"
)
)
)
]}
>
{(field, props) => (
<TextField
{...props}
{...field}
error={field.error}
label={i18n.t(
"settings.manage_federations.federation_code_label"
)}
placeholder="fed11..."
required
/>
]}
>
{(field, props) => (
<TextField
{...props}
{...field}
error={field.error}
label={i18n.t(
"settings.manage_federations.federation_code_label"
)}
placeholder="fed11..."
required
/>
)}
</Field>
<Button
loading={feedbackForm.submitting}
disabled={feedbackForm.invalid}
intent="blue"
type="submit"
>
{i18n.t("settings.manage_federations.add")}
</Button>
<Show when={error()}>
<InfoBox accent="red">{error()?.message}</InfoBox>
</Show>
<Show when={success()}>
<InfoBox accent="green">{success()}</InfoBox>
</Show>
</VStack>
</Form>
<pre>Discover a federation on Nostr!</pre>
<Suspense>
<For each={federations()}>
{(fed) => (
<FancyCard>
<div class="flex items-center gap-2 md:gap-4">
<LabelCircle
name={fed.metadata?.name}
image_url={fed.metadata?.picture}
contact={false}
label={false}
/>
<div>
<header class={`font-semibold`}>
{fed.metadata?.name}
</header>
<Show when={fed.metadata?.about}>
<p>{fed.metadata?.about}</p>
</Show>
</div>
</div>
<KeyValue
key={i18n.t(
"settings.manage_federations.federation_id"
)}
>
<MiniStringShower text={fed.id} />
</KeyValue>
<KeyValue key="created at">
{/* todo i18n */}
<time>{timeAgo(fed.created_at)}</time>
</KeyValue>
<KeyValue key={"codes"}>
{/* todo i18n, handle singular vs plural */}
<For each={fed.invite_codes}>
{(invite) => (
<MiniStringShower text={invite} />
)}
</For>
</KeyValue>
<Show when={fed.recommendations.length > 0}>
<KeyValue key={"recommended by"}>
{/* todo i18n */}
<For each={fed.recommendations}>
{(contact) => (
<LabelCircle
name={contact.name}
image_url={contact.image_url}
contact={true}
label={false}
/>
)}
</For>
</KeyValue>
</Show>
{/* FIXME: do something smarter than just taking first code */}
<Button
intent="blue"
onClick={() => onSelect(fed.invite_codes[0])}
>
{i18n.t("settings.manage_federations.add")}
</Button>
</FancyCard>
)}
</Field>
<Button
loading={feedbackForm.submitting}
disabled={feedbackForm.invalid}
intent="blue"
type="submit"
>
{i18n.t("settings.manage_federations.add")}
</Button>
<Show when={error()}>
<InfoBox accent="red">{error()?.message}</InfoBox>
</Show>
<Show when={success()}>
<InfoBox accent="green">{success()}</InfoBox>
</Show>
</VStack>
</Form>
</For>
</Suspense>
</>
);
}

Expand Down

0 comments on commit d3f84f9

Please sign in to comment.