Skip to content

Commit

Permalink
wip use browser router
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Mar 4, 2024
1 parent 6c73eb4 commit d60c3fb
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 29 deletions.
16 changes: 16 additions & 0 deletions src/configuration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,25 @@ export function updateConnectionState<K extends keyof ConnectionAppState>(
writeConfiguration(config);
}

function getConnectionFromName(name: string): ConnectionObject {
console.log(name);
const config = getConfiguration();

console.log(config);

if (!config.connections || !config.connections[name]) {
throw new Error(`Connection ${name} not found`);
}

const { appState: _, ...rest } = config.connections[name];

return rest;
}

const IPC_EVENT_BINDING = {
[CONFIGURATION_CHANNEL.GET]: getConfiguration,
[CONFIGURATION_CHANNEL.ADD_CONNECTION]: addConnectionToConfig,
[CONFIGURATION_CHANNEL.GET_CONNECTION_FROM_NAME]: getConnectionFromName,
[CONFIGURATION_CHANNEL.EDIT_CONNECTION]: editConnection,
[CONFIGURATION_CHANNEL.CHANGE_THEME]: changeTheme,
[CONFIGURATION_CHANNEL.UPDATE_CONNECTION_STATE]: updateConnectionState,
Expand Down
2 changes: 2 additions & 0 deletions src/contexts/ConfigurationContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type ConfigurationContextType = {
key: K,
value: ConnectionAppState[K]
) => void;
getConnectionFromName: (name: string) => Promise<ConnectionObject>;
};

const ConfigurationContext = createContext<null | ConfigurationContextType>(
Expand Down Expand Up @@ -55,6 +56,7 @@ export function ConfigurationContextProvider({
),
updateConnectionState: window.config.updateConnectionState,
editConnection: willChangeConfiguration(window.config.editConnection),
getConnectionFromName: window.config.getConnectionFromName,
}),
[configuration]
);
Expand Down
5 changes: 5 additions & 0 deletions src/preload/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ interface Config {
value: ConnectionAppState[K]
): Promise<void>;

getConnectionFromName(name: string): Promise<ConnectionObject>;

editConnection(
connectionName: string,
connection: ConnectionObject
Expand All @@ -29,5 +31,8 @@ export const config: Config = {
updateConnectionState: bindChannel(
CONFIGURATION_CHANNEL.UPDATE_CONNECTION_STATE
),
getConnectionFromName: bindChannel(
CONFIGURATION_CHANNEL.GET_CONNECTION_FROM_NAME
),
editConnection: bindChannel(CONFIGURATION_CHANNEL.EDIT_CONNECTION),
};
1 change: 1 addition & 0 deletions src/preload/configurationChannel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export enum CONFIGURATION_CHANNEL {
GET = 'config:get',
ADD_CONNECTION = 'config:connection:add',
GET_CONNECTION_FROM_NAME = 'config:connection:getFromName',
EDIT_CONNECTION = 'config:connection:edit',
CHANGE_THEME = 'config:theme:change',
UPDATE_CONNECTION_STATE = 'config:connection:updateState',
Expand Down
28 changes: 14 additions & 14 deletions src/renderer/app.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import { RouterProvider, createMemoryRouter } from 'react-router';
import { RouterProvider, createBrowserRouter } from 'react-router-dom';
import invariant from 'tiny-invariant';
import ErrorPage from './error-page';
import Connect from './routes/connect';
Expand All @@ -11,7 +11,7 @@ import TableName, {
} from './routes/connections.$connectionName.$databaseName.$tableName';
import { Home } from './routes/home';
import Root from './routes/root';
import { Tables } from './routes/tables';
import Tables from './routes/tables';

const appElement = document.getElementById('App');

Expand All @@ -24,7 +24,7 @@ const root = createRoot(appElement);
// export const history = createMemoryHistory();
// <Router history={history}>

const router = createMemoryRouter([
const router = createBrowserRouter([
{
path: '/',
element: <Root />,
Expand Down Expand Up @@ -55,17 +55,17 @@ const router = createMemoryRouter([
path: 'connections/:connectionName',
element: <Tables />,
},
{
path: 'connections/:connectionName/:databaseName',
element: <Tables />,
children: [
{
path: ':tableName',
loader: tableNameLoader,
element: <TableName />,
},
],
},
// {
// path: 'connections/:connectionName/:databaseName',
// element: <Tables />,
// children: [
// {
// path: ':tableName',
// loader: tableNameLoader,
// element: <TableName />,
// },
// ],
// },
],
},
]);
Expand Down
16 changes: 8 additions & 8 deletions src/renderer/component/Connection/ConnectionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ function ConnectionPage() {
<Flex key={connection.name} gap="small">
<Button block>
<Link
to={connection.name}
onClick={(e) => {
// TODO move this in the page component
e.preventDefault();
to={`/connections/${connection.name}/`}
// onClick={(e) => {
// // TODO move this in the page component
// e.preventDefault();

// remove unwanted properties
const { appState: _, ...rest } = connection;
// // remove unwanted properties
// const { appState: _, ...rest } = connection;

connectTo(rest);
}}
// connectTo(rest);
// }}
>
{connection.name}
</Link>
Expand Down
19 changes: 14 additions & 5 deletions src/renderer/component/Connection/ConnectionStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,22 @@ function ConnectionStack({ children }: Props) {
}, []);

const handleConnectTo = useCallback(
async (params: ConnectionObject) => {
await window.sql.openConnection(params);
setConnectionNameList((prev) => [...prev, params.name]);
async (params: ConnectionObject | string) => {
const connection =
typeof params === 'string'
? await window.config.getConnectionFromName(params)
: params;

navigate(`/connections/${params.name}`);
if (connectionNameList.includes(connection.name)) {
return;
}

await window.sql.openConnection(connection);
setConnectionNameList((prev) => [...prev, connection.name]);

// navigate(`/connections/${connection.name}`);
},
[navigate, setConnectionNameList]
[connectionNameList]
);

const handleSetDatabase = useCallback(
Expand Down
45 changes: 43 additions & 2 deletions src/renderer/routes/tables.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { useCallback, useEffect } from 'react';
import { Layout } from 'antd';
import { Outlet } from 'react-router';
import { Outlet, Params, useParams } from 'react-router';

Check failure on line 3 in src/renderer/routes/tables.tsx

View workflow job for this annotation

GitHub Actions / test (18.x)

'Params' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 3 in src/renderer/routes/tables.tsx

View workflow job for this annotation

GitHub Actions / test (18.x)

'Params' is defined but never used. Allowed unused vars must match /^_/u
import { styled } from 'styled-components';
import invariant from 'tiny-invariant';

Check failure on line 5 in src/renderer/routes/tables.tsx

View workflow job for this annotation

GitHub Actions / test (18.x)

'invariant' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 5 in src/renderer/routes/tables.tsx

View workflow job for this annotation

GitHub Actions / test (18.x)

'invariant' is defined but never used. Allowed unused vars must match /^_/u
import { useConfiguration } from '../../contexts/ConfigurationContext';
import { useConnectionContext } from '../../contexts/ConnectionContext';
import DatabaseSelector from '../component/DatabaseSelector';
import TableList from '../component/TableList';
import { getSetting } from '../theme';
Expand All @@ -14,7 +18,44 @@ const PaddedDiv = styled.div`
padding: 10px;
`;

export function Tables() {
export default function Tables() {
// // TODO move this in the page component
// e.preventDefault();

// // remove unwanted properties
// const { appState: _, ...rest } = connection;

// connectTo(rest);
// }}

const { connectionName } = useParams();
const { connectTo } = useConnectionContext();
const { getConnectionFromName } = useConfiguration();

useEffect(() => {
console.log({ connectionName });
}, [connectionName]);

useEffect(() => {
console.log({ getConnectionFromName });
}, [getConnectionFromName]);

useEffect(() => {
console.log({ connectTo });
}, [connectTo]);

const doConnect = useCallback(async () => {
console.log('doConnect', connectionName);
if (connectionName) {
const connection = await getConnectionFromName(connectionName);
await connectTo(connection);
}
}, [connectionName]);

useEffect(() => {
doConnect();
}, [doConnect]);

return (
<Layout>
<Sider width={200} style={{ overflow: 'auto' }}>
Expand Down
2 changes: 2 additions & 0 deletions src/sql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class ConnectionStack {
}

async executeQuery(senderId: number, query: string): QueryResult {
console.log(this.connections);

const connection = this.#getConnection(senderId);

return await connection.query(query);
Expand Down

0 comments on commit d60c3fb

Please sign in to comment.