-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
feat(dashboard): Digest liquid helper and popover handler #7439
Open
scopsy
wants to merge
6
commits into
pills-for-all-inputs
Choose a base branch
from
digest-liquid-helper
base: pills-for-all-inputs
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...c/components/primitives/field-editor/variable-popover/hooks/use-suggested-transformers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { useMemo } from 'react'; | ||
import { TRANSFORMERS } from '../constants'; | ||
import type { Transformer, TransformerWithParam } from '../types'; | ||
|
||
type SuggestionGroup = { | ||
label: string; | ||
transformers: Transformer[]; | ||
}; | ||
|
||
export function useSuggestedTransformers( | ||
variableName: string, | ||
currentTransformers: TransformerWithParam[] | ||
): SuggestionGroup[] { | ||
return useMemo(() => { | ||
const currentTransformerValues = new Set(currentTransformers.map((t) => t.value)); | ||
const suggestedTransformers: Transformer[] = []; | ||
|
||
const addSuggestions = (transformerValues: string[]) => { | ||
const newTransformers = TRANSFORMERS.filter( | ||
(t) => transformerValues.includes(t.value) && !currentTransformerValues.has(t.value) | ||
); | ||
|
||
suggestedTransformers.push(...newTransformers); | ||
}; | ||
|
||
if (isStepsEventsPattern(variableName)) { | ||
addSuggestions(['digest']); | ||
} | ||
|
||
if (isDateVariable(variableName)) { | ||
addSuggestions(['date']); | ||
} | ||
|
||
if (isNumberVariable(variableName)) { | ||
addSuggestions(['round', 'floor', 'ceil', 'abs', 'plus', 'minus', 'times', 'divided_by']); | ||
} | ||
|
||
if (isArrayVariable(variableName)) { | ||
addSuggestions(['first', 'last', 'join', 'map', 'where', 'size']); | ||
} | ||
|
||
if (isTextVariable(variableName)) { | ||
addSuggestions(['upcase', 'downcase', 'capitalize', 'truncate', 'truncatewords']); | ||
} | ||
|
||
return suggestedTransformers.length > 0 ? [{ label: 'Suggested', transformers: suggestedTransformers }] : []; | ||
}, [variableName, currentTransformers]); | ||
} | ||
|
||
function isDateVariable(name: string): boolean { | ||
const datePatterns = ['date', 'time', 'created', 'updated', 'timestamp', 'scheduled']; | ||
|
||
return datePatterns.some((pattern) => name.toLowerCase().includes(pattern)); | ||
} | ||
|
||
function isNumberVariable(name: string): boolean { | ||
const numberPatterns = ['count', 'amount', 'total', 'price', 'quantity', 'number', 'sum', 'age']; | ||
|
||
return numberPatterns.some((pattern) => name.toLowerCase().includes(pattern)); | ||
} | ||
|
||
function isArrayVariable(name: string): boolean { | ||
const arrayPatterns = ['list', 'array', 'items', 'collection', 'set', 'group', 'events']; | ||
|
||
return arrayPatterns.some((pattern) => name.toLowerCase().includes(pattern)); | ||
} | ||
|
||
function isTextVariable(name: string): boolean { | ||
const textPatterns = ['name', 'title', 'description', 'text', 'message', 'content', 'label']; | ||
|
||
return textPatterns.some((pattern) => name.toLowerCase().includes(pattern)); | ||
} | ||
|
||
function isStepsEventsPattern(name: string): boolean { | ||
return /^steps\..*\.events$/.test(name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
type NestedObject = Record<string, unknown>; | ||
|
||
function getNestedValue(obj: NestedObject, path: string): string { | ||
const value = path.split('.').reduce((current: unknown, key) => { | ||
if (current && typeof current === 'object') { | ||
return (current as Record<string, unknown>)[key]; | ||
} | ||
|
||
return undefined; | ||
}, obj); | ||
|
||
if (value === null || value === undefined) return ''; | ||
if (typeof value === 'string') return value; | ||
if (typeof value === 'number' || typeof value === 'boolean') return String(value); | ||
if (typeof value === 'object') { | ||
const stringified = JSON.stringify(value); | ||
|
||
return stringified === '{}' ? '' : stringified; | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
/** | ||
* Format a list of items for digest notifications with configurable behavior | ||
* Default formatting: | ||
* - 1 item: "John" | ||
* - 2 items: "John and Josh" | ||
* - 3 items: "John, Josh and Sarah" | ||
* - 4+ items: "John, Josh and 2 others" | ||
* | ||
* @param array The array of items to format | ||
* @param maxNames Maximum names to show before using "others" | ||
* @param keyPath Path to extract from objects (e.g., "name" or "profile.name") | ||
* @param separator Custom separator between names (default: ", ") | ||
* @returns Formatted string | ||
* | ||
* Examples: | ||
* {{ actors | digest }} => "John, Josh and 2 others" | ||
* {{ actors | digest: 2 }} => "John, Josh and 3 others" | ||
* {{ users | digest: 2, "name" }} => For array of {name: string} | ||
* {{ users | digest: 2, "profile.name", "•" }} => "John • Josh and 3 others" | ||
*/ | ||
export function digest(array: unknown, maxNames = 2, keyPath?: string, separator = ', '): string { | ||
if (!Array.isArray(array) || array.length === 0) return ''; | ||
|
||
const values = keyPath | ||
? array.map((item) => { | ||
if (typeof item !== 'object' || !item) return ''; | ||
|
||
return getNestedValue(item as NestedObject, keyPath); | ||
}) | ||
: array; | ||
|
||
if (values.length === 1) return values[0]; | ||
if (values.length === 2) return `${values[0]} and ${values[1]}`; | ||
|
||
if (values.length === 3 && maxNames >= 3) { | ||
return `${values[0]}, ${separator}${values[1]} and ${values[2]}`; | ||
} | ||
|
||
// Use "others" format for 4+ items or when maxNames is less than array length | ||
const shownItems = values.slice(0, maxNames); | ||
const othersCount = values.length - maxNames; | ||
|
||
return `${shownItems.join(separator)} and ${othersCount} ${othersCount === 1 ? 'other' : 'others'}`; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not sure if that's the best name 🤔