<commit_before>import * as React from "react";
import * as data from "./data";
import * as sui from "./sui";
import * as simulator from "./simulator";
import * as screenshot from "./screenshot";

type ISettingsProps = pxt.editor.ISettingsProps;

export enum ShareMode {
    Code,
    Url,
    Editor,
    Simulator
}

export interface ShareEditorProps extends ISettingsProps {
    loading?: boolean;
}

export enum ShareRecordingState {
    None,
    ScreenshotSnap,
    GifLoading,
    GifRecording,
    GifRendering
}

// This Component overrides shouldComponentUpdate, be sure to update that if the state is updated
export interface ShareEditorState {
    advancedMenu?: boolean;
    mode?: ShareMode;
    currentPubId?: string;
    pubCurrent?: boolean;
    visible?: boolean;
    sharingError?: boolean;
    loading?: boolean;
    projectName?: string;
    projectNameChanged?: boolean;
    thumbnails?: boolean;
    screenshotUri?: string;
    recordingState?: ShareRecordingState;
    recordError?: string;
}

export class ShareEditor extends data.Component<ShareEditorProps, ShareEditorState> {
    private loanedSimulator: HTMLElement;
    private _gifEncoder: screenshot.GifEncoder;

    constructor(props: ShareEditorProps) {
        super(props);
        this.state = {
            currentPubId: undefined,
            pubCurrent: false,
            visible: false,
            advancedMenu: false,
            screenshotUri: undefined,
            recordingState: ShareRecordingState.None,
            recordError: undefined
        }

        this.hide = this.hide.bind(this);
        this.toggleAdvancedMenu = this.toggleAdvancedMenu.bind(this);
        this.setAdvancedMode = this.setAdvancedMode.bind(this);
        this.handleProjectNameChange = this.handleProjectNameChange.bind(this);
        this.restartSimulator = this.restartSimulator.bind(this);
        this.handleRecordClick = this.handleRecordClick.bind(this);
        this.handleScreenshotClick = this.handleScreenshotClick.bind(this);
        this.handleScreenshotMessage = this.handleScreenshotMessage.bind(this);
    }

    hide() {
        if (this._gifEncoder) {
            this._gifEncoder.cancel();
            this._gifEncoder = undefined;
        }
        if (this.loanedSimulator) {
            simulator.driver.unloanSimulator();
            this.loanedSimulator = undefined;
            this.props.parent.popScreenshotHandler();
            simulator.driver.stopRecording();
        }
        this.setState({
            visible: false,
            screenshotUri: undefined,
            projectName: undefined,
            projectNameChanged: false,
            recordingState: ShareRecordingState.None,
            recordError: undefined
        });
    }

    show(header: pxt.workspace.Header) {
        // TODO investigate why edge does not render well
        // upon hiding dialog, the screen does not redraw properly
        const thumbnails = pxt.appTarget.cloud && pxt.appTarget.cloud.thumbnails;
        if (thumbnails) {
            this.loanedSimulator = simulator.driver.loanSimulator();
            this.props.parent.pushScreenshotHandler(this.handleScreenshotMessage);
        }
        this.setState({
            thumbnails,
            visible: true,
            mode: ShareMode.Code,
            pubCurrent: header.pubCurrent,
            sharingError: false,
            screenshotUri: undefined
        }, () => this.props.parent.startSimulator());
    }

    handleScreenshotMessage(msg: pxt.editor.ScreenshotData) {
        if (!msg) return;

        if (msg.event === "start") {
            switch (this.state.recordingState) {
                case ShareRecordingState.None:
                    this.gifRecord();
                    break;
                default:
                    // ignore<commit_msg>better flag check for sim in share dialog (#5305)<commit_after>import * as React from "react";
import * as data from "./data";
import * as sui from "./sui";
import * as simulator from "./simulator";
import * as screenshot from "./screenshot";

type ISettingsProps = pxt.editor.ISettingsProps;

export enum ShareMode {
    Code,
    Url,
    Editor,
    Simulator
}

export interface ShareEditorProps extends ISettingsProps {
    loading?: boolean;
}

export enum ShareRecordingState {
    None,
    ScreenshotSnap,
    GifLoading,
    GifRecording,
    GifRendering
}

// This Component overrides shouldComponentUpdate, be sure to update that if the state is updated
export interface ShareEditorState {
    advancedMenu?: boolean;
    mode?: ShareMode;
    currentPubId?: string;
    pubCurrent?: boolean;
    visible?: boolean;
    sharingError?: boolean;
    loading?: boolean;
    projectName?: string;
    projectNameChanged?: boolean;
    thumbnails?: boolean;
    screenshotUri?: string;
    recordingState?: ShareRecordingState;
    recordError?: string;
}

export class ShareEditor extends data.Component<ShareEditorProps, ShareEditorState> {
    private loanedSimulator: HTMLElement;
    private _gifEncoder: screenshot.GifEncoder;

    constructor(props: ShareEditorProps) {
        super(props);
        this.state = {
            currentPubId: undefined,
            pubCurrent: false,
            visible: false,
            advancedMenu: false,
            screenshotUri: undefined,
            recordingState: ShareRecordingState.None,
            recordError: undefined
        }

        this.hide = this.hide.bind(this);
        this.toggleAdvancedMenu = this.toggleAdvancedMenu.bind(this);
        this.setAdvancedMode = this.setAdvancedMode.bind(this);
        this.handleProjectNameChange = this.handleProjectNameChange.bind(this);
        this.restartSimulator = this.restartSimulator.bind(this);
        this.handleRecordClick = this.handleRecordClick.bind(this);
        this.handleScreenshotClick = this.handleScreenshotClick.bind(this);
        this.handleScreenshotMessage = this.handleScreenshotMessage.bind(this);
    }

    hide() {
        if (this._gifEncoder) {
            this._gifEncoder.cancel();
            this._gifEncoder = undefined;
        }
        if (this.loanedSimulator) {
            simulator.driver.unloanSimulator();
            this.loanedSimulator = undefined;
            this.props.parent.popScreenshotHandler();
            simulator.driver.stopRecording();
        }
        this.setState({
            visible: false,
            screenshotUri: undefined,
            projectName: undefined,
            projectNameChanged: false,
            recordingState: ShareRecordingState.None,
            recordError: undefined
        });
    }

    show(header: pxt.workspace.Header) {
        // TODO investigate why edge does not render well
        // upon hiding dialog, the screen does not redraw properly
        const thumbnails = pxt.appTarget.cloud && pxt.appTarget.cloud.thumbnails
            && (pxt.appTarget.appTheme.simScreenshot || pxt.appTarget.appTheme.simGif);
        if (thumbnails) {
            this.loanedSimulator = simulator.driver.loanSimulator();
            this.props.parent.pushScreenshotHandler(this.handleScreenshotMessage);
        }
        this.setState({
            thumbnails,
            visible: true,
            mode: ShareMode.Code,
            pubCurrent: header.pubCurrent,
            sharingError: false,
            screenshotUri: undefined
        }, () => this.props.parent.startSimulator());
    }

    handleScreenshotMessage(msg: pxt.editor.ScreenshotData) {
        if (!msg) return;

        if (msg.event === "start") {
            switch (this.state.recordingState) {
                case ShareRecordingState.None:
                    this.gifRecord();
                    break;
                default:
                    // ignore
{'fraction_non_alphanumeric': 0.09126013643969623, 'fraction_numerical': 0.0005148667782211353, 'mean_word_length': 2.362180874080485, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2445593', 'n_tokens_mistral': 2097, 'n_tokens_neox': 2050, 'n_words': 453}