-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ClinicalTrials/feat/inline-table-views
Feat/inline table views
- Loading branch information
Showing
6 changed files
with
243 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { CodeBlock } from './CodeBlock'; | ||
import AutoExecutorTableView from './ExecutionView/AutoExecutorTableView'; | ||
import { useState } from 'react'; | ||
import Icon from './Icon'; | ||
import Tooltip from './kit/Tooltip'; | ||
import { ExecutionResult, } from '~/types'; | ||
|
||
interface Props { | ||
language: string; | ||
value: string; | ||
title: string; | ||
} | ||
|
||
const CodeFlipCard = (props: Props) => { | ||
const { language, value, title } = props; | ||
const [showCode, setShowCode] = useState(false); | ||
const [isHidden, setIsHidden] = useState(false); | ||
const [executionResult, setExecutionResult] = useState<ExecutionResult | undefined>(undefined); | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
return ( | ||
<div className="relative w-full"> | ||
<div className="relative w-screen sm:w-[calc(80vw)] lg:w-[calc(60vw)] 2xl:w-[calc(60vw)] left-[calc(-30vw+50%)]"> | ||
<div className="bg-gray-100 dark:bg-zinc-700 px-4 py-2 rounded-lg"> | ||
<div className="flex mb-2"> | ||
<div className="flex text-center h-auto align-text-center"> | ||
{title} | ||
</div> | ||
<div className="ml-auto"> | ||
{!isHidden && ( | ||
<Tooltip title={showCode ? "Show results" : "Show code"} side="top"> | ||
<button | ||
className="flex justify-center items-center rounded bg-none w-6 h-6 p-1 text-xs text-white bg-bcg-green opacity-90 hover:opacity-100" | ||
onClick={() => setShowCode(!showCode)} | ||
> | ||
{showCode ? ( | ||
<Icon.BsTable className="w-full h-auto" /> | ||
) : ( | ||
<Icon.BsCode className="w-full h-auto" /> | ||
)} | ||
</button> | ||
</Tooltip> | ||
)} | ||
</div> | ||
<div className="ml-2"> | ||
<Tooltip title={isHidden ? "Show" : "Collapse"} side="top"> | ||
<button | ||
className="flex justify-center items-center rounded bg-none w-6 h-6 p-1 text-xs text-white bg-bcg-green opacity-90 hover:opacity-100" | ||
onClick={() => setIsHidden(!isHidden)} | ||
> | ||
{isHidden ? ( | ||
<Icon.BiExpandAlt className="w-full h-auto" /> | ||
) : ( | ||
<Icon.BiCollapseAlt className="w-full h-auto" /> | ||
)} | ||
</button> | ||
</Tooltip> | ||
</div> | ||
</div> | ||
{!isHidden && ( | ||
showCode ? ( | ||
<div className="bg-zinc-900 rounded-lg"> | ||
<CodeBlock | ||
language={language} | ||
value={value} | ||
/> | ||
</div> | ||
) : ( | ||
<AutoExecutorTableView | ||
statement={value} | ||
executionResult={executionResult} | ||
setExecutionResult={setExecutionResult} | ||
isLoading={isLoading} | ||
setIsLoading={setIsLoading} | ||
/> | ||
) | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default CodeFlipCard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
frontend/src/components/ExecutionView/AutoExecutorTableView.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { use, useEffect, useState } from "react"; | ||
import { toast } from "react-hot-toast"; | ||
import { useQueryStore } from "@/store"; | ||
import { ExecutionResult, ResponseObject } from "@/types"; | ||
import { checkStatementIsSelect, getMessageFromExecutionResult } from "@/utils"; | ||
import Icon from "../Icon"; | ||
import DataTableView from "./DataTableView"; | ||
import NotificationView from "./NotificationView"; | ||
|
||
interface Props { | ||
statement: string; | ||
executionResult: ExecutionResult | undefined; | ||
setExecutionResult: (result: ExecutionResult | undefined) => void; | ||
isLoading: boolean; | ||
setIsLoading: (isLoading: boolean) => void; | ||
} | ||
|
||
const AutoExecutorTableView = (props: Props) => { | ||
const { statement, executionResult, setExecutionResult, isLoading, setIsLoading } = props; | ||
const executionMessage = executionResult ? getMessageFromExecutionResult(executionResult) : ""; | ||
|
||
useEffect(() => { | ||
if (!executionResult) { | ||
if (statement !== "" && checkStatementIsSelect(statement)) { | ||
executeStatement(statement); | ||
} | ||
setExecutionResult(undefined); | ||
} | ||
}, [statement]); | ||
|
||
const executeStatement = async (statement: string) => { | ||
if (!statement) { | ||
toast.error("Please enter a statement."); | ||
setIsLoading(false); | ||
setExecutionResult(undefined); | ||
return; | ||
} | ||
|
||
setIsLoading(true); | ||
setExecutionResult(undefined); | ||
try { | ||
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/sql/execute`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
// TODO: Add authorisation header (JWT) | ||
// "Authorization": `Bearer ${API_KEY}`, | ||
}, | ||
body: JSON.stringify({ | ||
statement, | ||
}), | ||
}); | ||
const result = (await response.json()) as ResponseObject<ExecutionResult>; | ||
if (result.data) { | ||
setExecutionResult(result.data); | ||
} else if (!result.data && result.message) { | ||
setExecutionResult({ | ||
rawResult: [], | ||
error: result.message, | ||
}); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
toast.error("Failed to execute statement"); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="dark:text-gray-300 flex flex-col justify-start items-start p-4"> | ||
<div className="w-full flex flex-col justify-start items-start mt-4"> | ||
{isLoading ? ( | ||
<div className="w-full flex flex-col justify-center items-center py-6 pt-10"> | ||
<Icon.BiLoaderAlt className="w-7 h-auto opacity-70 animate-spin" /> | ||
<span className="text-sm font-mono text-gray-500 mt-2"> | ||
Executing query... | ||
</span> | ||
</div> | ||
) : ( | ||
<> | ||
{executionResult ? ( | ||
executionMessage ? ( | ||
<NotificationView message={executionMessage} style={executionResult?.error ? "error" : "info"} /> | ||
) : ( | ||
<DataTableView rawResults={executionResult?.rawResult || []} /> | ||
) | ||
) : ( | ||
<></> | ||
)} | ||
</> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default AutoExecutorTableView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.