Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
cskrov committed Oct 4, 2024
1 parent 80a13e1 commit bf6f6e3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
31 changes: 25 additions & 6 deletions frontend/src/components/smart-editor/tabbed-editors/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import { useCanEditDocument } from '@app/components/smart-editor/hooks/use-can-e
import { Content } from '@app/components/smart-editor/tabbed-editors/content';
import { PositionedRight } from '@app/components/smart-editor/tabbed-editors/positioned-right';
import { StickyRight } from '@app/components/smart-editor/tabbed-editors/sticky-right';
import { useRefreshOboToken } from '@app/components/smart-editor/tabbed-editors/use-refresh-obo-token';
// import { useRefreshOboToken } from '@app/components/smart-editor/tabbed-editors/use-refresh-obo-token';
import { VersionStatus } from '@app/components/smart-editor/tabbed-editors/version-status';
import { DocumentErrorComponent } from '@app/error-boundary/document-error';
import { ErrorBoundary } from '@app/error-boundary/error-boundary';
import { hasOwn, isObject } from '@app/functions/object';
import { useOppgave } from '@app/hooks/oppgavebehandling/use-oppgave';
import { useSmartEditorSpellCheckLanguage } from '@app/hooks/use-smart-editor-language';
import { PlateEditor } from '@app/plate/plate-editor';
Expand Down Expand Up @@ -109,7 +110,7 @@ const PlateContext = ({ smartDocument, oppgave }: PlateContextProps) => {
const editor = useMyPlateEditorRef(id);
const [isConnected, setIsConnected] = useState(editor.yjs.provider.isConnected);

const oboTokenIsValid = useRefreshOboToken();
// const oboTokenIsValid = useRefreshOboToken();

// const editor = useMyPlateEditorRef(id);

Expand Down Expand Up @@ -150,11 +151,29 @@ const PlateContext = ({ smartDocument, oppgave }: PlateContextProps) => {

useEffect(() => {
// Close happens after connect is broken. Safe to reconnect.
const onClose = () => {
const onClose = async () => {
setIsConnected(false);

if (oboTokenIsValid) {
editor.yjs.provider.connect();
try {
const res = await fetch('/oauth2/session', { credentials: 'include' });

if (!res.ok) {
throw new Error(`API responded with error code ${res.status} for /oauth2/session`);
}

const data: unknown = await res.json();

if (
isObject(data) &&
hasOwn(data, 'session') &&
isObject(data.session) &&
hasOwn(data.session, 'active') &&
data.session.active === true
) {
editor.yjs.provider.connect();
}
} catch (err) {
console.error(err);
}
};

Expand All @@ -163,7 +182,7 @@ const PlateContext = ({ smartDocument, oppgave }: PlateContextProps) => {
return () => {
editor.yjs.provider.off('close', onClose);
};
}, [editor.yjs.provider, oboTokenIsValid]);
}, [editor.yjs.provider]);

useEffect(() => {
// Disconnect happens before close. Too early to reconnect.
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/functions/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** Checks if the given object has the given property. */
export const hasOwn = <T extends object, K extends PropertyKey>(obj: T, key: K): obj is T & Record<K, unknown> =>
Object.hasOwn(obj, key);

export const isObject = (value: unknown): value is Record<string, unknown> => value !== null && typeof value === 'object';
1 change: 1 addition & 0 deletions frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default defineConfig({
'/nytt-dokument': PROXY,
'/vedleggsoversikt': PROXY,
'/version': PROXY,
'/oauth': PROXY,
},
},
});

0 comments on commit bf6f6e3

Please sign in to comment.