Wrap focusedGenerationId in a type

Summary:
- Use `FocusInfo` type instead of a raw string (I'll add another field to it in the next diff and it made sense to keep the changes separate).
- `userSelectedGenerationId` now has the type of `string | undefined` for consistency.

Reviewed By: fabiomassimo

Differential Revision: D23160820

fbshipit-source-id: f210d93b9ed7cb25eb3c2c8e6e6aadec4dc872d7
This commit is contained in:
Andrey Mishanin
2020-08-17 08:03:11 -07:00
committed by Facebook GitHub Bot
parent f626925443
commit 4fc63da386

View File

@@ -69,12 +69,16 @@ type Events = {
updateTreeGenerationChangesetGeneration: UpdateTreeGenerationChangesetGenerationPayload; updateTreeGenerationChangesetGeneration: UpdateTreeGenerationChangesetGenerationPayload;
}; };
type FocusInfo = {
generationId: string;
};
export function plugin(client: FlipperClient<Events, {}>) { export function plugin(client: FlipperClient<Events, {}>) {
const generations = createState<{[id: string]: TreeGeneration}>( const generations = createState<{[id: string]: TreeGeneration}>(
{}, {},
{persist: 'generations'}, {persist: 'generations'},
); );
const focusedGenerationId = createState<string | null>(null); const focusInfo = createState<FocusInfo | undefined>(undefined);
const recording = createState<boolean>(true); const recording = createState<boolean>(true);
client.onMessage('addEvent', (data) => { client.onMessage('addEvent', (data) => {
@@ -84,7 +88,9 @@ export function plugin(client: FlipperClient<Events, {}>) {
generations.update((draft) => { generations.update((draft) => {
draft[data.id] = {...data, changeSets: []}; draft[data.id] = {...data, changeSets: []};
}); });
focusedGenerationId.set(focusedGenerationId.get() || data.id); focusInfo.set({
generationId: focusInfo.get()?.generationId || data.id,
});
}); });
client.onMessage('updateTreeGenerationHierarchyGeneration', (data) => { client.onMessage('updateTreeGenerationHierarchyGeneration', (data) => {
generations.update((draft) => { generations.update((draft) => {
@@ -114,22 +120,28 @@ export function plugin(client: FlipperClient<Events, {}>) {
} }
function clear() { function clear() {
generations.set({}); generations.set({});
focusedGenerationId.set(null); focusInfo.set(undefined);
recording.set(true); recording.set(true);
} }
return {generations, focusedGenerationId, recording, setRecording, clear}; return {
generations,
focusInfo,
recording,
setRecording,
clear,
};
} }
export function Component() { export function Component() {
const instance = usePlugin(plugin); const instance = usePlugin(plugin);
const generations = useValue(instance.generations); const generations = useValue(instance.generations);
const focusedGenerationId = useValue(instance.focusedGenerationId); const focusInfo = useValue(instance.focusInfo);
const recording = useValue(instance.recording); const recording = useValue(instance.recording);
const [userSelectedGenerationId, setUserSelectedGenerationId] = useState< const [userSelectedGenerationId, setUserSelectedGenerationId] = useState<
string | null string | undefined
>(null); >();
const [searchString, setSearchString] = useState<string>(''); const [searchString, setSearchString] = useState<string>('');
const [focusedChangeSet, setFocusedChangeSet] = useState< const [focusedChangeSet, setFocusedChangeSet] = useState<
UpdateTreeGenerationChangesetApplicationPayload | null | undefined UpdateTreeGenerationChangesetApplicationPayload | null | undefined
@@ -137,12 +149,12 @@ export function Component() {
const [selectedTreeNode, setSelectedTreeNode] = useState<any>(); const [selectedTreeNode, setSelectedTreeNode] = useState<any>();
const focusedTreeGeneration: TreeGeneration | null = useMemo(() => { const focusedTreeGeneration: TreeGeneration | null = useMemo(() => {
const id = userSelectedGenerationId || focusedGenerationId; const id = userSelectedGenerationId || focusInfo?.generationId;
if (id === null) { if (id === undefined) {
return null; return null;
} }
return generations[id]; return generations[id];
}, [userSelectedGenerationId, focusedGenerationId, generations]); }, [userSelectedGenerationId, focusInfo, generations]);
const filteredGenerations: Array<TreeGeneration> = useMemo(() => { const filteredGenerations: Array<TreeGeneration> = useMemo(() => {
const generationValues = Object.values(generations); const generationValues = Object.values(generations);
if (searchString.length <= 0) { if (searchString.length <= 0) {
@@ -207,9 +219,9 @@ export function Component() {
<EventTable <EventTable
generations={filteredGenerations} generations={filteredGenerations}
focusedGenerationId={ focusedGenerationId={
userSelectedGenerationId || focusedGenerationId userSelectedGenerationId || focusInfo?.generationId
} }
onClick={(id: string | null) => { onClick={(id?: string) => {
setFocusedChangeSet(null); setFocusedChangeSet(null);
setUserSelectedGenerationId(id); setUserSelectedGenerationId(id);
setSelectedTreeNode(null); setSelectedTreeNode(null);
@@ -217,7 +229,7 @@ export function Component() {
/> />
</Sidebar> </Sidebar>
<Layout.Top> <Layout.Top>
<Sidebar position="top" minHeight={80} height={80}> <Sidebar position="top" minHeight={400} height={400}>
<TreeHierarchy <TreeHierarchy
generation={focusedTreeGeneration} generation={focusedTreeGeneration}
focusedChangeSet={focusedChangeSet} focusedChangeSet={focusedChangeSet}