Skip to content

Commit 3c4bdb9

Browse files
committed
feat: fix weighted problem in X + added char counter
1 parent 623b27e commit 3c4bdb9

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

apps/frontend/src/components/new-launch/editor.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const EditorWrapper: FC<{
5555
setGlobalValue,
5656
setInternalValue,
5757
internalFromAll,
58+
totalChars
5859
} = useLaunchStore(
5960
useShallow((state) => ({
6061
internal: state.internal.find((p) => p.integration.id === state.current),
@@ -75,6 +76,7 @@ export const EditorWrapper: FC<{
7576
deleteInternalValue: state.deleteInternalValue,
7677
setGlobalValue: state.setGlobalValue,
7778
setInternalValue: state.setInternalValue,
79+
totalChars: state.totalChars,
7880
}))
7981
);
8082

@@ -271,6 +273,7 @@ export const EditorWrapper: FC<{
271273
autoComplete={canEdit}
272274
validateChars={true}
273275
identifier={internalFromAll?.identifier || 'global'}
276+
totalChars={totalChars}
274277
/>
275278
</div>
276279
<div className="flex flex-col items-center gap-[10px]">
@@ -337,6 +340,7 @@ export const Editor: FC<{
337340
autoComplete?: boolean;
338341
validateChars?: boolean;
339342
identifier?: string;
343+
totalChars?: number;
340344
}> = (props) => {
341345
const {
342346
allValues,
@@ -447,6 +451,9 @@ export const Editor: FC<{
447451
/>
448452
)}
449453
</div>
454+
<div className={clsx("text-end text-sm mt-1", props.value.length > props.totalChars && "!text-red-500")}>
455+
{props.value.length}/{props.totalChars}
456+
</div>
450457
</>
451458
);
452459
};

apps/frontend/src/components/new-launch/manage.modal.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,16 @@ export const ManageModal: FC<AddEditModalProps> = (props) => {
176176

177177
const sliceNeeded = checkAllValid.filter((p: any) => {
178178
return p.values.some((a: any) => {
179-
return (
180-
countCharacters(a.content, p?.integration?.identifier || '') >
181-
(p.maximumCharacters || 1000000)
179+
const weightedLength = countCharacters(
180+
a.content,
181+
p?.integration?.identifier || ''
182182
);
183+
const totalCharacters =
184+
weightedLength > a.content.length
185+
? weightedLength
186+
: a.content.length;
187+
188+
return totalCharacters > (p.maximumCharacters || 1000000);
183189
});
184190
});
185191

apps/frontend/src/components/new-launch/providers/high.order.provider.tsx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React, {
44
FC,
55
forwardRef,
66
useCallback,
7+
useEffect,
78
useImperativeHandle,
89
useMemo,
910
} from 'react';
@@ -59,6 +60,8 @@ export const withProvider = function <T extends object>(
5960
isGlobal,
6061
tab,
6162
setTab,
63+
setTotalChars,
64+
justCurrent,
6265
} = useLaunchStore(
6366
useShallow((state) => ({
6467
date: state.date,
@@ -67,15 +70,39 @@ export const withProvider = function <T extends object>(
6770
global: state.global,
6871
internal: state.internal.find((p) => p.integration.id === props.id),
6972
integrations: state.selectedIntegrations,
73+
justCurrent: state.current,
7074
current: state.current === props.id,
7175
isGlobal: state.current === 'global',
7276
setCurrent: state.setCurrent,
77+
setTotalChars: state.setTotalChars,
7378
selectedIntegration: state.selectedIntegrations.find(
7479
(p) => p.integration.id === props.id
7580
),
7681
}))
7782
);
7883

84+
useEffect(() => {
85+
if (!setTotalChars) {
86+
return;
87+
}
88+
89+
if (isGlobal) {
90+
setTotalChars(0);
91+
}
92+
93+
if (current) {
94+
setTotalChars(
95+
typeof maximumCharacters === 'number'
96+
? maximumCharacters
97+
: maximumCharacters(
98+
JSON.parse(
99+
selectedIntegration.integration.additionalSettings || '[]'
100+
)
101+
)
102+
);
103+
}
104+
}, [justCurrent, current, isGlobal, setTotalChars]);
105+
79106
const getInternalPlugs = useCallback(async () => {
80107
return (
81108
await fetch(
@@ -207,7 +234,12 @@ export const withProvider = function <T extends object>(
207234

208235
{(tab === 0 || !SettingsComponent) &&
209236
!value?.[0]?.content?.length && (
210-
<div>{t('start_writing_your_post', 'Start writing your post for a preview')}</div>
237+
<div>
238+
{t(
239+
'start_writing_your_post',
240+
'Start writing your post for a preview'
241+
)}
242+
</div>
211243
)}
212244
{(tab === 0 || !SettingsComponent) &&
213245
!!value?.[0]?.content?.length &&

apps/frontend/src/components/new-launch/store.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface StoreState {
2727
date: dayjs.Dayjs;
2828
repeater?: number;
2929
isCreateSet: boolean;
30+
totalChars: number;
3031
tags: { label: string; value: string }[];
3132
tab: 0 | 1;
3233
current: string;
@@ -100,11 +101,13 @@ interface StoreState {
100101
setRepeater: (repeater: number) => void;
101102
setTags: (tags: { label: string; value: string }[]) => void;
102103
setIsCreateSet: (isCreateSet: boolean) => void;
104+
setTotalChars?: (totalChars: number) => void;
103105
}
104106

105107
const initialState = {
106108
date: dayjs(),
107109
tags: [] as { label: string; value: string }[],
110+
totalChars: 0,
108111
tab: 0 as 0,
109112
isCreateSet: false,
110113
current: 'global',
@@ -445,4 +448,8 @@ export const useLaunchStore = create<StoreState>()((set) => ({
445448
: item
446449
),
447450
})),
451+
setTotalChars: (totalChars: number) =>
452+
set((state) => ({
453+
totalChars,
454+
})),
448455
}));

0 commit comments

Comments
 (0)