gitlab-org--gitlab-foss/app/assets/javascripts/notes/stores/utils.js

45 lines
1.5 KiB
JavaScript
Raw Normal View History

import AjaxCache from '~/lib/utils/ajax_cache';
import { trimFirstCharOfLineContent } from '~/diffs/store/utils';
import { sprintf, __ } from '~/locale';
import createGqClient, { fetchPolicies } from '~/lib/graphql';
2019-05-06 16:06:00 -04:00
// factory function because global flag makes RegExp stateful
const createQuickActionsRegex = () => /^\/\w+.*$/gm;
2018-08-29 07:37:12 -04:00
export const findNoteObjectById = (notes, id) => notes.filter(n => n.id === id)[0];
2018-03-16 16:16:21 -04:00
export const getQuickActionText = note => {
let text = __('Applying command');
2018-08-29 07:37:12 -04:00
const quickActions = AjaxCache.get(gl.GfmAutoComplete.dataSources.commands) || [];
2018-03-16 16:16:21 -04:00
const executedCommands = quickActions.filter(command => {
const commandRegex = new RegExp(`/${command.name}`);
return commandRegex.test(note);
});
if (executedCommands && executedCommands.length) {
if (executedCommands.length > 1) {
text = __('Applying multiple commands');
} else {
const commandDescription = executedCommands[0].description.toLowerCase();
2019-08-05 10:31:40 -04:00
text = sprintf(__('Applying command to %{commandDescription}'), { commandDescription });
}
}
return text;
};
2019-05-06 16:06:00 -04:00
export const hasQuickActions = note => createQuickActionsRegex().test(note);
2019-05-06 16:06:00 -04:00
export const stripQuickActions = note => note.replace(createQuickActionsRegex(), '').trim();
export const prepareDiffLines = diffLines =>
diffLines.map(line => ({ ...trimFirstCharOfLineContent(line) }));
export const gqClient = createGqClient(
{},
{
fetchPolicy: fetchPolicies.NO_CACHE,
},
);