Skip to content

Commit

Permalink
refactor(interface): separate main suggestion, rename suggestion list
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxuum committed Jan 9, 2024
1 parent 267163d commit c146a1f
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 113 deletions.
114 changes: 114 additions & 0 deletions src/client/interface/components/terminal/suggestion/MainSuggestion.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { BindingOrValue } from "@rbxts/pretty-react-hooks";
import Roact, { Binding } from "@rbxts/roact";
import { fonts } from "../../../constants/fonts";
import { palette } from "../../../constants/palette";
import { useRem } from "../../../hooks/useRem";
import { ArgumentSuggestion, Suggestion } from "../../../types";
import { Frame } from "../../interface/Frame";
import { Group } from "../../interface/Group";
import { Padding } from "../../interface/Padding";
import { Text } from "../../interface/Text";
import { Badge } from "./Badge";
import { SuggestionSizes } from "./types";
import { highlightMatching } from "./util";

export interface MainSuggestionProps {
suggestion?: Suggestion;
argument: BindingOrValue<boolean>;
currentText?: string;
size: BindingOrValue<UDim2>;
sizes: Binding<SuggestionSizes>;
}

export function MainSuggestion({
suggestion,
currentText,
size,
sizes,
argument,
}: MainSuggestionProps) {
const rem = useRem();

return (
<Frame
size={size}
backgroundColor={palette.crust}
cornerRadius={new UDim(0, rem(0.5))}
clipsDescendants={true}
>
<Padding key="padding" all={new UDim(0, rem(1))} />

<Group
key="badges"
anchorPoint={new Vector2(1, 0)}
size={sizes.map((val) =>
UDim2.fromOffset(math.max(val.typeBadgeWidth, rem(7)), rem(4.5)),
)}
position={UDim2.fromScale(1, 0)}
visible={argument}
>
<Badge
key="optional-badge"
size={new UDim2(1, 0, 0, rem(2))}
color={
argument &&
suggestion !== undefined &&
(suggestion.main as ArgumentSuggestion).optional
? palette.blue
: palette.red
}
text={
argument &&
suggestion !== undefined &&
(suggestion.main as ArgumentSuggestion).optional
? "Optional"
: "Required"
}
textColor={palette.white}
textSize={rem(1.5)}
/>

<Badge
key="type-badge"
size={new UDim2(1, 0, 0, rem(2))}
position={UDim2.fromOffset(0, rem(2.5))}
color={palette.surface0}
text={
argument && suggestion !== undefined
? (suggestion.main as ArgumentSuggestion).dataType
: ""
}
textColor={palette.white}
textSize={rem(1.5)}
/>
</Group>

<Text
key="title"
size={sizes.map((val) => val.title)}
text={
argument
? suggestion?.main.title
: highlightMatching(suggestion?.main.title, currentText)
}
textSize={rem(2)}
textColor={palette.white}
textXAlignment="Left"
richText={true}
font={fonts.inter.bold}
/>

<Text
key="description"
size={sizes.map((val) => val.description)}
position={UDim2.fromOffset(0, rem(2))}
text={suggestion?.main.description ?? ""}
textSize={rem(1.5)}
textColor={palette.white}
textXAlignment="Left"
textWrapped={true}
richText={true}
/>
</Frame>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,18 @@ import { useMotion } from "../../../hooks/useMotion";
import { useRem } from "../../../hooks/useRem";
import { SuggestionContext } from "../../../providers/suggestionProvider";
import { selectText } from "../../../store/app";
import { ArgumentSuggestion } from "../../../types";
import { toHex } from "../../../util/color";
import { Frame } from "../../interface/Frame";
import { Group } from "../../interface/Group";
import { Padding } from "../../interface/Padding";
import { Text } from "../../interface/Text";
import { Badge } from "./Badge";
import { MainSuggestion } from "./MainSuggestion";
import { SuggestionSizes } from "./types";
import { highlightMatching } from "./util";

export interface SuggestionListProps {
position?: UDim2;
}

const HIGHLIGHT_PREFIX = `<font color="${toHex(palette.blue)}">`;

function highlight(text?: string, terminalText?: string) {
if (text === undefined) return "";
if (terminalText === undefined) return text;

const subText = text.sub(0, terminalText.size());
if (terminalText.lower() !== subText.lower()) {
return text;
}

const unhighlightedText = text.sub(terminalText.size() + 1);
return `${HIGHLIGHT_PREFIX}${subText}</font>${unhighlightedText}`;
}

export function SuggestionList({ position }: SuggestionListProps) {
const rem = useRem();

Expand All @@ -62,14 +47,9 @@ export function SuggestionList({ position }: SuggestionListProps) {

// Suggestions
const suggestion = useContext(SuggestionContext).suggestion;
const isArgument = useMemo(
() => suggestion?.main.type === "argument",
[suggestion],
);

const [sizes, setSizes] = useBinding({
titleText: UDim2.fromOffset(rem(16), rem(2)),
descriptionText: UDim2.fromOffset(rem(16), rem(2)),
const [sizes, setSizes] = useBinding<SuggestionSizes>({
title: UDim2.fromOffset(rem(16), rem(2)),
description: UDim2.fromOffset(rem(16), rem(2)),
typeBadgeWidth: rem(6),
});

Expand Down Expand Up @@ -127,11 +107,8 @@ export function SuggestionList({ position }: SuggestionListProps) {
}

setSizes({
titleText: UDim2.fromOffset(titleBounds.X, titleBounds.Y),
descriptionText: UDim2.fromOffset(
descriptionBounds.X,
descriptionBounds.Y,
),
title: UDim2.fromOffset(titleBounds.X, titleBounds.Y),
description: UDim2.fromOffset(descriptionBounds.X, descriptionBounds.Y),
typeBadgeWidth:
typeBadgeBounds !== undefined
? typeBadgeBounds.X + rem(2)
Expand Down Expand Up @@ -179,87 +156,14 @@ export function SuggestionList({ position }: SuggestionListProps) {
clipsDescendants={true}
visible={suggestion !== undefined}
>
<Frame
key="top"
<MainSuggestion
key="main"
suggestion={suggestion}
argument={suggestion?.main.type === "argument"}
currentText={currentTextPart}
size={suggestionSize}
backgroundColor={palette.crust}
cornerRadius={new UDim(0, rem(0.5))}
clipsDescendants={true}
>
<Padding key="padding" all={new UDim(0, rem(1))} />

<Group
key="badges"
anchorPoint={new Vector2(1, 0)}
size={sizes.map((val) =>
UDim2.fromOffset(math.max(val.typeBadgeWidth, rem(7)), rem(4.5)),
)}
position={UDim2.fromScale(1, 0)}
visible={isArgument}
>
<Badge
key="optional-badge"
size={new UDim2(1, 0, 0, rem(2))}
color={
isArgument &&
suggestion !== undefined &&
(suggestion.main as ArgumentSuggestion).optional
? palette.blue
: palette.red
}
text={
isArgument &&
suggestion !== undefined &&
(suggestion.main as ArgumentSuggestion).optional
? "Optional"
: "Required"
}
textColor={palette.white}
textSize={rem(1.5)}
/>

<Badge
key="type-badge"
size={new UDim2(1, 0, 0, rem(2))}
position={UDim2.fromOffset(0, rem(2.5))}
color={palette.surface0}
text={
isArgument && suggestion !== undefined
? (suggestion.main as ArgumentSuggestion).dataType
: ""
}
textColor={palette.white}
textSize={rem(1.5)}
/>
</Group>

<Text
key="title"
size={sizes.map((val) => val.titleText)}
text={
isArgument
? suggestion?.main.title
: highlight(suggestion?.main.title, currentTextPart)
}
textSize={rem(2)}
textColor={palette.white}
textXAlignment="Left"
richText={true}
font={fonts.inter.bold}
/>

<Text
key="description"
size={sizes.map((val) => val.descriptionText)}
position={UDim2.fromOffset(0, rem(2))}
text={suggestion?.main.description ?? ""}
textSize={rem(1.5)}
textColor={palette.white}
textXAlignment="Left"
textWrapped={true}
richText={true}
/>
</Frame>
sizes={sizes}
/>

<Group key="other" size={otherSuggestionSize}>
<uilistlayout
Expand All @@ -282,7 +186,7 @@ export function SuggestionList({ position }: SuggestionListProps) {
<Text
key="text"
size={new UDim2(1, 0, 1, 0)}
text={highlight(name, currentTextPart)}
text={highlightMatching(name, currentTextPart)}
textColor={palette.white}
textSize={rem(1.6)}
textXAlignment="Left"
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./SuggestionList";
export * from "./Suggestions";
5 changes: 5 additions & 0 deletions src/client/interface/components/terminal/suggestion/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface SuggestionSizes {
title: UDim2;
description: UDim2;
typeBadgeWidth: number;
}
17 changes: 17 additions & 0 deletions src/client/interface/components/terminal/suggestion/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { palette } from "../../../constants/palette";
import { toHex } from "../../../util/color";

const HIGHLIGHT_PREFIX = `<font color="${toHex(palette.blue)}">`;

export function highlightMatching(text?: string, terminalText?: string) {
if (text === undefined) return "";
if (terminalText === undefined) return text;

const subText = text.sub(0, terminalText.size());
if (terminalText.lower() !== subText.lower()) {
return text;
}

const unhighlightedText = text.sub(terminalText.size() + 1);
return `${HIGHLIGHT_PREFIX}${subText}</font>${unhighlightedText}`;
}

0 comments on commit c146a1f

Please sign in to comment.