import React, { useEffect, useState } from "react"; import { useParams } from "react-router-dom"; import { WRITER_MODE_HOSTNAMES } from "../../env"; import { Source } from "../../../../libs/types/document"; import "./edit-actions.scss"; import { useLocale } from "../../hooks"; export function EditActions({ source }: { source: Source }) { const { folder, filename, github_url } = source; const [opening, setOpening] = useState(false); const [editorOpeningError, setEditorOpeningError] = useState( null ); useEffect(() => { let unsetOpeningTimer: ReturnType; if (opening) { unsetOpeningTimer = setTimeout(() => { setOpening(false); }, 3000); } return () => { if (unsetOpeningTimer) { clearTimeout(unsetOpeningTimer); } }; }, [opening]); async function openInEditorHandler(event: React.MouseEvent) { event.preventDefault(); const filepath = `${folder}/${filename}`; console.log(`Going to try to open ${filepath} in your editor`); setOpening(true); try { const response = await fetch(`/_open?filepath=${filepath}`); if (!response.ok) { if (response.status >= 500) { setEditorOpeningError( new Error(`${response.status}: ${response.statusText}`) ); } else { const body = await response.text(); setEditorOpeningError(new Error(`${response.status}: ${body}`)); } } } catch (err: any) { setEditorOpeningError(err); } } const locale = useLocale(); const { "*": slug } = useParams(); if (!folder) { return null; } // If window.location.host is 'localhost:3000` then // window.location.hostname is 'localhost' const isReadOnly = !WRITER_MODE_HOSTNAMES.includes(window.location.hostname); return ( ); }