Skip to content

Commit

Permalink
Merge pull request #99 from dolthub/taylor/fixes-2
Browse files Browse the repository at this point in the history
Misc fixes
  • Loading branch information
tbantle22 authored Jan 10, 2024
2 parents 0067668 + dbd6ed7 commit 5073bb3
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 85 deletions.
3 changes: 3 additions & 0 deletions graphql-server/src/diffStats/diffStat.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export function checkArgs(args: DiffStatArgs): void {
) {
throw new Error("refName is required for TwoDot diff with ref keyword");
}
if (args.refName && isRefKeyword(args.refName)) {
throw new Error("refName cannot be a ref keyword");
}
}

function isRefKeyword(refName: string): boolean {
Expand Down
34 changes: 15 additions & 19 deletions graphql-server/src/queryFactory/dolt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,25 +412,21 @@ export class DoltQueryFactory
async getOneSidedRowDiff(
args: t.TableArgs & { offset: number },
): Promise<{ rows: t.RawRows; columns: t.RawRows }> {
return this.queryMultiple(
async query => {
const columns = await query(qh.tableColsQueryAsOf, [
args.tableName,
args.refName,
]);
const { q, cols } = qh.getRowsQueryAsOf(columns);
const rows = await query(q, [
args.tableName,
args.refName,
...cols,
ROW_LIMIT + 1,
args.offset,
]);
return { rows, columns };
},
args.databaseName,
args.refName,
);
return this.queryMultiple(async query => {
const columns = await query(qh.tableColsQueryAsOf, [
args.tableName,
args.refName,
]);
const { q, cols } = qh.getRowsQueryAsOf(columns);
const rows = await query(q, [
args.tableName,
args.refName,
...cols,
ROW_LIMIT + 1,
args.offset,
]);
return { rows, columns };
}, args.databaseName);
}

async getRowDiffs(args: t.RowDiffArgs): t.DiffRes {
Expand Down
51 changes: 51 additions & 0 deletions web/components/CellButtons/CopyRowButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Button from "@components/Button";
import { serializeCellValue } from "@components/pageComponents/FileUploadPage/Steps/Upload/EditableTable/TableGrid/utils";
import {
ColumnForDataTableFragment,
RowForDataTableFragment,
} from "@gen/graphql-types";
import useDelay from "@hooks/useDelay";
import { getBitDisplayValue } from "@lib/dataTable";
import CopyToClipboard from "react-copy-to-clipboard";
import css from "./index.module.css";

type Props = {
row: RowForDataTableFragment;
columns: ColumnForDataTableFragment[];
disabled?: boolean;
};

export default function CopyRowButton({
row,
columns,
disabled,
}: Props): JSX.Element {
const success = useDelay();
const copyVal = getCopyVal(row, columns);
return (
<CopyToClipboard onCopy={success.start} text={copyVal}>
<Button.Link
className={css.button}
data-cy="copy-row-button"
disabled={disabled}
>
{success.active ? "Copied" : "Copy row"}
</Button.Link>
</CopyToClipboard>
);
}

function getCopyVal(
row: RowForDataTableFragment,
columns: ColumnForDataTableFragment[],
): string {
return row.columnValues
.map((c, cidx) => {
const colType = columns[cidx].type;
const value = c.displayValue;
return colType === "bit(1)"
? getBitDisplayValue(value)
: serializeCellValue(value);
})
.join(",");
}
8 changes: 5 additions & 3 deletions web/components/DataTable/Table/Row.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import CopyRowButton from "@components/CellButtons/CopyRowButton";
import DeleteRowButton from "@components/CellButtons/DeleteRowButton";
import HideRowButton from "@components/CellButtons/HideRowButton";
import CellDropdown from "@components/CellDropdown";
import {
ColumnForDataTableFragment,
RowForDataTableFragment,
} from "@gen/graphql-types";
import { ColumnStatus } from "@lib/tableTypes";
import cx from "classnames";
import { useState } from "react";
import DeleteRowButton from "@components/CellButtons/DeleteRowButton";
import HideRowButton from "@components/CellButtons/HideRowButton";
import CellDropdown from "@components/CellDropdown";
import Cell from "./Cell";
import css from "./index.module.css";
import { getDiffTypeClassnameForRow } from "./utils";
Expand Down Expand Up @@ -41,6 +42,7 @@ export default function Row(props: Props) {
>
<HideRowButton {...props} />
<DeleteRowButton {...props} />
<CopyRowButton {...props} />
</CellDropdown>
)}
</td>
Expand Down
6 changes: 5 additions & 1 deletion web/components/DiffTableNav/DiffTableStats/TableName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Link from "@components/links/Link";
import { useDiffContext } from "@contexts/diff";
import { DiffSummaryFragment, TableDiffType } from "@gen/graphql-types";
import { useRouter } from "next/router";
import css from "./index.module.css";

type TableProps = {
diffSummary: DiffSummaryFragment;
Expand All @@ -28,7 +29,10 @@ function TableLink({ displayedTableName, diffSummary }: TableProps) {

if (stayWithinPage) {
return (
<Button.Link onClick={() => setActiveTableName(diffSummary.tableName)}>
<Button.Link
onClick={() => setActiveTableName(diffSummary.tableName)}
className={css.tableButton}
>
{displayedTableName}
</Button.Link>
);
Expand Down
4 changes: 4 additions & 0 deletions web/components/DiffTableNav/DiffTableStats/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@
.noChanges {
@apply mx-3 mt-4 mb-8 lg:mb-16;
}

.tableButton {
@apply leading-5;
}
13 changes: 11 additions & 2 deletions web/components/SqlDataTable/WorkingDiff.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import ErrorMsg from "@components/ErrorMsg";
import Loader from "@components/Loader";
import NotDoltWrapper from "@components/util/NotDoltWrapper";
import { DiffProvider, useDiffContext } from "@contexts/diff";
import useSqlParser from "@hooks/useSqlParser";
import { RefParams } from "@lib/params";
import css from "./index.module.css";

type Props = {
params: RefParams;
params: RefParams & { q: string };
};

function Inner() {
Expand All @@ -28,9 +29,17 @@ export default function WorkingDiff(props: Props) {
const fromRefName = "HEAD";
const toRefName = "WORKING";
const params = { ...props.params, toRefName, fromRefName };

const { getTableNames } = useSqlParser();
const tns = getTableNames(params.q);

return (
<NotDoltWrapper hideNotDolt>
<DiffProvider params={params} stayWithinPage>
<DiffProvider
params={params}
stayWithinPage
initialTableName={tns ? tns[0] : undefined}
>
<Inner />
</DiffProvider>
</NotDoltWrapper>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useEffect, useState } from "react";
import { TagForListFragment, useTagListQuery } from "@gen/graphql-types";
import useApolloError from "@hooks/useApolloError";
import { handleCaughtApolloError } from "@lib/errors/helpers";
import { ApolloErrorType } from "@lib/errors/types";
import { DatabaseParams } from "@lib/params";
import { useEffect, useState } from "react";

type ReturnType = {
tags: TagForListFragment[] | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Link from "@components/links/Link";
import { useDataTableContext } from "@contexts/dataTable";
import { useSqlEditorContext } from "@contexts/sqleditor";
import { useTagListQuery } from "@gen/graphql-types";
import useDatabaseDetails from "@hooks/useDatabaseDetails";
import useSqlBuilder from "@hooks/useSqlBuilder";
import { TableParams } from "@lib/params";
import { table } from "@lib/urls";
Expand All @@ -20,17 +21,16 @@ type Props = {
params: TableParams;
};

export default function EditTableButtons(props: Props) {
type InnerProps = Props & {
refIsTag?: boolean;
};

function Inner(props: InnerProps) {
const { executeQuery, setEditorString, toggleSqlEditor } =
useSqlEditorContext();
const { dropTable, insertIntoTable } = useSqlBuilder();
const { columns } = useDataTableContext();
const tagRes = useTagListQuery({
variables: props.params,
});
const refIsTag = !!tagRes.data?.tags.list.find(
t => t.tagName === props.params.refName,
);

const uploadParams = { ...props.params, uploadId: String(Date.now()) };

const onWriteQuery = () => {
Expand All @@ -53,26 +53,26 @@ export default function EditTableButtons(props: Props) {
Edit table{" "}
<span className={css.tableName}>{props.params.tableName}</span>
</h2>
{refIsTag && (
{props.refIsTag && (
<ErrorMsg errString="A tag is currently selected. Please change to a branch from the left branch/tag dropdown to edit this database." />
)}
<div className={css.sections}>
<OptionSquare
icon={<AiOutlineCode />}
disabled={refIsTag}
disabled={props.refIsTag}
link={
<Button.Link
onClick={onWriteQuery}
data-cy="sql-query-edit-button"
disabled={refIsTag}
disabled={props.refIsTag}
>
SQL Query
</Button.Link>
}
/>
<OptionSquare
icon={<ImTable2 />}
disabled={refIsTag}
disabled={props.refIsTag}
link={
<DatabaseUploadStageLink
params={{ ...uploadParams, spreadsheet: true }}
Expand All @@ -84,7 +84,7 @@ export default function EditTableButtons(props: Props) {
/>
<OptionSquare
icon={<FiUpload />}
disabled={refIsTag}
disabled={props.refIsTag}
link={
<DatabaseUploadStageLink params={uploadParams} stage="upload">
File Upload
Expand All @@ -95,7 +95,7 @@ export default function EditTableButtons(props: Props) {
<Button.Link
onClick={onDrop}
className={css.drop}
disabled={refIsTag}
disabled={props.refIsTag}
red
>
<AiOutlineDelete /> Drop Table
Expand All @@ -106,3 +106,20 @@ export default function EditTableButtons(props: Props) {
</div>
);
}

function ForDolt(props: Props) {
const tagRes = useTagListQuery({
variables: props.params,
});
const refIsTag = !!tagRes.data?.tags.list.find(
t => t.tagName === props.params.refName,
);
return <Inner {...props} refIsTag={refIsTag} />;
}

export default function EditTableButtons(props: Props) {
const res = useDatabaseDetails();

if (!res.isDolt) return <Inner {...props} />;
return <ForDolt {...props} />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@ function Inner(props: InnerProps) {

const gridElement = (
<DataGrid
columns={[
indexColumn,
...props.state.columns.map(col => {
return {
...col,
// headerRenderer: HeaderRenderer,
};
}),
]}
columns={[indexColumn, ...props.state.columns]}
topSummaryRows={[{ _id: -1 }]}
rows={props.state.rows}
rowKeyGetter={row => row._id}
Expand Down Expand Up @@ -102,30 +94,3 @@ export default function TableGrid(props: Props) {
const { state, setState, gridFunctions: gf } = useGrid(props.columns);
return <Inner {...props} {...{ state, setState, gf }} />;
}

// function HeaderRenderer(props: HeaderRendererProps<Row>) {
// return (
// <ContextMenuTrigger
// id="grid-header-context-menu"
// collect={() => {
// return { column: props.column };
// }}
// >
// <div>{props.column.name}</div>
// </ContextMenuTrigger>
// );
// }

// function RowRenderer(props: RowRendererProps<Row>) {
// return (
// <ContextMenuTrigger
// id="grid-row-context-menu"
// collect={() => {
// return { rowIdx: props.rowIdx };
// }}
// disable={props.rowIdx === 0}
// >
// <GridRow {...props} />
// </ContextMenuTrigger>
// );
// }
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function filterOutEmptyRowsAndCols(rows: string[][]): string[][] {
return filteredEmptyCols;
}

function serializeCellValue(value: string): string {
export function serializeCellValue(value: string): string {
const formattedValue = value.replace(/"/g, '""');
return formattedValue.includes(",") ||
formattedValue.includes('"') ||
Expand Down
23 changes: 14 additions & 9 deletions web/lib/refetchQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,20 @@ export const refetchResetChangesQueries = (
toRefName: "WORKING",
};
return [
...(isDolt ? [{ query: gen.GetStatusDocument, variables }] : []),
{
query: gen.DiffStatDocument,
variables: diffVariables,
},
{
query: gen.DiffSummariesDocument,
variables: diffVariables,
},
...(isDolt
? [
{ query: gen.GetStatusDocument, variables },
{
query: gen.DiffStatDocument,
variables: diffVariables,
},
{
query: gen.DiffSummariesDocument,
variables: diffVariables,
},
]
: []),

{
query: gen.TableNamesDocument,
variables: { ...variables, filterSystemTables: true },
Expand Down

0 comments on commit 5073bb3

Please sign in to comment.