Skip to content

Commit

Permalink
add noUncheckedIndexedAccess, do fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
karooolis committed Oct 25, 2024
1 parent 47b547d commit fdf5a78
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function WorldsForm({ worlds }: { worlds: Address[] }) {
function onLuckyWorld() {
if (worlds.length > 0) {
const luckyAddress = worlds[Math.floor(Math.random() * worlds.length)];
router.push(getWorldUrl(chainName as string, luckyAddress));
router.push(getWorldUrl(chainName as string, luckyAddress as Address));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ export function EditableTableCell({ name, table, keyTuple, value: defaultValue }
const account = useAccount();

const valueSchema = getValueSchema(table);
const fieldType = valueSchema[name as never].type;
const fieldType = valueSchema?.[name as never]?.type;

const { mutate, isPending } = useMutation({
mutationFn: async (newValue: unknown) => {
if (!fieldType) throw new Error("Field type not found");

const fieldIndex = getFieldIndex<ValueSchema>(getSchemaTypes(valueSchema), name);
const encodedFieldValue = encodeField(fieldType, newValue);
const txHash = await writeContract(wagmiConfig, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function TableSelector({ tables }: { tables?: Table[] }) {
const selectedTableConfig = tables?.find(({ tableId }) => tableId === selectedTableId);

useEffect(() => {
if (!selectedTableId && Array.isArray(tables) && tables.length > 0) {
if (!selectedTableId && tables?.[0]) {
setTableId(tables[0].tableId);
}
}, [selectedTableId, setTableId, tables]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ArrowUpDownIcon, LoaderIcon, TriangleAlertIcon } from "lucide-react";
import { parseAsJson, parseAsString, useQueryState } from "nuqs";
import { useMemo } from "react";
import { Schema, Table as TableType } from "@latticexyz/config";
import { getKeySchema, getKeyTuple, getSchemaPrimitives } from "@latticexyz/protocol-parser/internal";
import { Table as TableType } from "@latticexyz/config";
import { getKeySchema, getKeyTuple } from "@latticexyz/protocol-parser/internal";
import {
ColumnDef,
SortingState,
Expand Down Expand Up @@ -37,7 +37,7 @@ export function TablesViewer({ table, query }: { table?: TableType; query?: stri
const [globalFilter, setGlobalFilter] = useQueryState("filter", parseAsString.withDefault(""));
const [sorting, setSorting] = useQueryState("sort", parseAsJson<SortingState>().withDefault(initialSortingState));

const tableColumns: ColumnDef<getSchemaPrimitives<Schema>>[] = useMemo(() => {
const tableColumns: ColumnDef<Record<string, unknown>>[] = useMemo(() => {
if (!table || !tableData) return [];

return tableData.columns.map((name) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ export function useMonacoErrorMarker() {
startColumn: number;
endColumn: number;
}) => {
if (monaco) {
monaco.editor.setModelMarkers(monaco.editor.getModels()[0], "sql", [
const model = monaco?.editor.getModels()[0];
if (model) {
monaco.editor.setModelMarkers(model, "sql", [
{
severity: monaco.MarkerSeverity.Error,
message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ function findErrorPosition(query: string, target: string) {
let currentPosition = 0;

for (let i = 0; i < lines.length; i++) {
if (currentPosition + lines[i].length >= query.indexOf(target)) {
const len = lines[i]?.length;
if (!len) continue;

if (currentPosition + len >= query.indexOf(target)) {
startLineNumber = i + 1;
startColumn = query.indexOf(target) - currentPosition + 1;
break;
}
currentPosition += lines[i].length + 1;
currentPosition += len + 1;
}

return {
Expand Down Expand Up @@ -76,17 +79,21 @@ export function useQueryValidator(table?: Table) {
}
}

monaco.editor.setModelMarkers(monaco.editor.getModels()[0], "sql", []);
const model = monaco?.editor.getModels()[0];
if (model) {
monaco.editor.setModelMarkers(model, "sql", []);
}
return true;
} catch (error) {
if (error instanceof Error) {
const lines = decodedQuery.split("\n");
const lastLine = lines[lines.length - 1];
setErrorMarker({
message: error.message,
startLineNumber: 1,
endLineNumber: lines.length,
startColumn: 1,
endColumn: lines[lines.length - 1].length + 1,
endColumn: lastLine ? lastLine.length + 1 : 1,
});
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function POST(request: Request) {
const result = [];
for (const { query } of queries) {
const data = (await db?.prepare(query).all()) as SqliteTable;
if (!data) {
if (!data || !data[0]) {
throw new Error("No data found");
}

Expand Down
3 changes: 1 addition & 2 deletions packages/explorer/src/app/(explorer)/hooks/useHashState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function useHashState() {
const setHash = (newHash: string) => {
let updatedUrl = window.location.href;
updatedUrl = queryString.stringifyUrl({
url: updatedUrl.split("#")[0],
url: updatedUrl.split("#")[0] ?? "",
fragmentIdentifier: newHash,
});

Expand All @@ -37,7 +37,6 @@ export function useHashState() {

useEffect(() => {
window.addEventListener("hashchange", handleHashChange);

return () => {
window.removeEventListener("hashchange", handleHashChange);
};
Expand Down
23 changes: 13 additions & 10 deletions packages/explorer/src/app/(explorer)/queries/useTableDataQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Props = {

export type TableData = {
columns: string[];
rows: Record<string, string>[];
rows: Record<string, unknown>[];
};

export function useTableDataQuery({ table, query }: Props) {
Expand Down Expand Up @@ -45,22 +45,25 @@ export function useTableDataQuery({ table, query }: Props) {

return data;
},
select: (data: DozerResponse) => {
if (!table || !data?.result?.[0]) return;
select: (data: DozerResponse): TableData | undefined => {
if (!table || !data?.result?.[0]) return undefined;

const schemaKeys = Object.keys(table.schema);
const result = data.result[0];
const columnKeys = result[0]
// if columns are undefined, the result is empty
if (!result[0]) return undefined;

const schema = Object.keys(table.schema);
const columns = result[0]
?.map((columnKey) => {
const schemaKey = schemaKeys.find((schemaKey) => schemaKey.toLowerCase() === columnKey);
const schemaKey = schema.find((schemaKey) => schemaKey.toLowerCase() === columnKey);
return schemaKey || columnKey;
})
.filter((key) => schemaKeys.includes(key));
const rows = result.slice(1)?.map((row) => Object.fromEntries(columnKeys.map((key, index) => [key, row[index]])));
.filter((key) => schema.includes(key));

const rows = result.slice(1).map((row) => Object.fromEntries(columns.map((key, index) => [key, row[index]])));
return {
columns: columnKeys || [],
rows: rows || [],
columns,
rows,
};
},
enabled: !!table && !!query,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useParams } from "next/navigation";
import { Hex } from "viem";
import { isDefined } from "@latticexyz/common/utils";
import { Table } from "@latticexyz/config";
import mudConfig from "@latticexyz/store/mud.config";
import { useQuery } from "@tanstack/react-query";
Expand Down Expand Up @@ -42,6 +43,7 @@ export function useTablesQuery() {
return data.result[0]
.slice(1)
.map((row: string[]) => {
if (!row[0] || !row[2] || !row[3] || !row[4] || !row[5]) return undefined;
return decodeTable({
tableId: row[0],
keySchema: row[2],
Expand All @@ -50,6 +52,7 @@ export function useTablesQuery() {
abiEncodedFieldNames: row[5],
});
})
.filter(isDefined)
.sort(({ namespace }) => (internalNamespaces.includes(namespace) ? 1 : -1));
},
refetchInterval: 5000,
Expand Down
2 changes: 1 addition & 1 deletion packages/explorer/src/components/AccountSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from ".
import { TruncatedHex } from "./ui/TruncatedHex";

function AccountSelectItem({ connector }: { connector: AnvilConnector }) {
const address = connector.accounts[0].address;
const address = connector.accounts[0]?.address;
const { data: balance } = useBalance({
address,
query: {
Expand Down
1 change: 1 addition & 0 deletions packages/explorer/src/observer/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ channel.addEventListener("message", ({ data }: MessageEvent<Message>) => {
if (data.type === "ping") return;
store.setState((state) => {
const write = data.type === "write" ? ({ ...data, events: [] } satisfies Write) : state.writes[data.writeId];
if (!write) return state;
return {
writes: {
...state.writes,
Expand Down
1 change: 1 addition & 0 deletions packages/explorer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"allowJs": true,
"incremental": true,
"isolatedModules": true,
"noUncheckedIndexedAccess": true,
"jsx": "preserve",
"plugins": [
{
Expand Down

0 comments on commit fdf5a78

Please sign in to comment.