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

Editable table data #295

Merged
merged 25 commits into from
Jul 18, 2024
Merged

Editable table data #295

merged 25 commits into from
Jul 18, 2024

Conversation

asutula
Copy link
Contributor

@asutula asutula commented Jul 3, 2024

Title says it all. This also leverages and displays new data related to table owner and ACL.

Copy link

vercel bot commented Jul 3, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 18, 2024 5:52pm

@asutula asutula force-pushed the asutula/editable-table-data branch from 59fd016 to db68e44 Compare July 9, 2024 15:51
@asutula asutula force-pushed the asutula/editable-table-data branch from 150c596 to 76c628d Compare July 11, 2024 17:57

const table = useReactTable({
data,
data: tab.results,
columns: tab.columns,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The react table lib needs the data as some sort of reactive state (prop, state, memo, etc). If you give it another object derived from some reactive state, like we were, you get that freeze up. I'm not sure what's it's doing internally that results in this behavior, but I was able to verify it using a simplified version of the console code I wrote and then made sure that fix worked here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't a problem previously because previous to my branch here, the data was passed as a prop to the DataTable component which internally passed that data prop to useReactTable. We now must call useReactTable outside DataTable and pass the resulting table into DataTable as a prop.

Comment on lines +7 to +19
usersForAddresses: publicProcedure
.input(z.object({ addresses: z.array(z.string().trim()).min(1) }))
.query(async ({ input }) => {
const users = await store.users.usersForAddresses(input.addresses);
const res = users.reduce<Map<string, (typeof users)[number]>>(
(acc, item) => {
acc.set(item.user.address, item);
return acc;
},
new Map<string, (typeof users)[number]>(),
);
return res;
}),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this new users router with this function that, given a list of addresses, returns a map of Studio accounts (keyed by address). If an address in the input doesn't correspond to a Studio user, there will be no entry in the map for it. You'll see the store package implementaiton as well.

Comment on lines +46 to +49
registryRecord = await cache(getRegistryRecord)(
deployment.chainId,
deployment.tableId,
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a card to show the table owner to the table page. This required a new validator query that reads information about the registry. This data is also used deeper into the view hierarchy for ACL related stuff.

export function LatestTables({
initialData,
}: {
initialData: RegistryRecord[];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just updated the Table type name to RegistryRecord to better represent what it is and avoid using our already-overloaded word "table".

Comment on lines +27 to +30
const session = await cache(getSession)({
headers: headers(),
cookies: cookies(),
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated update, just noticed we should be caching this call so the results are reused during a single server side rendering pass.

@@ -80,6 +84,7 @@ export default async function TablePage({
schema={schema}
tableName={params.name}
tableId={tableId}
owner={registryRecord.controller}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just querying and passing the new prop for the table owner here in the page for a Tableland table out of the context of a project.

Comment on lines +236 to +263
export async function getTablePermissionsForAddress(
chainId: number,
tableId: string,
address: string,
) {
const baseUrl = baseUrlForChain(chainId);
const validator = new Validator({ baseUrl });
const [res] = await validator.queryByStatement<{
chain_id: number;
controller: string;
created_at: number;
privileges: number;
table_id: number;
updated_at: number | null;
}>({
statement: `select * from system_acl
where chain_id = ${chainId}
and table_id = ${tableId}
and controller = '${address}'
limit 1`,
});
const permission: TablePermission = {
insert: (res.privileges & 1) > 0,
update: (res.privileges & 2) > 0,
delete: (res.privileges & 4) > 0,
};
return permission;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New function to get the ACL for a single address and table. I don't think we actually use this in the end, but left it anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved these here for shared use.

@@ -51,6 +51,7 @@
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"cmdk": "^0.2.0",
"deep-object-diff": "^1.1.9",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the package I use for JSON object diffing.

"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"include": [
"next-env.d.ts",
"types.d.ts",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to add this line to pick up our type extensions for tanstack table (file below).

Comment on lines +5 to +26
declare module "@tanstack/table-core" {
interface TableMeta<TData extends RowData> {
getRowClassName: (row: Row<TData>) => string;
pkName?: string;
accountPermissions?: ACLItem;
pendingTxn?: boolean;
editRow: (row: Row<TData>) => void;
updateRowColumn: (
row: Row<TData>,
columnName: string,
value: string | number,
) => void;
addRow: () => void;
deleteRow: (row: Row<TData>) => void;
revertRow: (row: Row<TData>) => void;
revertAll: () => void;
}
interface ColumnMeta {
columnName: string;
type: "number" | "string";
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how tanstack table allows you to add custom metadata types to your table and column metadata when you use typescript.

@asutula asutula requested a review from joewagner July 15, 2024 22:07
asutula added 5 commits July 18, 2024 11:00
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
asutula added 12 commits July 18, 2024 11:02
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
@asutula asutula force-pushed the asutula/editable-table-data branch from 13bb75d to 7ee03a7 Compare July 18, 2024 17:04
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
Signed-off-by: Aaron Sutula <asutula@users.noreply.github.com>
@asutula asutula merged commit 5d501ab into main Jul 18, 2024
4 checks passed
@asutula asutula deleted the asutula/editable-table-data branch July 18, 2024 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants