Skip to content

Commit

Permalink
feat: button to create model from pretemplated sql
Browse files Browse the repository at this point in the history
  • Loading branch information
benfdking committed May 9, 2024
1 parent fad5437 commit c8931df
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 21 deletions.
1 change: 1 addition & 0 deletions js/packages/quary-extension-bus/src/callBacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type Callbacks = {
chartViewRunQuery: ChartFile
chartViewChangeHandler: ChartFile
chartViewOpenTextEditor: null
chartViewCreateModel: string
}

export const useCallBackFrontEnd = <T extends keyof Callbacks>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const meta: Meta<typeof Card> = {
registerChangeChartFile: { action: 'change chart file' },
onClickRunQuery: { action: 'run query' },
onClickEdit: { action: 'edit' },
onClickCreateModel: { action: 'create model' },
},
}

Expand Down
3 changes: 3 additions & 0 deletions js/packages/quary-extension-ui/src/components/ChartEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface Props {
allAssets: string[]
onClickEdit: () => void
onClickRunQuery: (chartFile: ChartFile) => void
onCLickCreateModel: (sql: string) => void
registerChangeChartFile: (config: ChartFile) => void
}

Expand All @@ -24,13 +25,15 @@ export const ChartEditor: React.FC<Props> = ({
onClickEdit,
registerChangeChartFile,
title,
onCLickCreateModel,
}) => (
<div className="pt-1">
<WrappedMemoizedChartEditorHeader
data={chartFile.source}
allAssets={allAssets}
disabled={chartResults.type === 'loading'}
onClickEdit={onClickEdit}
onClickCreateModel={onCLickCreateModel}
onClickRunQuery={(source) => {
onClickRunQuery({
...chartFile,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Meta, StoryObj } from '@storybook/react'
import * as React from 'react'
import { Card } from './Card'
import { ChartEditorHeader } from './ChartEditorHeader'

Expand Down
87 changes: 75 additions & 12 deletions js/packages/quary-extension-ui/src/components/ChartEditorHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChartFile } from '@quary/proto/quary/service/v1/chart_file'
import * as z from 'zod'
import { PencilSquareIcon, PlayIcon } from '@heroicons/react/20/solid'
import { PencilSquareIcon, PlayIcon, PlusIcon } from '@heroicons/react/20/solid'
import React, { useState } from 'react'
import {
Select,
Expand All @@ -12,6 +12,12 @@ import {
SelectValue,
} from './ui/select'
import { Button } from './ui/button'
import {
TooltipProvider,
Tooltip,
TooltipContent,
TooltipTrigger,
} from './ui/tooltip'
import { Input } from '@/components/ui/input.tsx'

interface Props {
Expand All @@ -24,6 +30,8 @@ interface Props {
onClickRunQuery: (source: ChartFile['source']) => void
// open text editor
onClickEdit: () => void
// create a model from the sql should open file
onClickCreateModel: (sql: string) => void
}

export const ChartEditorHeader: React.FC<Props> = ({
Expand All @@ -32,6 +40,7 @@ export const ChartEditorHeader: React.FC<Props> = ({
disabled,
onChangeSource: onChangeSourceProp,
onClickRunQuery,
onClickCreateModel,
onClickEdit,
}) => {
const [state, changeState] = useState(data)
Expand Down Expand Up @@ -86,18 +95,25 @@ export const ChartEditorHeader: React.FC<Props> = ({
allAssets={allAssets}
disabled={disabled}
values={values}
onChangeSource={changeState}
onChangeSource={onChangeSource}
onClickCreateModel={onClickCreateModel}
/>
<Button
<RunQueryButton
disabled={disabled}
size="icon"
onClick={() => onClickRunQuery(mapFormToChartFileSource(values))}
>
<PlayIcon className="h-4 w-4" />
</Button>
<Button size="icon" onClick={onClickEdit}>
<PencilSquareIcon className="h-4 w-4" />
</Button>
/>
<TooltipProvider>
<Tooltip>
<TooltipTrigger>
<Button size="icon" onClick={onClickEdit} disabled={disabled}>
<PencilSquareIcon className="h-4 w-4" />
</Button>
</TooltipTrigger>
<TooltipContent>
<p>Edit Yaml</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
</div>
)
}
Expand All @@ -107,16 +123,17 @@ interface SubFormProps {
disabled: boolean
onChangeSource: (source: ChartFile['source']) => void
allAssets: string[]
onClickCreateModel: (sql: string) => void
}

const SubForm: React.FC<SubFormProps> = ({
values,
disabled,
onChangeSource,
allAssets,
onClickCreateModel,
}) => {
switch (values.type) {
// TODO Implement other cases
case 'rawSql':
return (
<div className="flex-1">
Expand All @@ -134,7 +151,7 @@ const SubForm: React.FC<SubFormProps> = ({
)
case 'preTemplatedSql':
return (
<div className="flex-1">
<div className="flex flex-1 flex-row items-center gap-1">
<Input
disabled={disabled}
value={values.preTemplatedSql}
Expand All @@ -145,6 +162,10 @@ const SubForm: React.FC<SubFormProps> = ({
})
}}
/>
<CreateModelButton
onClick={() => onClickCreateModel(values.preTemplatedSql)}
disabled={disabled}
/>
</div>
)
case 'reference':
Expand Down Expand Up @@ -245,3 +266,45 @@ const FormSchema = z.union([
])

type FormValues = z.infer<typeof FormSchema>

const CreateModelButton = ({
onClick,
disabled,
}: {
onClick: () => void
disabled: boolean
}) => (
<TooltipProvider>
<Tooltip>
<TooltipTrigger>
<Button size="icon" onClick={onClick} disabled={disabled}>
<PlusIcon className="h-4 w-4" />
</Button>
</TooltipTrigger>
<TooltipContent>
<p>Create Model</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
)

const RunQueryButton = ({
onClick,
disabled,
}: {
onClick: () => void
disabled: boolean
}) => (
<TooltipProvider>
<Tooltip>
<TooltipTrigger>
<Button size="icon" onClick={onClick} disabled={disabled}>
<PlayIcon className="h-4 w-4" />
</Button>
</TooltipTrigger>
<TooltipContent>
<p>Run Query</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
)
24 changes: 15 additions & 9 deletions js/packages/quary-extension-ui/src/views/ChartEditorView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@ interface Props {
}

export const ChartEditorView: React.FC<Props> = ({ chart }) => {
const { chartViewChangeHandler, chartViewRunQuery, chartViewOpenTextEditor } =
useCallBackFrontEnd(
[
'chartViewChangeHandler',
'chartViewRunQuery',
'chartViewOpenTextEditor',
],
vscode.postMessage,
)
const {
chartViewChangeHandler,
chartViewRunQuery,
chartViewOpenTextEditor,
chartViewCreateModel,
} = useCallBackFrontEnd(
[
'chartViewChangeHandler',
'chartViewRunQuery',
'chartViewOpenTextEditor',
'chartViewCreateModel',
],
vscode.postMessage,
)
const [chartFile, setChartFile] = React.useState(chart.chartFile)

return (
<ChartEditor
onCLickCreateModel={chartViewCreateModel}
title={chart.title}
chartResults={chart.results}
chartFile={
Expand Down
10 changes: 10 additions & 0 deletions js/packages/quary-extension/src/web/chartCustomEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,16 @@ export class ChartEditorProvider
'default',
)
}
case 'chartViewCreateModel': {
const sql = e.payload as string
const doc = await vscode.workspace.openTextDocument({
language: 'sql',
content: sql,
})
return vscode.window.showTextDocument(doc, {
preview: true,
})
}
default: {
throw new Error(`Error message, received message of type ${e.type}`)
}
Expand Down

0 comments on commit c8931df

Please sign in to comment.