Skip to content

Commit

Permalink
Merge pull request #1 from invm/results-table
Browse files Browse the repository at this point in the history
feat: results table
  • Loading branch information
invm authored Aug 8, 2023
2 parents 2197691 + 1ddb5cc commit 690798d
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 140 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@codemirror/state": "^6.2.1",
"@codemirror/view": "^6.14.1",
"@tauri-apps/api": "^1.4.0",
"@types/tabulator-tables": "^5.4.10",
"@uiw/codemirror-theme-dracula": "^4.21.7",
"fuse.js": "^6.6.2",
"i18next": "^23.1.0",
Expand All @@ -27,6 +28,7 @@
"solid-transition-group": "^0.2.2",
"split.js": "^1.6.5",
"sql-formatter": "^12.2.3",
"tabulator-tables": "^5.5.1",
"tauri-plugin-store-api": "github:tauri-apps/tauri-plugin-store#v1",
"tauri-plugin-window-state-api": "github:tauri-apps/tauri-plugin-window-state#v1",
"tinykeys": "^2.1.0",
Expand Down
26 changes: 20 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src-tauri/src/handlers/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use tauri::{command, AppHandle};
#[command]
pub async fn execute_query(app_handle: AppHandle, conn_id: String, mut query: String) -> CommandResult<Value> {
let connection = app_handle.acquire_connection(conn_id);
if !query.to_lowercase().ends_with("limit 100") {
query.push_str(" limit 100");
if !query.to_lowercase().ends_with("limit 1000") {
query.push_str(" limit 1000");
}
let result = connection.execute_query(query).await?;
Ok(result)
Expand Down
7 changes: 3 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CloseIcon } from 'components/UI/Icons';
import { ThemeSwitch } from 'components/UI/ThemeSwitch';
import { Console } from 'components/Screens/Console/Console';
import { Home } from 'components/Screens/Home/Home';
import "tabulator-tables/dist/css/tabulator_midnight.min.css";

function App() {
const { connectionsService: { removeTab, clearStore, connectionStore, setConnectionStore } } = useAppSelector()
Expand All @@ -19,10 +20,8 @@ function App() {
<div class="px-2 pb-2 bg-base-300 tabs tabs-boxed rounded-none gap-2 flex justify-between items-center">
<div class="flex items-center">
<div class="flex items-center">
<div onClick={() => {
console.log('click')
setConnectionStore('idx', 0)
}} class="tab tab-md"
<div onClick={() => { setConnectionStore('idx', 0) }}
class="tab tab-md"
classList={{ 'tab-active': connectionStore.idx === 0, }} >
<span class="text-md font-bold">Home</span>
</div>
Expand Down
49 changes: 32 additions & 17 deletions src/components/Screens/Console/Content/QueryTab/QueryTab.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
import { createEffect, onCleanup } from "solid-js"
import Split from "split.js"
import { QueryTextArea } from "./QueryTextArea"
import { ResultsArea } from "./ResultsArea"
import { Table } from "components/UI";
import { ContentTabData } from "services/ConnectionTabs";
import { useAppSelector } from "services/Context";
import { createEffect, createSignal, onCleanup, Show } from "solid-js";
import Split from "split.js";
import { QueryTextArea } from "./QueryTextArea";

export const QueryTab = () => {
const {
connectionsService: { getActiveContentTab },
} = useAppSelector();
const [data, setData] = createSignal<Record<string, any>[]>([]);

createEffect(() => {
const q = Split(['#query', '#results'], {
const tabledata = (getActiveContentTab().data as ContentTabData["QueryTab"])
.results;
if (!tabledata.length) return;
setData(tabledata);
});
createEffect(() => {
const q = Split(["#query", "#results"], {
sizes: [40, 60],
minSize: [100, 400],
direction: 'vertical',
maxSize: [500, Infinity],
direction: "vertical",
gutterSize: 8,
})
});
onCleanup(() => {
q.destroy()
})
})
q.destroy();
});
});

return (
<div class="flex flex-col h-full">
<div id="query" class="flex max-h-[40%]">
<div class="flex flex-col h-full overflow-hidden">
<div id="query" class="flex">
<QueryTextArea />
</div>
<div id="results" class="max-h-[60%] h-full bg-base-200 p-3 overflow-hidden">
<ResultsArea />
<div id="results" class="overflow-y-scroll">
<Show when={data().length}>
<Table data={data()} />
</Show>
</div>
</div >
)
}
</div>
);
};
103 changes: 68 additions & 35 deletions src/components/Screens/Console/Content/QueryTab/QueryTextArea.tsx
Original file line number Diff line number Diff line change
@@ -1,69 +1,102 @@
import { createCodeMirror, createEditorControlledValue } from "solid-codemirror";
import {
createCodeMirror,
createEditorControlledValue,
} from "solid-codemirror";
import { createEffect, createSignal } from "solid-js";
import { lineNumbers } from '@codemirror/view';
import { sql } from '@codemirror/lang-sql'
import { dracula } from '@uiw/codemirror-theme-dracula'
import { format } from 'sql-formatter';
import { lineNumbers } from "@codemirror/view";
import { sql } from "@codemirror/lang-sql";
import { dracula } from "@uiw/codemirror-theme-dracula";
import { format } from "sql-formatter";
import { t } from "i18next";
import { invoke } from '@tauri-apps/api';
import { invoke } from "@tauri-apps/api";
import { EditIcon, FireIcon } from "components/UI/Icons";
import { useAppSelector } from "services/Context";
import { ContentTab, ContentTabData } from "services/ConnectionTabs";
import { QueryResult } from "interfaces";

export const QueryTextArea = () => {
const { connectionsService: { setActiveContentQueryTabData, contentStore,
setContentStore, getActiveConnection, getActiveContentTab } } =
useAppSelector()
const {
connectionsService: {
setActiveContentQueryTabData,
contentStore,
setContentStore,
getActiveConnection,
getActiveContentTab,
},
} = useAppSelector();

const updateQueryText = async (query: string) => {
setContentStore('tabs', contentStore.tabs.map((t, idx) => idx === contentStore.idx ? {
...t,
data: { ...t.data, query }
} as ContentTab<'QueryTab'> : t))
}
setContentStore(
"tabs",
contentStore.tabs.map((t, idx) =>
idx === contentStore.idx
? ({
...t,
data: { ...t.data, query },
} as ContentTab<"QueryTab">)
: t
)
);
};

const onInput = (q: string) => {
updateQueryText(q)
setCode(q)
}
updateQueryText(q);
setCode(q);
};

const [code, setCode] = createSignal('');
const { ref, editorView, createExtension } = createCodeMirror({ onValueChange: onInput });
const [code, setCode] = createSignal("");
const { ref, editorView, createExtension } = createCodeMirror({
onValueChange: onInput,
});
createEditorControlledValue(editorView, code);
createExtension(() => lineNumbers());
createExtension(() => sql());
createExtension(dracula);

const onFormat = () => {
const formatted = format(code())
onInput(formatted)
}
const formatted = format(code());
onInput(formatted);
};

const onExecute = async () => {
const activeConnection = getActiveConnection()
const { result } = await invoke<QueryResult>('execute_query', { connId: activeConnection.id, query: code() })
console.log(result)
setActiveContentQueryTabData({ query: code(), results: result })
}
const activeConnection = getActiveConnection();
const { result } = await invoke<QueryResult>("execute_query", {
connId: activeConnection.id,
query: code(),
});
console.log(result);
setActiveContentQueryTabData({ query: code(), results: result });
};

createEffect(() => {
setCode((getActiveContentTab()?.data as ContentTabData['QueryTab']).query ?? '')
})
setCode(
(getActiveContentTab()?.data as ContentTabData["QueryTab"]).query ?? ""
);
});

return (
<div class="flex-1 flex flex-col">
<div class="w-full p-1 bg-base-100">
<div class="tooltip tooltip-primary" data-tip={t('components.console.actions.format')}>
<button class="btn btn-ghost btn-sm mr-2" onClick={() => onFormat()}><EditIcon /></button>
<div
class="tooltip tooltip-primary tooltip-bottom"
data-tip={t("components.console.actions.format")}
>
<button class="btn btn-ghost btn-sm mr-2" onClick={() => onFormat()}>
<EditIcon />
</button>
</div>
<div class="tooltip tooltip-primary" data-tip={t('components.console.actions.execute')}>
<button class="btn btn-ghost btn-sm mr-2" onClick={() => onExecute()}><FireIcon /></button>
<div
class="tooltip tooltip-primary tooltip-bottom"
data-tip={t("components.console.actions.execute")}
>
<button class="btn btn-ghost btn-sm mr-2" onClick={() => onExecute()}>
<FireIcon />
</button>
</div>
</div>
<div class="overflow-hidden w-full h-full">
<div ref={ref} class="w-full h-full" />
</div>
</div>
)
}
);
};
25 changes: 0 additions & 25 deletions src/components/Screens/Console/Content/QueryTab/ResultsArea.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/Screens/Console/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const Sidebar = () => {
</For>
</select>
</div>
<div class="text-xs font-bold text-primary">Tables</div>
<div class="text-sm font-bold py-1 text-primary">Tables</div>
<For each={tables}>
{(table) => (
<div class="rounded-sm mb-1 px-2 min-w-full w-fit">
Expand Down
3 changes: 1 addition & 2 deletions src/components/UI/Collapse.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { createSignal, JSXElement } from "solid-js"
import { ChevronDown, ChevronRight } from "./Icons"

export const Collapse = (props: { title: string, children: JSXElement }) => {
const [open, setOpen] = createSignal(false)
return (
<div class="w-full">
<div onClick={() => setOpen(!open())} class="collapse flex items-center rounded-sm text-sm font-semibold pointer border-b-2 border-base-300">
<div onClick={() => setOpen(!open())} class="collapse flex items-center rounded-sm text-sm font-semibold cursor-pointer border-b-2 border-base-300">
<label class={`swap text-6xl ${open() ? 'swap-active' : ''}`}>
<svg class="w-2 h-2 swap-off" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 6 10">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 9 4-4-4-4" />
Expand Down
Loading

0 comments on commit 690798d

Please sign in to comment.