mirror of
https://github.com/RGBCube/alejandra
synced 2025-08-01 04:57:44 +00:00
Merge pull request #88 from kamadorueda/kamadorueda
feat: random file from nixpkgs
This commit is contained in:
commit
923a2f8652
3 changed files with 24527 additions and 3 deletions
|
@ -2,8 +2,10 @@ import react from "react";
|
||||||
import ReactDiffViewer from "react-diff-viewer";
|
import ReactDiffViewer from "react-diff-viewer";
|
||||||
import * as wasm from "alejandra-front";
|
import * as wasm from "alejandra-front";
|
||||||
import { Editor } from "./Editor";
|
import { Editor } from "./Editor";
|
||||||
|
import { get, randomPath } from "./nixpkgs";
|
||||||
|
|
||||||
export const SideBySide = () => {
|
export const SideBySide = () => {
|
||||||
|
const [path, setPath] = react.useState(randomPath());
|
||||||
const [loading, setLoading] = react.useState(true);
|
const [loading, setLoading] = react.useState(true);
|
||||||
const [before, setBefore] = react.useState("let x = 123; in\n x");
|
const [before, setBefore] = react.useState("let x = 123; in\n x");
|
||||||
|
|
||||||
|
@ -12,8 +14,17 @@ export const SideBySide = () => {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}, [wasm]);
|
}, [wasm]);
|
||||||
|
|
||||||
|
react.useEffect(async () => {
|
||||||
|
const content = await get(path);
|
||||||
|
setBefore(content);
|
||||||
|
}, [path]);
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
return <span>Loading</span>;
|
return (
|
||||||
|
<div className="flex items-center justify-center">
|
||||||
|
<div className="f6 tc">Loading</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const after = wasm.format(before, "before.nix");
|
const after = wasm.format(before, "before.nix");
|
||||||
|
@ -21,7 +32,17 @@ export const SideBySide = () => {
|
||||||
return (
|
return (
|
||||||
<react.Fragment>
|
<react.Fragment>
|
||||||
<div className="flex items-center justify-center">
|
<div className="flex items-center justify-center">
|
||||||
<div className="f6 fl tc w-40">Type your code below</div>
|
<div className="f6 fl tc w-40">
|
||||||
|
Type your code below or
|
||||||
|
<span
|
||||||
|
className="blue underline"
|
||||||
|
onClick={() => {
|
||||||
|
setPath(randomPath());
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
click here to fetch a random file from Nixpkgs
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
<div className="w-10" />
|
<div className="w-10" />
|
||||||
<div className="f6 fl tc w-40">With Alejandra ❤️</div>
|
<div className="f6 fl tc w-40">With Alejandra ❤️</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -39,7 +60,11 @@ export const SideBySide = () => {
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center justify-center pt1">
|
<div className="flex items-center justify-center pt1">
|
||||||
<div className="f6 fl w-80">
|
<div className="f6 fl w-80">
|
||||||
<ReactDiffViewer oldValue={before} newValue={after} />
|
<ReactDiffViewer
|
||||||
|
disableWordDiff={true}
|
||||||
|
newValue={after}
|
||||||
|
oldValue={before}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</react.Fragment>
|
</react.Fragment>
|
||||||
|
|
18
front/src/nixpkgs.js
Normal file
18
front/src/nixpkgs.js
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import { COMMIT, FILES } from "./nixpkgsFiles";
|
||||||
|
|
||||||
|
export const randomPath = () => FILES[Math.floor(Math.random() * FILES.length)];
|
||||||
|
|
||||||
|
export const path2url = (path) =>
|
||||||
|
`https://raw.githubusercontent.com/nixos/nixpkgs/${COMMIT}/${path}`;
|
||||||
|
|
||||||
|
export const get = async (path) => {
|
||||||
|
const url = path2url(path);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(url);
|
||||||
|
|
||||||
|
return await response.text();
|
||||||
|
} catch (error) {
|
||||||
|
return `# An error ocurred while fetching ${url}\n# ${error}`;
|
||||||
|
}
|
||||||
|
};
|
24481
front/src/nixpkgsFiles.js
Normal file
24481
front/src/nixpkgsFiles.js
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue