mirror of
https://github.com/chartdb/chartdb.git
synced 2025-11-14 19:05:53 +00:00
create tables
This commit is contained in:
62
src/context/chartdb-context/chartdb-provider.tsx
Normal file
62
src/context/chartdb-context/chartdb-provider.tsx
Normal file
@@ -0,0 +1,62 @@
|
||||
import React from 'react';
|
||||
import { DBTable } from '@/lib/domain/db-table';
|
||||
import { randomHSLA } from '@/lib/utils';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { chartDBContext } from './chartdb-context';
|
||||
|
||||
export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({
|
||||
children,
|
||||
}) => {
|
||||
const [tables, setTables] = React.useState<DBTable[]>([]);
|
||||
|
||||
const addTable = (table: DBTable) => {
|
||||
setTables((tables) => [...tables, table]);
|
||||
};
|
||||
|
||||
const createTable = () => {
|
||||
addTable({
|
||||
id: nanoid(),
|
||||
name: `table_${tables.length + 1}`,
|
||||
x: 0,
|
||||
y: 0,
|
||||
fields: [
|
||||
{
|
||||
id: nanoid(),
|
||||
name: 'id',
|
||||
type: 'bigint',
|
||||
unique: true,
|
||||
nullable: false,
|
||||
primaryKey: true,
|
||||
createdAt: Date.now(),
|
||||
},
|
||||
],
|
||||
indexes: [],
|
||||
color: randomHSLA(),
|
||||
createdAt: Date.now(),
|
||||
});
|
||||
};
|
||||
|
||||
const removeTable = (id: string) => {
|
||||
setTables((tables) => tables.filter((table) => table.id !== id));
|
||||
};
|
||||
|
||||
const updateTable = (id: string, table: Partial<DBTable>) => {
|
||||
setTables((tables) =>
|
||||
tables.map((t) => (t.id === id ? { ...t, ...table } : t))
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<chartDBContext.Provider
|
||||
value={{
|
||||
createTable,
|
||||
addTable,
|
||||
removeTable,
|
||||
updateTable,
|
||||
tables,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</chartDBContext.Provider>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user