-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Create new field type JSON #4729
Changes from all commits
c22d6f6
d4f8efe
3be0ea4
8077ecd
1838f1f
5f34b07
f615626
3772d40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useJsonField } from '@/object-record/record-field/meta-types/hooks/useJsonField'; | ||
import { JsonDisplay } from '@/ui/field/display/components/JsonDisplay'; | ||
|
||
export const JsonFieldDisplay = () => { | ||
const { fieldValue, maxWidth } = useJsonField(); | ||
|
||
return ( | ||
<JsonDisplay | ||
text={fieldValue ? JSON.stringify(JSON.parse(fieldValue), null, 2) : ''} | ||
maxWidth={maxWidth} | ||
/> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { useContext } from 'react'; | ||
import { useRecoilState, useRecoilValue } from 'recoil'; | ||
|
||
import { useRecordFieldInput } from '@/object-record/record-field/hooks/useRecordFieldInput'; | ||
import { FieldJsonValue } from '@/object-record/record-field/types/FieldMetadata'; | ||
import { recordStoreFamilySelector } from '@/object-record/record-store/states/selectors/recordStoreFamilySelector'; | ||
import { FieldMetadataType } from '~/generated-metadata/graphql'; | ||
|
||
import { FieldContext } from '../../contexts/FieldContext'; | ||
import { assertFieldMetadata } from '../../types/guards/assertFieldMetadata'; | ||
import { isFieldRawJson } from '../../types/guards/isFieldRawJson'; | ||
import { isFieldTextValue } from '../../types/guards/isFieldTextValue'; | ||
|
||
export const useJsonField = () => { | ||
const { entityId, fieldDefinition, hotkeyScope, maxWidth } = | ||
useContext(FieldContext); | ||
|
||
assertFieldMetadata( | ||
FieldMetadataType.RawJson, | ||
isFieldRawJson, | ||
fieldDefinition, | ||
); | ||
|
||
const fieldName = fieldDefinition.metadata.fieldName; | ||
|
||
const [fieldValue, setFieldValue] = useRecoilState<FieldJsonValue>( | ||
recordStoreFamilySelector({ | ||
recordId: entityId, | ||
fieldName: fieldName, | ||
}), | ||
); | ||
const fieldTextValue = isFieldTextValue(fieldValue) ? fieldValue : ''; | ||
|
||
const { setDraftValue, getDraftValueSelector } = | ||
useRecordFieldInput<FieldJsonValue>(`${entityId}-${fieldName}`); | ||
|
||
const draftValue = useRecoilValue(getDraftValueSelector()); | ||
|
||
return { | ||
draftValue, | ||
setDraftValue, | ||
maxWidth, | ||
fieldDefinition, | ||
fieldValue: fieldTextValue, | ||
setFieldValue, | ||
hotkeyScope, | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { isValidJSON } from '@/object-record/record-field/utils/isFieldValueJson'; | ||
import { FieldTextAreaOverlay } from '@/ui/field/input/components/FieldTextAreaOverlay'; | ||
import { TextAreaInput } from '@/ui/field/input/components/TextAreaInput'; | ||
|
||
import { usePersistField } from '../../../hooks/usePersistField'; | ||
import { useJsonField } from '../../hooks/useJsonField'; | ||
|
||
import { FieldInputEvent } from './DateFieldInput'; | ||
|
||
export type RawJsonFieldInputProps = { | ||
onClickOutside?: FieldInputEvent; | ||
onEnter?: FieldInputEvent; | ||
onEscape?: FieldInputEvent; | ||
onTab?: FieldInputEvent; | ||
onShiftTab?: FieldInputEvent; | ||
}; | ||
|
||
export const RawJsonFieldInput = ({ | ||
onEnter, | ||
onEscape, | ||
onClickOutside, | ||
onTab, | ||
onShiftTab, | ||
}: RawJsonFieldInputProps) => { | ||
const { fieldDefinition, draftValue, hotkeyScope, setDraftValue } = | ||
useJsonField(); | ||
|
||
const persistField = usePersistField(); | ||
|
||
const handlePersistField = (newText: string) => { | ||
if (!newText || isValidJSON(newText)) persistField(newText || null); | ||
}; | ||
|
||
const handleEnter = (newText: string) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the behavior for other fields, right now I'm unable to escape / enter if invalid json. Instead I should be able to escape and it should come back to the previous value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense |
||
onEnter?.(() => handlePersistField(newText)); | ||
}; | ||
|
||
const handleEscape = (newText: string) => { | ||
onEscape?.(() => handlePersistField(newText)); | ||
}; | ||
|
||
const handleClickOutside = ( | ||
_event: MouseEvent | TouchEvent, | ||
newText: string, | ||
) => { | ||
onClickOutside?.(() => handlePersistField(newText)); | ||
}; | ||
|
||
const handleTab = (newText: string) => { | ||
onTab?.(() => handlePersistField(newText)); | ||
}; | ||
|
||
const handleShiftTab = (newText: string) => { | ||
onShiftTab?.(() => handlePersistField(newText)); | ||
}; | ||
|
||
const handleChange = (newText: string) => { | ||
setDraftValue(newText); | ||
}; | ||
|
||
const value = | ||
draftValue && isValidJSON(draftValue) | ||
? JSON.stringify(JSON.parse(draftValue), null, 2) | ||
: draftValue ?? ''; | ||
|
||
return ( | ||
<FieldTextAreaOverlay> | ||
<TextAreaInput | ||
placeholder={fieldDefinition.metadata.placeHolder} | ||
FelixMalfait marked this conversation as resolved.
Show resolved
Hide resolved
|
||
autoFocus | ||
value={value} | ||
onClickOutside={handleClickOutside} | ||
onEnter={handleEnter} | ||
onEscape={handleEscape} | ||
onShiftTab={handleShiftTab} | ||
onTab={handleTab} | ||
hotkeyScope={hotkeyScope} | ||
onChange={handleChange} | ||
maxRows={25} | ||
/> | ||
</FieldTextAreaOverlay> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { isNull, isString } from '@sniptt/guards'; | ||
|
||
import { FieldJsonValue } from '../FieldMetadata'; | ||
|
||
// TODO: add zod | ||
export const isFieldRawJsonValue = ( | ||
fieldValue: unknown, | ||
): fieldValue is FieldJsonValue => isString(fieldValue) || isNull(fieldValue); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { isString } from '@sniptt/guards'; | ||
|
||
export const isValidJSON = (str: string) => { | ||
try { | ||
if (isString(JSON.parse(str))) { | ||
throw new Error(`Strings are not supported as JSON: ${str}`); | ||
} | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { EllipsisDisplay } from './EllipsisDisplay'; | ||
|
||
type JsonDisplayProps = { | ||
text: string; | ||
maxWidth?: number; | ||
}; | ||
|
||
export const JsonDisplay = ({ text, maxWidth }: JsonDisplayProps) => ( | ||
<EllipsisDisplay maxWidth={maxWidth}>{text}</EllipsisDisplay> | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gitstart-twenty what is this useful for?