Skip to content

Commit

Permalink
Refatoração no compartilhamento de código para abrir espaço a arquivo…
Browse files Browse the repository at this point in the history
…s pessoais
  • Loading branch information
dgadelha committed Oct 26, 2024
1 parent 8f4ff31 commit b8271f4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 39 deletions.
38 changes: 17 additions & 21 deletions packages/ide/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Component, OnDestroy, OnInit } from "@angular/core";
import { Storage, getBlob, ref } from "@angular/fire/storage";
import { Component, inject, OnDestroy, OnInit } from "@angular/core";
import { FormControl } from "@angular/forms";
import { MatDialog, MatDialogRef } from "@angular/material/dialog";
import { MatSnackBar } from "@angular/material/snack-bar";
Expand All @@ -8,6 +7,7 @@ import { GoogleAnalyticsService } from "ngx-google-analytics";
import { Subscription } from "rxjs";
import { DialogConfirmCloseTabComponent } from "./dialog-confirm-close-tab/dialog-confirm-close-tab.component";
import { DialogRenameTabComponent } from "./dialog-rename-tab/dialog-rename-tab.component";
import { ShareService } from "./share.service";

interface Tab {
id: number;
Expand All @@ -22,6 +22,11 @@ interface Tab {
styleUrl: "./app.component.scss",
})
export class AppComponent implements OnInit, OnDestroy {
private gaService = inject(GoogleAnalyticsService);
private snack = inject(MatSnackBar);
private dialog = inject(MatDialog);
private shareService = inject(ShareService);

renameDialogRef?: MatDialogRef<DialogRenameTabComponent>;
renameDialogSubscription?: Subscription;
closeDialogRef?: MatDialogRef<DialogConfirmCloseTabComponent>;
Expand All @@ -47,32 +52,23 @@ export class AppComponent implements OnInit, OnDestroy {
},
];

constructor(
private gaService: GoogleAnalyticsService,
private storage: Storage,
private snack: MatSnackBar,
private dialog: MatDialog,
) {}

ngOnInit() {
void (async () => {
try {
if (window.location.hash.startsWith("#share=")) {
this.snack.open("Carregando código compartilhado…", undefined, { duration: -1 });
if (window.location.hash.startsWith("#share=")) {
this.snack.open("Carregando código compartilhado…", undefined, { duration: -1 });

const hash = window.location.hash.slice(7);
const data = await getBlob(ref(this.storage, hash));
const contents = await data.text();
const hash = window.location.hash.slice(7);
const data = await this.shareService.load(hash);

this.addTab(`Código compartilhado (#${hash})`, contents);
if (data) {
this.addTab(`Código compartilhado (#${hash})`, data);
this.snack.dismiss();
this.gaService.event("load_shared_code_success", "Interface", "Código compartilhado carregado");
} else {
this.snack.dismiss();
this.snack.open("Erro ao carregar código compartilhado", "FECHAR", { duration: 10_000 });
this.gaService.event("load_shared_code_error", "Interface", "Erro ao carregar código compartilhado");
}
} catch (error) {
console.error(error);
this.snack.dismiss();
this.snack.open("Erro ao carregar código compartilhado", "FECHAR", { duration: 10_000 });
this.gaService.event("load_shared_code_error", "Interface", "Erro ao carregar código compartilhado");
}
})();
}
Expand Down
38 changes: 38 additions & 0 deletions packages/ide/src/app/share.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { inject, Injectable } from "@angular/core";
import { getBlob, ref, Storage, uploadString } from "@angular/fire/storage";

@Injectable({ providedIn: "root" })
export class ShareService {
storage = inject(Storage);

async share(code: string): Promise<string | null> {
const shareId = (Math.random() + 1).toString(36).slice(2, 9);

try {
await uploadString(ref(this.storage, `share/${shareId}.por`), code, undefined, {
contentType: "text/plain",
});

return `https://portugol.dev/#share=${shareId}`;
} catch (error) {
console.error(error);
return null;
}
}

async load(shareId: string): Promise<string | null> {
try {
const oldVersionRef = ref(this.storage, shareId);
const newVersionRef = ref(this.storage, `share/${shareId}.por`);

// TODO: Remover oldVersionRef após 11/11/2024
const data = await getBlob(newVersionRef).catch(() => getBlob(oldVersionRef));
const contents = await data.text();

return contents;
} catch (error) {
console.error(error);
return null;
}
}
}
29 changes: 11 additions & 18 deletions packages/ide/src/app/tab-editor/tab-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
Output,
TemplateRef,
ViewChild,
inject,
} from "@angular/core";
import { Storage, ref, uploadString } from "@angular/fire/storage";
import { MatSnackBar } from "@angular/material/snack-bar";
import type { PortugolCodeError } from "@portugol-webstudio/antlr";
import { PortugolExecutor, PortugolWebWorkersRunner } from "@portugol-webstudio/runner";
Expand All @@ -20,6 +20,7 @@ import { ShortcutInput } from "ng-keyboard-shortcuts";
import { GoogleAnalyticsService } from "ngx-google-analytics";
import { Subscription, debounceTime, fromEventPattern, mergeMap } from "rxjs";
import { FileService } from "../file.service";
import { ShareService } from "../share.service";
import { WorkerService } from "../worker.service";

@Component({
Expand All @@ -32,6 +33,12 @@ export class TabEditorComponent implements OnInit, OnDestroy {
private _stdOut$?: Subscription;
private _events$?: Subscription;

private gaService = inject(GoogleAnalyticsService);
private snack = inject(MatSnackBar);
private worker = inject(WorkerService);
private fileService = inject(FileService);
private shareService = inject(ShareService);

@Input()
title?: string;

Expand Down Expand Up @@ -104,14 +111,6 @@ export class TabEditorComponent implements OnInit, OnDestroy {
@ViewChild("shareSnackTemplate", { read: TemplateRef })
shareSnackTemplate!: TemplateRef<{ data: { url: string } }>;

constructor(
private gaService: GoogleAnalyticsService,
private storage: Storage,
private snack: MatSnackBar,
private worker: WorkerService,
private fileService: FileService,
) {}

ngOnInit() {
this.code ||= `programa {\n funcao inicio() {\n \n }\n}\n`;

Expand Down Expand Up @@ -357,18 +356,12 @@ export class TabEditorComponent implements OnInit, OnDestroy {

this.sharing = true;

const shareCode = (Math.random() + 1).toString(36).slice(2, 9);
const result = await uploadString(ref(this.storage, shareCode), this.code, undefined, {
contentType: "text/plain",
}).catch((error: unknown) => {
console.error(error);
return null;
});
const shareUrl = await this.shareService.share(this.code);

if (result) {
if (shareUrl) {
this.snack.openFromTemplate(this.shareSnackTemplate, {
data: {
url: `https://portugol.dev/#share=${shareCode}`,
url: shareUrl,
},
});

Expand Down

0 comments on commit b8271f4

Please sign in to comment.