Skip to content

Commit

Permalink
fix: fix active
Browse files Browse the repository at this point in the history
  • Loading branch information
YanYuanFE committed May 10, 2024
1 parent 13634d7 commit 922d3ef
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/app/(main)/editor/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function EditorPage() {
const { account } = useAccount();
const { compileCairo, compileContract, compileLoading, runCairo, runLoading, testLoading, runTests} = useCairoWasm();

console.log(account, 'acc')
console.log(account, active, files, 'acc')


useEffect(() => {
Expand Down Expand Up @@ -126,9 +126,10 @@ export default function EditorPage() {
...files,
{
content: '',
name: 'new.cairo',
name: `new_${files.length}.cairo`,
}
]);
setActive(files.length);
}

const updateFileByIndex = (index: number, value: string) => {
Expand All @@ -143,14 +144,18 @@ export default function EditorPage() {
const removeFile = (index: number) => {
setFiles(files?.filter((item, i) => i !== index));
if(index===active) {
setActive(index-1);
const newIndex = active === 0 ? 0 : active - 1;
console.log('new', newIndex)
setActive(newIndex);
} else if(index < active) {
setActive(active - 1);
}
}

const handleOpenFile = (e: any) => {
var file = e.target.files[0];
const file = e.target.files[0];
if (file) {
var reader = new FileReader();
const reader = new FileReader();
reader.onload = function(e) {
updateFileByIndex(active, e.target?.result as string);
}
Expand Down Expand Up @@ -205,10 +210,15 @@ export default function EditorPage() {
{
files?.map((file, i) => {
return (
<div key={i} onClick={() => setActive(i)}
<div key={i} onClick={() => {
setActive(i);
}}
className={cn('flex items-center border-l border-r px-4 py-2 cursor-pointer gap-2', active === i ? 'border-t-2 border-t-primary' : '')}>
{file.name}
{i !== 0 && <X size={16} onClick={() => removeFile(i)}/>}
{i !== 0 && <X size={16} onClick={(e) => {
removeFile(i);
e.stopPropagation();
}}/>}
</div>
)
})
Expand All @@ -220,13 +230,16 @@ export default function EditorPage() {
</div>
</div>
<div>
<Editor
theme={'vs-dark'}
height="40vh"
defaultLanguage="rust"
value={files[active].content}
onChange={(v) => updateFileByIndex(active, v || '')}
/>
{
files[active] ? <Editor
theme={'vs-dark'}
height="40vh"
defaultLanguage="rust"
value={files[active].content}
onChange={(v) => updateFileByIndex(active, v || '')}
/> : null
}

</div>
</div>
<div className="toolbar flex justify-between gap-4 my-4 px-4">
Expand Down

0 comments on commit 922d3ef

Please sign in to comment.