-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move useCodapState hook to context provider + create components for t…
…able cells instead of using mapping functions.
- Loading branch information
Showing
18 changed files
with
585 additions
and
367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React, { createContext, ReactNode, useContext, useEffect } from "react"; | ||
import { ICollection, ICollections, IDataSet } from "../types"; | ||
import { InteractiveState, useCodapState } from "../hooks/useCodapState"; | ||
|
||
export type CodapState = { | ||
init: () => Promise<void>, | ||
handleSelectSelf: () => Promise<void>, | ||
dataSets: IDataSet[], | ||
selectedDataSet: any, | ||
collections: ICollections, | ||
handleSetCollections: (collections: ICollections) => void, | ||
handleSelectDataSet: (name: string) => void, | ||
getCollectionNameFromId: (id: number) => string | undefined, | ||
handleUpdateInteractiveState: (update: Partial<InteractiveState>) => void, | ||
interactiveState: InteractiveState, | ||
cases: any[], | ||
connected: boolean, | ||
handleUpdateAttributePosition: (coll: ICollection, attrName: string, position: number) => Promise<void>, | ||
handleAddCollection: (newCollectionName: string) => Promise<void>, | ||
handleSortAttribute: (context: string, attrId: number, isDescending: boolean) => Promise<void>, | ||
handleAddAttribute: (collection: ICollection, attrName: string) => Promise<void>, | ||
updateTitle: (title: string) => Promise<void>, | ||
selectCODAPCases: (caseIds: number[]) => Promise<void>, | ||
listenForSelectionChanges: (callback: (notification: any) => void) => void, | ||
handleCreateCollectionFromAttribute: (collection: ICollection, attr: any, parent: number|string) => Promise<void>, | ||
handleUpdateCollections: () => Promise<void> | ||
}; | ||
|
||
const CodapContext = createContext({} as CodapState); | ||
|
||
|
||
export const useCodapContext = () => { | ||
return useContext(CodapContext); | ||
}; | ||
|
||
export const CodapProvider = ({ children }: { children: ReactNode }) => { | ||
const codapState = useCodapState(); // Now useCodapState is called here | ||
|
||
useEffect(() => { | ||
codapState.init(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
if (!codapState.connected) { | ||
return <div>Loading...</div>; // Handle loading state in the provider itself | ||
} | ||
|
||
return ( | ||
<CodapContext.Provider value={codapState}> | ||
{children} | ||
</CodapContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React, { createContext, useContext, useRef, useState } from "react"; | ||
|
||
type FocusedCellInfo = { | ||
caseId: string; | ||
attrTitle: string; | ||
}; | ||
|
||
interface FocusContextValue { | ||
inputRefs: React.MutableRefObject<Record<string, HTMLInputElement | null>>; | ||
focusedCell: FocusedCellInfo|null; | ||
setFocusedCell: (cellInfo: FocusedCellInfo) => void; | ||
} | ||
|
||
const defaultContextValue: FocusContextValue = { | ||
inputRefs: { current: {} }, | ||
focusedCell: null, | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
setFocusedCell: (cellInfo: FocusedCellInfo) => {}, | ||
}; | ||
|
||
// Create a context for managing focus | ||
const FocusContext = createContext(defaultContextValue); | ||
|
||
export const useFocusContext = () => { | ||
return useContext(FocusContext); | ||
}; | ||
|
||
export const FocusProvider = ({children}: {children: React.ReactNode}) => { | ||
const inputRefs = useRef<Record<string, HTMLInputElement | null>>({}); | ||
const [focusedCell, setFocusedCell] = useState<FocusedCellInfo | null>(null); | ||
|
||
return ( | ||
<FocusContext.Provider value={{ inputRefs, focusedCell, setFocusedCell }}> | ||
{children} | ||
</FocusContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.