Skip to content

Commit af2852b

Browse files
committed
refactor: improve type handling and update function parameters across multiple components + satisfy linter
1 parent 08f2ae9 commit af2852b

10 files changed

Lines changed: 39 additions & 49 deletions

File tree

packages/edit-table/features/table/Table.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ export const Table = () => {
3333
<MaterialReactTable
3434
tableInstanceRef={tableInstanceRef}
3535

36-
//@ts-ignore
3736
columns={columns}
3837
data={tableData}
39-
state={tableState}
38+
state={tableState as any}
4039

4140
enableColumnResizing
4241
enablePinning

packages/edit-table/features/table/schema2columns.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test('single object + prefix', () => {
3131
}
3232
}
3333
}
34-
expect(schema2columns( schema, 'name' )).toStrictEqual(
34+
expect(schema2columns( schema, undefined, 'name' )).toStrictEqual(
3535
[
3636
{
3737
"accessorKey": "name.firstName",

packages/edit-table/features/table/schema2columns.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ function property2column({key, subJsonSchema, prefix, uiSchema}: {key: string, s
4646
}
4747

4848
export function schema2columns(schema: JsonSchema, uiSchema?: UISchemaElement, prefix='') {
49-
console.log({schema, uiSchema})
5049
switch(schema.type) {
5150
case "object":
5251
return Object.entries(schema.properties||{})

packages/edit-timeline/features/timeline/Timeline.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import {
1010
DataGroupCollectionType,
1111
DateType,
1212
IdType,
13-
Timeline as TimelineType,
1413
TimelineItem,
1514
TimelineOptions,
1615
TimelineOptionsItemCallbackFunction
17-
} from "vis-timeline/types";
16+
} from "vis-timeline/declarations";
1817
import { useAppSelector, useAppDispatch } from "state";
1918
import { AppDispatch } from "state";
2019
import { selectData, setCellData, setRowSelection, Row } from "state";
@@ -85,7 +84,7 @@ export function Timeline() {
8584
(event) => event && event.start
8685
) /** entries with missing start are not valid **/
8786
.map(event => event as Item),
88-
[tableData]
87+
[tableData, eventFromRow]
8988
);
9089
console.log({tableData, mapping, items})
9190

@@ -103,7 +102,7 @@ export function Timeline() {
103102

104103
const onMove: TimelineOptionsItemCallbackFunction = useCallback(
105104
(item: TimelineItem, callback: any) => handleMove(dispatch, item as Item, callback),
106-
[dispatch]
105+
[dispatch, handleMove]
107106
);
108107
const onSelect = useCallback(
109108
(selectionArgs: { items: number[] }) =>

packages/edit-timeline/features/timeline/visTimelineWrapper/VisTimelineWrapper.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import {
66
DataGroupCollectionType,
77
DateType,
88
IdType,
9-
Timeline as TimelineType,
109
TimelineItem,
1110
TimelineOptions,
12-
} from "vis-timeline/types";
11+
} from "vis-timeline/declarations";
1312
import "vis-timeline/styles/vis-timeline-graph2d.css";
1413

1514
import type {
15+
DataItem,
1616
TimelineAnimationOptions,
1717
TimelineEvents,
18-
} from "vis-timeline/types";
18+
} from "vis-timeline/declarations";
1919

2020
export type TimelineEventsWithMissing =
2121
| TimelineEvents
@@ -107,13 +107,15 @@ const VisTimelineWrapper = ({
107107
}: Props) => {
108108
const timelineContainerRef: RefObject<HTMLDivElement> =
109109
useRef<HTMLDivElement>(null);
110-
const timelineDatasetRef = useRef<DataSet<TimelineItem> | null>();
110+
const timelineDatasetRef = useRef<DataSet<DataItem, "id"> | null>(null);
111111
const timelineGroupsRef = useRef<DataGroupCollectionType>();
112-
const timelineRef = useRef<TimelineType | null>(null);
112+
const timelineRef = useRef<InstanceType<typeof TimelineConstructor> | null>(
113+
null
114+
);
113115
const prevItems = useRef<TimelineItem[] | null>(null);
114116

115117
useEffect(() => {
116-
timelineDatasetRef.current = new DataSet();
118+
timelineDatasetRef.current = new DataSet<DataItem, "id">();
117119
timelineRef.current = new TimelineConstructor(
118120
timelineContainerRef.current!,
119121
timelineDatasetRef.current,
@@ -144,7 +146,7 @@ const VisTimelineWrapper = ({
144146
if (!timelineDatasetRef.current || !timelineRef.current) return;
145147

146148
let itemsLength = timelineDatasetRef.current.get().length
147-
timelineDatasetRef.current.update(items);
149+
timelineDatasetRef.current.update(items as DataItem[]);
148150
if(items.length !== itemsLength) {
149151
timelineRef.current.fit();
150152
}

packages/example/features/app/Debug.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function Print({json={}}: {json: any}) {
1111
<h4>{key}</h4>
1212
<p>
1313
{ JSON.stringify(data, undefined, indentation).split('\n').map(
14-
line => <>{ line.split(indentation).map( token => <span>{token} &nbsp; </span> ) }<br/></>
14+
line => <React.Fragment key={line}>{ line.split(indentation).map( token => <span key={token}>{token} &nbsp; </span> ) }<br/></React.Fragment>
1515
)
1616
}
1717
</p>

packages/import/features/Import.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { useEffect } from 'react';
22

33
import { useAppSelector, useAppDispatch, setRowData } from 'state';
4-
import { selectJsonSchema, selectCryptedData, setCryptedData, selectUiSchema, setJsonSchema, setUiSchema, CryptedData } from 'project-state';
4+
import { selectJsonSchema, selectCryptedData, setCryptedData, selectUiSchema, setJsonSchema, setUiSchema } from 'project-state';
55
import { PGPProvider, useDecryptUsingContext } from 'pgp-provider';
66
import { api } from '@formswizard/api';
77

88
function useFormId() {
9-
const hash = typeof location != 'undefined' ? location.hash.slice(1) : '';
10-
const hashParameters = !hash ? {} : Object.fromEntries(new URLSearchParams(hash) as any);
11-
const { formId } = hashParameters;
12-
return formId
9+
if (typeof location === 'undefined' || !location.hash) {
10+
return undefined;
11+
}
12+
13+
const hash = location.hash.startsWith('#') ? location.hash.slice(1) : location.hash;
14+
const formId = new URLSearchParams(hash).get('formId');
15+
return formId ?? undefined;
1316
}
1417

1518
export function DecryptAndImportLastNewSubmission() {
@@ -19,13 +22,14 @@ export function DecryptAndImportLastNewSubmission() {
1922
const cryptedData = useAppSelector(selectCryptedData);
2023
useEffect( () => {
2124
async function loadCryptedData() {
25+
if (!formId) return;
2226
const { cryptedData } = await api.getProjectStateCryptedData(formId);
2327
cryptedData?.map( cryptedDatum => {
2428
dispatch(setCryptedData(cryptedDatum)); // TODO use a setter for all cryptedData at the same time
2529
});
2630
}
2731
cryptedData.length || loadCryptedData()
28-
}, [cryptedData])
32+
}, [cryptedData, dispatch, formId])
2933

3034
const decrypt = useDecryptUsingContext();
3135
useEffect( () => {
@@ -36,7 +40,7 @@ export function DecryptAndImportLastNewSubmission() {
3640
const row = { ...decrypted, id, keyId, armoredPublicKey }
3741
decrypted && dispatch(setRowData({row}));
3842
})
39-
}, [cryptedData]);
43+
}, [cryptedData, dispatch, decrypt]);
4044

4145
return <></>
4246
}
@@ -49,13 +53,14 @@ export function useSchema() {
4953

5054
useEffect(() => {
5155
async function loadSchema() {
56+
if (!formId) return;
5257
const { schema } = await api.getProjectStateSchema(formId);
5358
const { jsonSchema, uiSchema } = schema || {};
5459
jsonSchema && dispatch(setJsonSchema(jsonSchema))
5560
jsonSchema && dispatch(setUiSchema(uiSchema))
5661
}
5762
jsonSchema || loadSchema()
58-
}, [jsonSchema, uiSchema, dispatch])
63+
}, [jsonSchema, uiSchema, dispatch, formId])
5964
}
6065

6166
export function Import() {

packages/layout/features/layout/Layout.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ export function Layout({children, title, drawer, drawerWidth='30%', tabs}: Props
9393

9494
<Box sx={{ borderBottom: 1, borderColor: 'divider' }}>
9595
<Tabs value={ tabIdx } /*onChange={handleChange}*/>
96-
{ tabs.map( (tab, idx) => { const onClick = useCallback( () => setTabIdx(idx), [idx] );
97-
return <Tab key={'tab'+idx} icon={tab.icon as any} label={tab.label} onClick={onClick} /> } ) }
96+
{ tabs.map( (tab, idx) =>
97+
<Tab key={'tab'+idx} icon={tab.icon as any} label={tab.label} onClick={() => setTabIdx(idx)} />
98+
) }
9899
</Tabs>
99100
</Box>
100101
{ tabContent }

packages/pgp-provider/PGPProvider.tsx

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function PGPProvider({generateKeyOptions={}, children}: {generateKeyOptio
6464
};
6565
};
6666
asyncEffect();
67-
}, [keyPair, setKeyPair])
67+
}, [keyPair, setKeyPair, generateKeyOptions, passphrase])
6868

6969
return (
7070
<PGPContext.Provider value={keyPair}>
@@ -83,7 +83,7 @@ export async function encrypt(text: string, armoredPubKeys?: string[], publicKey
8383
}
8484
}
8585

86-
export function encryptUsingContext(text: string, armoredPubKeys?: string[]) {
86+
export function useEncryptUsingContext(text: string, armoredPubKeys?: string[]) {
8787
const {publicKey} = useKeyContext(); // The users publicKey will used in addition to the optionally specified armoredPubKeys
8888
const [encrypted, setEncrypted] = useState<string>();
8989

@@ -93,7 +93,7 @@ export function encryptUsingContext(text: string, armoredPubKeys?: string[]) {
9393
setEncrypted(encrypted);
9494
};
9595
asyncEffect();
96-
}, [text, publicKey, setEncrypted]);
96+
}, [text, publicKey, armoredPubKeys, setEncrypted]);
9797

9898
return encrypted;
9999
};
@@ -106,27 +106,12 @@ export function useDecryptUsingContext() {
106106
const message = await readMessage({ armoredMessage });
107107
const decrypted = (await pgp_decrypt({ message, decryptionKeys })
108108
.catch(e => { console.error('Decryption failed. Do you have a correct privateKey?')
109-
console.warn(e) })
110-
)?.data.toString()
109+
console.warn(e)
110+
return undefined
111+
})
112+
)?.data?.toString()
111113
return decrypted
112114
}
113115
};
114116
return decrypt
115117
}
116-
117-
export function decryptUsingContext(armoredMessage?: string) {
118-
const decrypt = useDecryptUsingContext();
119-
const [decrypted, setDecrypted] = useState<string>();
120-
121-
useEffect(() => {
122-
const asyncEffect = async () => {
123-
if(armoredMessage) {
124-
const decrypted = await decrypt(armoredMessage);
125-
decrypted && setDecrypted(decrypted)
126-
}
127-
};
128-
asyncEffect();
129-
}, [armoredMessage, setDecrypted]);
130-
131-
return decrypted;
132-
};

packages/security-state/features/react/PresetSelector.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function PresetSelectorCard({preset, raised, index, attackVector}: {preset: Pres
1010
const onClick = useCallback( () => securityStateDispatch && securityStateDispatch({ attackVector,
1111
type: ACTIONS.selectPreset,
1212
payload: {index} }),
13-
[index, securityStateDispatch] );
13+
[index, securityStateDispatch, attackVector] );
1414
const Icon = preset._ui?.icon;
1515
return (
1616
<Card onClick={onClick}

0 commit comments

Comments
 (0)