Skip to content

Commit

Permalink
Feature/claim display size (#17)
Browse files Browse the repository at this point in the history
* add support for claim display size in hyperboard rendering

* add ui support and rls for updating display size
  • Loading branch information
Jipperism authored Nov 22, 2023
1 parent a810cea commit 2128fb6
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 123 deletions.
33 changes: 20 additions & 13 deletions components/admin/create-registry-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,27 @@ export const CreateRegistryModal = ({

try {
const claimInserts: ClaimInsert[] = await Promise.all(
claims.map(async ({ hypercert_id, claim_id = crypto.randomUUID() }) => {
const claim = await client.indexer.claimById(hypercert_id);
if (!claim.claim) {
throw new Error("Claim not found");
}
return {
id: claim_id,
registry_id: insertedRegistry.id,
claims.map(
async ({
hypercert_id,
chain_id: chainId,
admin_id: address,
owner_id: claim.claim.owner,
};
}),
claim_id = crypto.randomUUID(),
display_size,
}) => {
const claim = await client.indexer.claimById(hypercert_id);
if (!claim.claim) {
throw new Error("Claim not found");
}
return {
id: claim_id,
registry_id: insertedRegistry.id,
display_size: Number(display_size.toString()),
hypercert_id,
chain_id: chainId,
admin_id: address,
owner_id: claim.claim.owner,
};
},
),
);
await createClaims({
claims: claimInserts,
Expand Down
12 changes: 9 additions & 3 deletions components/admin/registries-admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ export const RegistriesAdmin = () => {
setSelectedRegistry(undefined);
};

// TODO: A number should be able to be set for each claim <> registry relation, to see how much of the total space of the registry it takes up
// TODO: This space is then divided using the units, and how much everybody owns of the total number of units for a claim.
return (
<Flex direction={"column"} width={"100%"}>
<VStack minHeight={"100%"} spacing={4} alignItems={"flex-start"}>
Expand All @@ -81,6 +79,7 @@ export const RegistriesAdmin = () => {
claims: registry.claims.map((claim) => ({
claim_id: claim.id,
hypercert_id: claim.hypercert_id,
display_size: claim.display_size,
})),
});
createOnOpen();
Expand All @@ -101,6 +100,7 @@ export const RegistriesAdmin = () => {
<Th>Owner</Th>
<Th>External url</Th>
<Th>Description</Th>
<Th>Display size</Th>
</Tr>
</Thead>
<Tbody>
Expand Down Expand Up @@ -174,7 +174,12 @@ export const RegistriesAdmin = () => {
);
};

export const ClaimRow = ({ hypercert_id, chain_id, id }: {} & ClaimEntity) => {
export const ClaimRow = ({
hypercert_id,
chain_id,
id,
display_size,
}: {} & ClaimEntity) => {
const { data, isLoading } = useFetchHypercertById(hypercert_id);

if (isLoading) {
Expand Down Expand Up @@ -220,6 +225,7 @@ export const ClaimRow = ({ hypercert_id, chain_id, id }: {} & ClaimEntity) => {
<Td maxW={"300px"} isTruncated>
{data.metadata.description}
</Td>
<Td>{display_size}</Td>
<Td textAlign={"end"}>
<DeleteClaimButton size="xs" claimId={id} />
</Td>
Expand Down
13 changes: 11 additions & 2 deletions components/forms/create-or-update-registry-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export interface CreateOrUpdateHyperboardFormProps {
const minimumCharacters = 40;

export type CreateUpdateRegistryFormValues = RegistryInsert & {
claims: { claim_id?: string; hypercert_id: string; id?: string }[];
claims: {
claim_id?: string;
hypercert_id: string;
id?: string;
display_size: number;
}[];
};

export const CreateOrUpdateRegistryForm = ({
Expand Down Expand Up @@ -113,6 +118,7 @@ const ClaimsField = ({
{fields.map((item, index) => (
<HStack key={item.id}>
<Input {...register(`claims.${index}.hypercert_id`)} />
<Input {...register(`claims.${index}.display_size`)} />
{item.claim_id ? (
<DeleteClaimButton claimId={item.claim_id} />
) : (
Expand All @@ -126,7 +132,10 @@ const ClaimsField = ({
)}
</HStack>
))}
<Button type="button" onClick={() => append({ hypercert_id: "" })}>
<Button
type="button"
onClick={() => append({ hypercert_id: "", display_size: 1 })}
>
+ add another claim
</Button>
</VStack>
Expand Down
32 changes: 28 additions & 4 deletions hooks/useFetchHyperboardContents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DefaultSponsorMetadataEntity,
RegistryEntity,
} from "@/types/database-entities";
import { sift } from "@/utils/sift";

// interface EntryDisplayData {
// image: string;
Expand Down Expand Up @@ -40,16 +41,26 @@ export const useListRegistries = () => {
const processRegistryForDisplay = async (
registry: RegistryEntity & { claims: ClaimEntity[] },
label: string | null,
totalOfAllDisplaySizes: number,
client: HypercertClient,
) => {
const claims = registry.claims;
const fractionsResults = await Promise.all(
claims.map((claim) => {
return client.indexer.fractionsByClaim(claim.hypercert_id);
claims.map(async (claim) => {
const fractions = await client.indexer.fractionsByClaim(
claim.hypercert_id,
);
const displaySizeCoefficient =
claim.display_size / totalOfAllDisplaySizes;
return fractions.claimTokens.map((fraction) => ({
...fraction,
unitsAdjustedForDisplaySize:
parseInt(fraction.units, 10) * displaySizeCoefficient,
}));
}),
);

const fractions = _.flatMap(fractionsResults, (x) => x.claimTokens);
const fractions = _.flatMap(fractionsResults, (x) => x);
const ownerAddresses = _.uniq(fractions.map((x) => x.owner)) as string[];

const claimDisplayDataResponse = await getEntriesDisplayData(ownerAddresses);
Expand All @@ -64,7 +75,10 @@ const processRegistryForDisplay = async (
return {
fractions: fractionsPerOwner,
displayData: claimDisplayData[owner],
totalValue: _.sum(fractionsPerOwner.map((x) => parseInt(x.units, 10))),
totalValue: _.sumBy(
fractionsPerOwner,
(x) => x.unitsAdjustedForDisplaySize,
),
};
})
.value();
Expand Down Expand Up @@ -117,11 +131,21 @@ export const useFetchHyperboardContents = (hyperboardId: string) => {
return null;
}

const totalOfAllDisplaySizes = _.sumBy(
sift(
hyperboardData.hyperboard_registries
.map((registry) => registry.registries?.claims)
.flat(),
),
(x) => x.display_size,
);

const results = await Promise.all(
hyperboardData.hyperboard_registries.map(async (registry) => {
return await processRegistryForDisplay(
registry.registries!,
registry.label,
totalOfAllDisplaySizes,
client,
);
}),
Expand Down
2 changes: 1 addition & 1 deletion hooks/useFetchMyHyperboards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const useFetchMyHyperboards = () => {
}
return supabase
.from("hyperboards")
.select("*, hyperboard_registries (*, registries (*))")
.select("*, hyperboard_registries!hyperboard_id (*, registries (*))")
.eq("admin_id", address);
},
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@
"@types/react": "18.2.21",
"@types/react-dom": "18.2.7",
"prettier": "^3.0.3",
"snaplet": "^0.72.2"
"snaplet": "0.71"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alter table hyperboard_registries
rename constraint hyperboard_registries_registry_id_fkey to hyperboard_registries_registries_id_fk;

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
create policy "Only allow for owner of registry"
on "public"."claims"
as permissive
for update
to public
using (((auth.jwt() ->> 'address'::text) IN (SELECT hyperboards.admin_id
FROM hyperboards
JOIN public.hyperboard_registries hr on hyperboards.id = hr.hyperboard_id
JOIN public.claims c on hr.registry_id = c.registry_id
WHERE (hr.registry_id = c.registry_id))));

alter table "public"."claims" add column "display_size" bigint not null default '1'::bigint;
3 changes: 3 additions & 0 deletions types/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export interface Database {
admin_id: string
chain_id: number
created_at: string
display_size: number
hypercert_id: string
id: string
owner_id: string
Expand All @@ -88,6 +89,7 @@ export interface Database {
admin_id: string
chain_id: number
created_at?: string
display_size?: number
hypercert_id: string
id?: string
owner_id: string
Expand All @@ -97,6 +99,7 @@ export interface Database {
admin_id?: string
chain_id?: number
created_at?: string
display_size?: number
hypercert_id?: string
id?: string
owner_id?: string
Expand Down
Loading

0 comments on commit 2128fb6

Please sign in to comment.