mirror of
https://github.com/drawdb-io/drawdb.git
synced 2025-09-21 15:24:51 +00:00
Redo login page
This commit is contained in:
@@ -1,325 +1,12 @@
|
||||
import { useEffect, useState, useRef } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
import logo from "../assets/icon_dark_64.png";
|
||||
import google_logo from "../assets/google.png";
|
||||
import github_logo from "../assets/github.png";
|
||||
import axios from "axios";
|
||||
import { Cardinality } from "../data/data";
|
||||
import { calcPath } from "../utils";
|
||||
import { Toast } from "@douyinfe/semi-ui";
|
||||
|
||||
const xOffset = window.innerWidth * 0.42 * 0.15;
|
||||
const diagram = {
|
||||
tables: [
|
||||
{
|
||||
name: "galactic_users",
|
||||
x: xOffset + 101,
|
||||
y: window.innerHeight * 0.75 - (4 * 36 + 50 + 7) * 0.5,
|
||||
fields: [
|
||||
{
|
||||
name: "id",
|
||||
type: "INT",
|
||||
},
|
||||
{
|
||||
name: "username",
|
||||
type: "VARCHAR",
|
||||
},
|
||||
{
|
||||
name: "email",
|
||||
type: "VARCHAR",
|
||||
},
|
||||
{
|
||||
name: "password",
|
||||
type: "VARCHAR",
|
||||
},
|
||||
],
|
||||
color: "#7d9dff",
|
||||
},
|
||||
{
|
||||
name: "celestial_data",
|
||||
x: xOffset,
|
||||
y: window.innerHeight * 0.32 - (5 * 36 + 50 + 7) * 0.5,
|
||||
fields: [
|
||||
{
|
||||
name: "id",
|
||||
type: "INT",
|
||||
},
|
||||
{
|
||||
name: "user_id",
|
||||
type: "INT",
|
||||
},
|
||||
{
|
||||
name: "type",
|
||||
type: "ENUM",
|
||||
},
|
||||
{
|
||||
name: "time",
|
||||
type: "TIMESTAMP",
|
||||
},
|
||||
{
|
||||
name: "content",
|
||||
type: "VARCHAR",
|
||||
},
|
||||
],
|
||||
color: "#89e667",
|
||||
},
|
||||
],
|
||||
relationships: [
|
||||
{
|
||||
startTableId: 1,
|
||||
startFieldId: 1,
|
||||
endTableId: 0,
|
||||
endFieldId: 0,
|
||||
startX: xOffset + 16,
|
||||
startY:
|
||||
window.innerHeight * 0.32 - (4 * 36 + 50 + 7) * 0.5 + (50 + 18 * 2),
|
||||
endX: xOffset + 115,
|
||||
endY: window.innerHeight * 0.75 - (4 * 36 + 50 + 7) * 0.5 + (50 + 18 * 1),
|
||||
cardinality: "One to one",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
function Table({ table, grab }) {
|
||||
const [isHovered, setIsHovered] = useState(false);
|
||||
const [hoveredField, setHoveredField] = useState(-1);
|
||||
const height = table.fields.length * 36 + 50 + 7;
|
||||
return (
|
||||
<foreignObject
|
||||
key={table.name}
|
||||
x={table.x}
|
||||
y={table.y}
|
||||
width={200}
|
||||
height={height}
|
||||
className="drop-shadow-lg rounded-md cursor-move"
|
||||
onMouseDown={grab}
|
||||
onMouseEnter={() => setIsHovered(true)}
|
||||
onMouseLeave={() => setIsHovered(false)}
|
||||
>
|
||||
<div
|
||||
className={`border-2 ${
|
||||
isHovered ? "border-dashed border-blue-500" : "border-zinc-300"
|
||||
} select-none rounded-lg w-full bg-zinc-100 text-zinc-800`}
|
||||
>
|
||||
<div
|
||||
className={`h-[10px] w-full rounded-t-md`}
|
||||
style={{ backgroundColor: table.color }}
|
||||
/>
|
||||
<div className="font-bold h-[40px] flex justify-between items-center border-b border-zinc-400 bg-zinc-200 px-3">
|
||||
{table.name}
|
||||
</div>
|
||||
{table.fields.map((e, i) => (
|
||||
<div
|
||||
key={i}
|
||||
className={`${
|
||||
i === table.fields.length - 1 ? "" : "border-b border-gray-400"
|
||||
} h-[36px] px-2 py-1 flex justify-between`}
|
||||
onMouseEnter={() => setHoveredField(i)}
|
||||
onMouseLeave={() => setHoveredField(-1)}
|
||||
>
|
||||
<div className={hoveredField === i ? "text-zinc-500" : ""}>
|
||||
<button
|
||||
className={`w-[9px] h-[9px] bg-[#2f68ad] opacity-80 z-50 rounded-full me-2`}
|
||||
/>
|
||||
{e.name}
|
||||
</div>
|
||||
<div className="text-zinc-400">{e.type}</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</foreignObject>
|
||||
);
|
||||
}
|
||||
|
||||
function Relationship({ relationship }) {
|
||||
const pathRef = useRef();
|
||||
let start = { x: 0, y: 0 };
|
||||
let end = { x: 0, y: 0 };
|
||||
|
||||
let cardinalityStart = "1";
|
||||
let cardinalityEnd = "1";
|
||||
|
||||
switch (relationship.cardinality) {
|
||||
case Cardinality.MANY_TO_ONE:
|
||||
cardinalityStart = "n";
|
||||
cardinalityEnd = "1";
|
||||
break;
|
||||
case Cardinality.ONE_TO_MANY:
|
||||
cardinalityStart = "1";
|
||||
cardinalityEnd = "n";
|
||||
break;
|
||||
case Cardinality.ONE_TO_ONE:
|
||||
cardinalityStart = "1";
|
||||
cardinalityEnd = "1";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
const length = 32;
|
||||
|
||||
const [refAquired, setRefAquired] = useState(false);
|
||||
useEffect(() => {
|
||||
setRefAquired(true);
|
||||
}, []);
|
||||
|
||||
if (refAquired) {
|
||||
const pathLength = pathRef.current.getTotalLength();
|
||||
const point1 = pathRef.current.getPointAtLength(length);
|
||||
start = { x: point1.x, y: point1.y };
|
||||
const point2 = pathRef.current.getPointAtLength(pathLength - length);
|
||||
end = { x: point2.x, y: point2.y };
|
||||
}
|
||||
|
||||
return (
|
||||
<g className="select-none" onClick={() => console.log(pathRef.current)}>
|
||||
<path
|
||||
ref={pathRef}
|
||||
d={calcPath(
|
||||
relationship.startX,
|
||||
relationship.endX,
|
||||
relationship.startY,
|
||||
relationship.endY,
|
||||
relationship.startFieldId,
|
||||
relationship.endFieldId
|
||||
)}
|
||||
stroke="gray"
|
||||
fill="none"
|
||||
strokeWidth={2}
|
||||
/>
|
||||
{pathRef.current && (
|
||||
<>
|
||||
<circle cx={start.x} cy={start.y} r="12" fill="grey"></circle>
|
||||
<text
|
||||
x={start.x}
|
||||
y={start.y}
|
||||
fill="white"
|
||||
strokeWidth="0.5"
|
||||
textAnchor="middle"
|
||||
alignmentBaseline="middle"
|
||||
>
|
||||
{cardinalityStart}
|
||||
</text>
|
||||
<circle cx={end.x} cy={end.y} r="12" fill="grey"></circle>
|
||||
<text
|
||||
x={end.x}
|
||||
y={end.y}
|
||||
fill="white"
|
||||
strokeWidth="0.5"
|
||||
textAnchor="middle"
|
||||
alignmentBaseline="middle"
|
||||
>
|
||||
{cardinalityEnd}
|
||||
</text>
|
||||
</>
|
||||
)}
|
||||
</g>
|
||||
);
|
||||
}
|
||||
|
||||
function Canvas() {
|
||||
const [tables, setTables] = useState(diagram.tables);
|
||||
const [relationships, setRelationships] = useState(diagram.relationships);
|
||||
const [dragging, setDragging] = useState(-1);
|
||||
const [offset, setOffset] = useState({ x: 0, y: 0 });
|
||||
|
||||
const grabTable = (e, id) => {
|
||||
setDragging(id);
|
||||
setOffset({
|
||||
x: e.clientX - tables[id].x,
|
||||
y: e.clientY - tables[id].y,
|
||||
});
|
||||
};
|
||||
|
||||
const moveTable = (e) => {
|
||||
if (dragging !== -1) {
|
||||
const dx = e.clientX - offset.x;
|
||||
const dy = e.clientY - offset.y;
|
||||
setTables((prev) =>
|
||||
prev.map((table, i) => {
|
||||
if (i === dragging) {
|
||||
setRelationships((prev) =>
|
||||
prev.map((r) => {
|
||||
if (r.startTableId === i) {
|
||||
return {
|
||||
...r,
|
||||
startX: dx + 15,
|
||||
startY: dy + r.startFieldId * 36 + 69,
|
||||
endX: tables[r.endTableId].x + 15,
|
||||
endY: tables[r.endTableId].y + r.endFieldId * 36 + 69,
|
||||
};
|
||||
} else if (r.endTableId === i) {
|
||||
return {
|
||||
...r,
|
||||
startX: tables[r.startTableId].x + 15,
|
||||
startY: tables[r.startTableId].y + r.startFieldId * 36 + 69,
|
||||
endX: dx + 15,
|
||||
endY: dy + r.endFieldId * 36 + 69,
|
||||
};
|
||||
}
|
||||
return r;
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
...table,
|
||||
x: dx,
|
||||
y: dy,
|
||||
};
|
||||
}
|
||||
return table;
|
||||
})
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const releaseTable = () => {
|
||||
setDragging(-1);
|
||||
setOffset({ x: 0, y: 0 });
|
||||
};
|
||||
|
||||
return (
|
||||
<svg
|
||||
className="w-full h-full cursor-grab"
|
||||
onMouseUp={releaseTable}
|
||||
onMouseMove={moveTable}
|
||||
onMouseLeave={releaseTable}
|
||||
>
|
||||
<defs>
|
||||
<pattern
|
||||
id="pattern-circles"
|
||||
x="0"
|
||||
y="0"
|
||||
width="22"
|
||||
height="22"
|
||||
patternUnits="userSpaceOnUse"
|
||||
patternContentUnits="userSpaceOnUse"
|
||||
>
|
||||
<circle
|
||||
id="pattern-circle"
|
||||
cx="4"
|
||||
cy="4"
|
||||
r="0.85"
|
||||
fill="rgb(99, 152, 191)"
|
||||
></circle>
|
||||
</pattern>
|
||||
</defs>
|
||||
<rect
|
||||
x="0"
|
||||
y="0"
|
||||
width="100%"
|
||||
height="100%"
|
||||
fill="url(#pattern-circles)"
|
||||
></rect>
|
||||
{tables.map((t, i) => (
|
||||
<Table key={i} table={t} grab={(e) => grabTable(e, i)} />
|
||||
))}
|
||||
{relationships.map((r, i) => (
|
||||
<Relationship key={i} relationship={r} />
|
||||
))}
|
||||
</svg>
|
||||
);
|
||||
}
|
||||
import Canvas from "../components/AuthCanvas";
|
||||
import { diagram } from "../data/signupDiagram";
|
||||
|
||||
export default function SignUp() {
|
||||
const [formValues, setFormValues] = useState({
|
||||
@@ -369,7 +56,7 @@ export default function SignUp() {
|
||||
return (
|
||||
<div className="grid grid-cols-7 h-screen bg-white">
|
||||
<div className="bg-white col-span-3 sm:hidden md:hidden lg:col-span-3 overflow-y-hidden relative">
|
||||
<Canvas />
|
||||
<Canvas diagram={diagram} />
|
||||
<Link to="/">
|
||||
<img src={logo} className="absolute left-0 top-0 p-3" width={56} />
|
||||
</Link>
|
||||
|
Reference in New Issue
Block a user