Skip to content

Commit 43ca42b

Browse files
authored
update to start node process with metro, changes to persisting (#25)
* update to start node process with metro, changes to persisting * metro changes --------- Signed-off-by: Steven Conner <steven.c.conner@gmail.com>
1 parent 7263835 commit 43ca42b

File tree

11 files changed

+37
-21
lines changed

11 files changed

+37
-21
lines changed

app/app.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,8 @@ if (__DEV__) {
2929
function App(): React.JSX.Element {
3030
const { colors } = useTheme()
3131
const { toggleSidebar } = useSidebar()
32-
const [activeItem, setActiveItem] = useGlobal<MenuItemId>("sidebar-active-item", "logs", {
33-
persist: true,
34-
})
35-
const [, setTimelineItems] = withGlobal<TimelineItem[]>("timelineItems", [], {
36-
persist: true,
37-
})
32+
const [activeItem, setActiveItem] = useGlobal<MenuItemId>("sidebar-active-item", "logs")
33+
const [, setTimelineItems] = withGlobal<TimelineItem[]>("timelineItems", [])
3834

3935
const menuConfig = useMemo(
4036
() => ({

app/components/ClearLogsButton.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ export function ClearLogsButton() {
1212
})
1313

1414
// Using withGlobal so we don't rerender when the logs change
15-
const [_timelineItems, setTimelineItems] = withGlobal<TimelineItem[]>("timelineItems", [], {
16-
persist: true,
17-
})
15+
const [_timelineItems, setTimelineItems] = withGlobal<TimelineItem[]>("timelineItems", [])
1816
const [activeClientId] = useGlobal("activeClientId", "")
1917
const clearLogs = useCallback(() => {
2018
setTimelineItems((prev) => prev.filter((item) => item.clientId !== activeClientId))

app/components/ClientTab.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function ClientTab({ clientId }: { clientId: string }) {
1010
const label: string = clientData?.name ?? clientId
1111
const platformVersion: string = clientData?.platformVersion ?? ""
1212

13-
const [activeTab, setActiveTab] = useGlobal(tabgroup, label, { persist: true })
13+
const [activeTab, setActiveTab] = useGlobal(tabgroup, label)
1414
const active = activeTab === clientId
1515

1616
const getOsLabel = (os: string) => {

app/components/ExpandableRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function ExpandableRow({
3535
responseStatusCode,
3636
children,
3737
}: ExpandableRowProps) {
38-
const [isOpen, setIsOpen] = useGlobal(`timeline-${id}-open`, false, { persist: true })
38+
const [isOpen, setIsOpen] = useGlobal(`timeline-${id}-open`, false)
3939
const time = formatTime(date)
4040

4141
return (

app/components/Tab.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { useGlobal } from "../state/useGlobal"
1010
* with useGlobal/withGlobal to retrieve which tab is currently active.
1111
*/
1212
export function Tab({ id, label, tabgroup }: { id: string; label: string; tabgroup: string }) {
13-
const [activeTab, setActiveTab] = useGlobal(tabgroup, label, { persist: true })
13+
const [activeTab, setActiveTab] = useGlobal(tabgroup, label)
1414
const active = activeTab === label
1515

1616
return (

app/screens/StateScreen.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function StateScreen() {
1515
const [stateSubscriptionsByClientId, setStateSubscriptionsByClientId] = useGlobal<{
1616
[clientId: string]: StateSubscription[]
1717
}>("stateSubscriptionsByClientId", {})
18-
const [activeTab] = useGlobal("activeClientId", "")
18+
const [activeTab, setActiveTab] = useGlobal("activeClientId", "")
1919

2020
const clientStateSubscriptions = stateSubscriptionsByClientId[activeTab] || []
2121

@@ -64,6 +64,7 @@ export function StateScreen() {
6464
[activeTab]: [],
6565
}))
6666
sendToCore("state.values.subscribe", { paths: [], clientId: activeTab })
67+
setActiveTab("")
6768
}}
6869
>
6970
<Text>Clear State</Text>

app/state/connectToServer.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getUUID } from "../utils/random/getUUID"
2+
import { deleteGlobal, withGlobal } from "./useGlobal"
23
import { CommandType } from "reactotron-core-contract"
34
import type { StateSubscription, TimelineItem } from "../types"
4-
import { withGlobal } from "./useGlobal"
55
import { isSafeKey, sanitizeValue } from "../utils/sanitize"
66

77
type UnsubscribeFn = () => void
@@ -33,9 +33,7 @@ export function connectToServer(props: { port: number } = { port: 9292 }): Unsub
3333
const [_e, setError] = withGlobal<Error | null>("error", null)
3434
const [clientIds, setClientIds] = withGlobal<string[]>("clientIds", [])
3535
const [, setActiveClientId] = withGlobal("activeClientId", "")
36-
const [_timelineItems, setTimelineItems] = withGlobal<TimelineItem[]>("timelineItems", [], {
37-
persist: true,
38-
})
36+
const [_timelineItems, setTimelineItems] = withGlobal<TimelineItem[]>("timelineItems", [])
3937
const [_stateSubscriptionsByClientId, setStateSubscriptionsByClientId] = withGlobal<{
4038
[clientId: string]: StateSubscription[]
4139
}>("stateSubscriptionsByClientId", {})
@@ -77,13 +75,22 @@ export function connectToServer(props: { port: number } = { port: 9292 }): Unsub
7775
}
7876

7977
if (data.type === "connectedClients") {
78+
let newestClientId = ""
8079
data.clients.forEach((client: any) => {
8180
// Store the client data in global state
8281
const clientId = client.clientId
8382
const [_, setClientData] = withGlobal(`client-${clientId}`, {})
8483
setClientData(client)
84+
if (!clientIds.includes(clientId)) {
85+
newestClientId = clientId
86+
}
8587
})
8688
setClientIds(data.clients.map((client: any) => client.clientId))
89+
90+
if (newestClientId) {
91+
// Set the active client to the newest client
92+
setActiveClientId(newestClientId)
93+
}
8794
}
8895

8996
if (data.type === "command" && data.cmd) {
@@ -109,7 +116,6 @@ export function connectToServer(props: { port: number } = { port: 9292 }): Unsub
109116
console.tron.log("unknown command", data.cmd)
110117
}
111118
if (data.cmd.type === CommandType.StateValuesChange) {
112-
console.log("state.values.change", data.cmd)
113119
data.cmd.payload.changes.forEach((change: StateSubscription) => {
114120
if (!isSafeKey(data.cmd.clientId) || !isSafeKey(change.path)) {
115121
console.warn(
@@ -155,8 +161,17 @@ export function connectToServer(props: { port: number } = { port: 9292 }): Unsub
155161
// Clean up after disconnect
156162
ws.socket.onclose = () => {
157163
console.tron.log("Reactotron server disconnected")
158-
setIsConnected(false)
164+
// Clear individual client data
165+
clientIds.forEach((clientId) => {
166+
const [_, setClientData] = withGlobal(`client-${clientId}`, {})
167+
setClientData({})
168+
})
169+
deleteGlobal("clientIds")
159170
setClientIds([])
171+
setIsConnected(false)
172+
setActiveClientId("")
173+
setTimelineItems([])
174+
setStateSubscriptionsByClientId({})
160175
}
161176

162177
// Send a message to the server (which will be forwarded to the client)

app/state/useGlobal.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ function buildSetValue<T>(id: string, persist: boolean) {
112112
}
113113
}
114114

115+
export function deleteGlobal(id: string): void {
116+
delete globals[id]
117+
}
118+
115119
/**
116120
* Clear all globals and reset the storage entirely.
117121
* Optionally rerender all components that use useGlobal.

app/utils/useSelectedTimelineItems.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { TimelineItem } from "../types"
1515
*/
1616
export const useSelectedTimelineItems = () => {
1717
const [selectedItemId, setSelectedItemId] = useGlobal<string | null>("selectedTimelineItem", null)
18-
const [timelineItems] = useGlobal<TimelineItem[]>("timelineItems", [], { persist: true })
18+
const [timelineItems] = useGlobal<TimelineItem[]>("timelineItems", [])
1919

2020
const selectedItem = selectedItemId
2121
? timelineItems.find((item) => item.id === selectedItemId) || null

app/utils/useTimeline.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { normalize } from "./normalize"
66
import { safeTime } from "./safeTime"
77

88
export function useTimeline(filters: TimelineFilters): TimelineItem[] {
9-
const [items] = useGlobal<TimelineItem[]>("timelineItems", [], { persist: true })
9+
const [items] = useGlobal<TimelineItem[]>("timelineItems", [])
1010
const [search] = useGlobal("search", "")
1111

1212
return useMemo(() => {

0 commit comments

Comments
 (0)