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 error toasts when loading from "url" query paramater #622

Merged
merged 5 commits into from
Nov 15, 2023
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
18 changes: 12 additions & 6 deletions apps/studio/src/services/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ export class ApplicationService extends AbstractService {
// subscribe to state to hide preloader
this.hidePreloader();

const { readOnly, url, base64 } = this.svcs.navigationSvc.getUrlParameters();
const { readOnly, url, base64 } =
this.svcs.navigationSvc.getUrlParameters();
// readOnly state should be only set to true when someone pass also url or base64 parameter
const isStrictReadonly = Boolean(readOnly && (url || base64));

let error: any;
let error: any;
try {
await this.fetchResource(url, base64);
} catch (err) {
error = err;
console.error(err);
}

if (error) {
appState.setState({ initErrors: [error] });
}

if (isStrictReadonly && !error) {
appState.setState({
readOnly,
Expand All @@ -32,7 +37,8 @@ export class ApplicationService extends AbstractService {
}

public async afterAppInit() {
const { readOnly, url, base64, redirectedFrom } = this.svcs.navigationSvc.getUrlParameters();
const { readOnly, url, base64, redirectedFrom } =
this.svcs.navigationSvc.getUrlParameters();
const isStrictReadonly = Boolean(readOnly && (url || base64));

// show RedirectedModal modal if the redirectedFrom is set (only when readOnly state is set to false)
Expand All @@ -45,11 +51,11 @@ export class ApplicationService extends AbstractService {
if (!url && !base64) {
return;
}

const { updateFile } = filesState.getState();
let content = '';
if (url) {
content = await fetch(url).then(res => res.text());
content = await fetch(url).then((res) => res.text());
} else if (base64) {
content = this.svcs.formatSvc.decodeBase64(base64);
}
Expand Down Expand Up @@ -79,4 +85,4 @@ export class ApplicationService extends AbstractService {
}
});
}
}
}
2 changes: 2 additions & 0 deletions apps/studio/src/state/app.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ export type AppState = {
initialized: boolean;
readOnly: boolean;
liveServer: boolean;
initErrors: any[],
}

export const appState = create<AppState>(() => ({
initialized: false,
readOnly: false,
liveServer: false,
initErrors: [],
}));

export const useAppState = appState;
13 changes: 11 additions & 2 deletions apps/studio/src/studio.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect } from 'react';
import { Toaster } from 'react-hot-toast';
import toast, { Toaster } from 'react-hot-toast';

import { Content, Sidebar, Template, Toolbar } from './components';

Expand All @@ -8,7 +8,9 @@ import { appState } from './state';

export interface AsyncAPIStudioProps {}

export const AsyncAPIStudio: React.FunctionComponent<AsyncAPIStudioProps> = () => {
export const AsyncAPIStudio: React.FunctionComponent<
AsyncAPIStudioProps
> = () => {
const services = useServices();

useEffect(() => {
Expand All @@ -24,6 +26,13 @@ export const AsyncAPIStudio: React.FunctionComponent<AsyncAPIStudioProps> = () =
</div>
);
}
const unsubscribe = appState.subscribe((state) => {
state.initErrors.forEach((e) => {
toast.error(e.message);
});
unsubscribe();
appState.setState({ initErrors: [] });
});

return (
<div className="flex flex-col h-full w-full h-screen">
Expand Down