Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | 1x 1x 1x 1x 1x 3x 3x 3x 2x 2x 2x 2x 2x 3x 3x 1x 3x 3x 1x 1x 3x 3x 1x 9x 9x 9x 9x 2x 2x 2x 2x 1x 1x 2x 2x 9x 4x 4x 3x 9x 9x 1x 4x 4x 4x 4x 4x 4x 4x 4x 1x 53x 53x 53x 53x 9x 9x 9x 9x 53x 53x 53x 1x 140x 140x 140x 140x 140x 140x 140x 1x 127x 127x 127x 127x 1x 59x 59x 59x 59x 59x 59x 68x 2x 66x 59x 59x 1x 6x 6x 6x 6x 4x 4x 4x 4x 4x 4x 4x 4x 4x 6x | // Types
import {
ActionGroup,
ActionItemType,
ActionKind,
ActivityType,
} from '@shared/types';
// Stores
import { useSetChatResetFlag } from '@/stores';
// Constants
import {
HOME_NAVIGATION_SUGGESTION,
MARKDOWN_HASH_LINK_REGEX,
ROUTES,
SEARCH_NAVIGATION_SUGGESTIONS,
} from '@/constants';
import { CHATBOT_MESSAGES, UserRole } from '@shared/constants';
interface HandleActionClickParams {
item: ActionItemType;
onClose?: () => void;
}
export type ChatMessage = {
id?: string;
role?: string;
content?: string;
};
export const stripJsonSuffix = (content?: string) => {
if (!content) return '';
const trimmedEnd = content.trimEnd();
if (!trimmedEnd || trimmedEnd.startsWith('```')) return content;
const jsonStartIndex = Math.max(
trimmedEnd.lastIndexOf('{'),
trimmedEnd.lastIndexOf('['),
);
if (jsonStartIndex < 0) return content;
try {
const parsed = JSON.parse(trimmedEnd.slice(jsonStartIndex));
if (parsed !== null && typeof parsed === 'object') {
return trimmedEnd.slice(0, jsonStartIndex).trimEnd();
}
} catch {
return content;
}
return content;
};
const formalizeMsgContent = (content: string, actionLabel: string): string => {
const sanitized = content.replace(MARKDOWN_HASH_LINK_REGEX, '');
if (content.includes('```ts') && actionLabel) {
return CHATBOT_MESSAGES.SEARCH_SUCCESSFULLY(actionLabel);
}
return sanitized || CHATBOT_MESSAGES.SEARCH_SUCCESSFULLY(actionLabel);
};
export const getMessageDisplayContent = (
msg: ChatMessage,
actionLabel: string,
): string | null => {
if (msg.role === 'tool') {
try {
const parsed = JSON.parse(msg.content ?? '');
return (
(typeof parsed?.validation === 'string' && parsed.validation) ||
(typeof parsed?.success === 'string' && parsed.success) ||
null
);
} catch {
return null;
}
}
if (msg.role === UserRole.USER || !msg.content) {
return null;
}
const stripped = stripJsonSuffix(msg.content);
return stripped ? formalizeMsgContent(stripped, actionLabel) : null;
};
/**
* Determines if the search dialog should be closed based on the action item
*
* @param item The action item that was clicked
* @returns boolean indicating whether the dialog should be closed
*/
export const shouldCloseDialog = (item: ActionItemType): boolean => {
const closableTypes = [
ActivityType.VIEW_MENU,
ActivityType.VIEW_STATISTIC,
ActivityType.COMPARE_STATISTIC,
ActivityType.VIEW_FEEDBACK,
];
return closableTypes.includes(item.type);
};
/**
* Groups options by their group property
* @param options Array of action items to be grouped
* @returns Object with group names as keys and arrays of matching options as values
*/
export const groupOptionsByGroup = (
options: ActionItemType[],
): Record<string, ActionItemType[]> => {
return options.reduce(
(acc, option) => {
const groupKey = String(option.group);
(acc[groupKey] ??= []).push(option);
return acc;
},
{} as Record<string, ActionItemType[]>,
);
};
/**
* Determines the redirect path for an action item based on keywords in its label
*
* @param item The action item containing label and payload information
* @returns The matching route path if keywords are found in the label
*/
export const getRedirectPath = (item: ActionItemType) => {
const redirectMap: Partial<Record<ActionGroup, string>> = {
[ActionGroup.MENU]: ROUTES.HOME,
[ActionGroup.STATISTIC]: ROUTES.STATISTICS,
[ActionGroup.FEEDBACK]: ROUTES.FEEDBACK,
};
return redirectMap[item.group] ?? '/';
};
// Remove trailing slashes from paths for comparison
const normalizePath = (path?: string): string => {
if (!path) return ROUTES.HOME;
const trimmed = path.replace(/\/+$/, '');
return trimmed || ROUTES.HOME;
};
// Replace the current page's suggestion with the fallback shortcut
export const getContextualNavigationSuggestions = (
pathname?: string,
baseSuggestions: ActionItemType[] = SEARCH_NAVIGATION_SUGGESTIONS,
homeSuggestion: ActionItemType = HOME_NAVIGATION_SUGGESTION,
): ActionItemType[] => {
const currentPath = normalizePath(pathname);
return baseSuggestions.map((item) =>
normalizePath(getRedirectPath(item)) === currentPath
? homeSuggestion
: item,
);
};
export const handleActionClick = async ({
item,
onClose,
}: HandleActionClickParams) => {
// For REDIRECT actions from keyboard, trigger click to let Link handle navigation
if (item.kind === ActionKind.REDIRECT) {
const pathname = getRedirectPath(item) || '';
// Reset chat before navigation
useSetChatResetFlag(true);
// Normalize to absolute URL
const absoluteUrl = new URL(pathname, window.location.origin).toString();
window.location.href = absoluteUrl;
if (shouldCloseDialog(item)) {
onClose?.();
}
return;
}
};
|