Skip to content

Commit

Permalink
Merge pull request #30 from ozkeisar/disable-filename-field
Browse files Browse the repository at this point in the history
disable filename field
  • Loading branch information
ozkeisar authored Sep 26, 2024
2 parents d8611e1 + 5236e44 commit eda1af1
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.5.0",
"version": "1.5.1",
"description": "",
"keywords": [
"electron",
Expand Down
4 changes: 2 additions & 2 deletions release/app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion release/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Mockingbird",
"version": "1.5.0",
"version": "1.5.1",
"description": "",
"license": "MIT",
"author": {
Expand Down
37 changes: 11 additions & 26 deletions src/renderer/components/dialogs/parentDialog/parentDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';

import Typography from '@mui/material/Typography';
import { v4 as uuid } from 'uuid';
Expand All @@ -29,11 +29,12 @@ import {
RouteParent,
} from '../../../../types';
import styles from './parentDialog.module.css';
import { formatFileName } from '../../../../utils/utils';
import { formatToValidFilename } from '../../../../utils/utils';
import { isParentExist, parentsProperties } from '../../../../utils/parent';

const METHODS: GraphQlRouteType[] = ['Query', 'Mutation'];


type Props = {
onClose: Function;
open: boolean;
Expand Down Expand Up @@ -62,6 +63,12 @@ export function ParentDialog({
const isEdit = !!data?.id;
const isGraphQl = type === 'GraphQl';

useEffect(()=>{
if(!isEdit){
setFilename(formatToValidFilename(isGraphQl ? name :restPath))
}
}, [isEdit, restPath, name, isGraphQl])


const {
filenames,
Expand Down Expand Up @@ -162,13 +169,6 @@ export function ParentDialog({

const handleNameChanged = (e: any) => {
setName(e.target.value);

if (
(filename.length === 0 || e.target.value.includes(filename)) &&
!isEdit
) {
setFilename(formatFileName(e.target.value));
}
};

const handleSchemaPathChanged = (e: any) => {
Expand All @@ -177,13 +177,6 @@ export function ParentDialog({

const handleRestPathChanged = (e: any) => {
setRestPath(e.target.value);

if (
(!isEdit && filename.length === 0) ||
e.target.value.includes(filename)
) {
setFilename(formatFileName(e.target.value));
}
};

const handleServerChange = (event: SelectChangeEvent) => {
Expand Down Expand Up @@ -359,8 +352,8 @@ export function ParentDialog({
</div>
{type === 'Rest' ? renderRestType() : renderGraphQlType()}
<TextField
disabled={isEdit}
value={filename}
disabled
value={filename + '.json'}
required
margin="dense"
id="filename"
Expand All @@ -369,14 +362,6 @@ export function ParentDialog({
type="text"
fullWidth
variant="outlined"
onChange={(e) => {
if (
isValidFilename(e.target.value) ||
e.target.value.length === 0
) {
setFilename(e.target.value);
}
}}
error={!!filenameAlreadyExist}
/>
{!!filenameAlreadyExist && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import LoadingButton from '@mui/lab/LoadingButton';
import { PresetsFolder } from '../../../../types';
import {
emitSocketEvent,
isValidFilename,
reportButtonClick,
socket,
} from '../../../utils';
import { useProjectStore } from '../../../state/project';
import { EVENT_KEYS } from '../../../../types/events';
import { BUTTONS } from '../../../../consts/analytics';
import { formatToValidFilename } from '../../../../utils/utils';

type Props = {
onClose: Function;
Expand Down Expand Up @@ -49,6 +49,12 @@ export function PresetFolderDialog({ onClose, open, data }: Props) {
const filenameAlreadyExist =
existingFilenames?.includes(filename) && data?.filename !== filename;

useEffect(()=>{
if(!isEdit){
setFilename(formatToValidFilename(name))
}
}, [isEdit, name])

useEffect(() => {
const onEvent = (arg: any) => {
setIsLoading(false);
Expand Down Expand Up @@ -116,8 +122,8 @@ export function PresetFolderDialog({ onClose, open, data }: Props) {
)}

<TextField
disabled={isEdit}
value={filename}
disabled
value={filename + '.json'}
required
margin="dense"
id="filename"
Expand All @@ -126,11 +132,6 @@ export function PresetFolderDialog({ onClose, open, data }: Props) {
type="text"
fullWidth
variant="outlined"
onChange={(e) => {
if (isValidFilename(e.target.value) || !e.target.value.length) {
setFilename(e.target.value);
}
}}
error={!!filenameAlreadyExist}
/>
{!!filenameAlreadyExist && (
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { loader } from '@monaco-editor/react';
import * as monaco from 'monaco-editor';
import * as amplitude from '@amplitude/analytics-browser';
import App from './App';
import pj from './../../package.json'

const isDev = !process.env.NODE_ENV || process.env.NODE_ENV === 'development';

amplitude.init(
isDev
? '16543070a2c829f8a27f46c21fc0f708'
: 'ed51d61371d63ef1136b842900ebdae',
);
{appVersion: pj.version});

loader.config({ monaco });

Expand Down
35 changes: 35 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { v4 as uuid } from 'uuid';


export function removeParameters(input: string | null): string | null {
if (!input) {
return input;
Expand Down Expand Up @@ -53,3 +56,35 @@ export const formatFileName = (input: string): string => {
return truncatedFileName;
};


export function formatToValidFilename(input: string): string {
// List of invalid characters for Windows filenames
const invalidWindowsChars = /[<>:"/\\|?*\x00-\x1F]/g;
// macOS only disallows the ":" character
const invalidMacChars = /[:]/g;

// Replace invalid characters with underscores, except for the first and last characters
let sanitized = input.replace(invalidWindowsChars, '_').replace(invalidMacChars, '_');

// Remove any invalid characters if they are at the beginning or end
sanitized = sanitized.replace(/^[_]+|[_]+$/g, '');

// Trim whitespace from the start and end
sanitized = sanitized.trim();

// Ensure filename is not empty
if (sanitized === '' || sanitized === '.' || sanitized === '..') {
sanitized = 'untitled';
}

// Add a unique identifier (UUID) to the end of the filename
const uniqueSuffix = uuid().split('-')[0]; // Use only the first part of the UUID for brevity
sanitized = `${sanitized}_${uniqueSuffix}`;

// Limit the filename length to 255 characters minus the length of the unique suffix and underscore
if (sanitized.length > 255) {
sanitized = sanitized.substring(0, 255 - uniqueSuffix.length - 1) + `_${uniqueSuffix}`;
}

return sanitized;
}

0 comments on commit eda1af1

Please sign in to comment.