Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add link copy, better error handling #25

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions src/components/LearningPath.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import LearnPathMarkdownPreviewer from "./learningpath/MarkdownPreviewer";
import LearnPathVisualRenderer, { Path } from "./learningpath/Visualizer";
import { manipulatePath } from "./learningpath/PathManipulation";

const copyToClipboard = (text: string) => navigator.clipboard.writeText(text);

function Tab({
setGraphFullView,
isGraphFullView,
setMarkdownOpen,
isMarkdownOpen,
isUndoDisabled,
undoNodeLink,
isUndoDisabled,
}: {
setGraphFullView: (bool: boolean) => void;
isGraphFullView: boolean;
Expand Down Expand Up @@ -40,6 +42,25 @@ function Tab({
/>
</svg>
</button>
<button
onClick={() => copyToClipboard(window.location.toString())}
className="border-2 bg-background text-secondary border-slate p-2 rounded-xl"
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className="w-5 h-5"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="m18.375 12.739-7.693 7.693a4.5 4.5 0 0 1-6.364-6.364l10.94-10.94A3 3 0 1 1 19.5 7.372L8.552 18.32m.009-.01-.01.01m5.699-9.941-7.81 7.81a1.5 1.5 0 0 0 2.112 2.13"
/>
</svg>
</button>
<button
onClick={() => setGraphFullView(!isGraphFullView)}
className="border-2 bg-background text-secondary border-slate p-2 rounded-xl"
Expand Down Expand Up @@ -100,18 +121,19 @@ function Tab({
}

export default function Main() {
const [nodeUrl, setNodeUrl] = useState<string>("/index.json");
const [markdownUrl, setMarkdownUrl] = useState<string>("/index.md");
const nodeParam =
new URLSearchParams(window.location.search).get("node") || "/index";

const [nodeUrl, setNodeUrl] = useState<string>(nodeParam + ".json");
const [markdownUrl, setMarkdownUrl] = useState<string>(nodeParam + ".md");

const [isGraphFullView, setGraphFullView] = useState(false);
const [isMarkdownOpen, setMarkdownOpen] = useState(false);

const modules = useMemo(() => {
const importModules = import.meta.glob(
"/public/content/learningpaths/**/*.{md,json}",
{
eager: true,
},
{ eager: true },
);
return (relativePath: string) =>
importModules[
Expand Down Expand Up @@ -156,7 +178,7 @@ export default function Main() {
}
};

return (
return node[0] && node[1] ? (
<div
style={
isGraphFullView
Expand Down Expand Up @@ -193,5 +215,9 @@ export default function Main() {
undoNodeLink={revertNode}
/>
</div>
) : (
<p className="border border-secondary px-4 py-2 rounded-xl">
Sorry, but this visual graph could not be loaded.
</p>
);
}
95 changes: 43 additions & 52 deletions src/components/learningpath/Visualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,8 @@ function NodeLine({ data, from }: { data: PathNode; from: PathNode }) {
>
<defs>
<linearGradient gradientUnits="userSpaceOnUse" id={gradientId}>
<stop offset="20%" stopColor={generationColor(from.generation)} />
<stop
offset="50%"
stopColor={generationColor(data.generation + 1)}
opacity={0.2}
/>
<stop offset="80%" stopColor={generationColor(data.generation)} />
<stop offset="40%" stopColor={generationColor(from.generation)} />
<stop offset="60%" stopColor={generationColor(data.generation)} />
</linearGradient>
</defs>
<line
Expand Down Expand Up @@ -193,61 +188,57 @@ export default function LearnPathVisualRenderer({
maxDimensions: {
maxX: number;
maxY: number;
} | null;
path: Path | null;
};
path: Path;
}) {
return (
<div
key={JSON.stringify(path)}
className="h-[100%] w-[100%] overflow-scroll border-2 border-secondary bg-background border-opacity-20 rounded-2xl p-5 pt-16 select-none relative"
className="h-[100%] w-[100%] overflow-scroll border-2 border-secondary bg-background border-opacity-20 rounded-2xl p-5 pt-20 select-none relative"
>
{path ? (
<div
className="absolute w-full h-full"
style={{
width: maxDimensions?.maxX || 100,
height: maxDimensions?.maxY || 100,
<div
className="absolute w-full h-full"
style={{
width: maxDimensions?.maxX || 100,
height: maxDimensions?.maxY || 100,
}}
>
<Node
setMarkdownUrl={setMarkdownUrl}
setNodeUrl={setNodeUrl}
data={{
name: path.title,
generation: 0,
height: path.height,
width: path.width,
x: path.x,
y: path.y,
markdownUrl: path.markdownUrl,
}}
>
from={null}
/>
{path.nodes?.map((node, i, elements) => (
<Node
setMarkdownUrl={setMarkdownUrl}
setNodeUrl={setNodeUrl}
data={{
name: path.title,
generation: 0,
height: path.height,
width: path.width,
x: path.x,
y: path.y,
markdownUrl: path.markdownUrl,
}}
from={null}
key={i}
data={node}
from={
i == 0
? {
name: path.title,
generation: 0,
height: path.height,
width: path.width,
x: path.x,
y: path.y,
markdownUrl: path.markdownUrl,
}
: elements[i - 1]
}
/>
{path.nodes?.map((node, i, elements) => (
<Node
setMarkdownUrl={setMarkdownUrl}
setNodeUrl={setNodeUrl}
key={i}
data={node}
from={
i == 0
? {
name: path.title,
generation: 0,
height: path.height,
width: path.width,
x: path.x,
y: path.y,
markdownUrl: path.markdownUrl,
}
: elements[i - 1]
}
/>
))}
</div>
) : (
<p>Sorry</p>
)}
))}
</div>
</div>
);
}
Loading