Something went wrong. Try again.
opencode v2 mirror
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109export const SESSION_OPEN_FILE_TAB = "open-file"export const SESSION_BROWSER_TAB = "browser"export const SESSION_BTW_TAB = "btw"export const sessionBrowserTab = (tabID: string) => `${SESSION_BROWSER_TAB}:${tabID}`export const isSessionBrowserTab = (tab: string | undefined) => !!tab && (tab === SESSION_BROWSER_TAB || tab.startsWith(`${SESSION_BROWSER_TAB}:`))
export type SessionTabs = { active?: string all: string[]}
export type SessionTabState = { tabs: SessionTabs preview?: string}
const sessionTabPreview = (current: SessionTabState) => current.preview ?? (current.tabs.all.includes(SESSION_OPEN_FILE_TAB) ? SESSION_OPEN_FILE_TAB : undefined)
export function previewSessionTab(current: SessionTabState, tab: string): SessionTabState { const preview = sessionTabPreview(current) const previewIndex = preview ? current.tabs.all.indexOf(preview) : -1 const existingIndex = current.tabs.all.indexOf(tab)
if (existingIndex !== -1) { if (previewIndex === -1 || preview === tab) { return { tabs: { all: current.tabs.all, active: tab }, preview: preview === tab ? tab : undefined } } return { tabs: { all: current.tabs.all.filter((item) => item !== preview), active: tab }, } }
if (previewIndex === -1) { return { tabs: { all: [...current.tabs.all, tab], active: tab }, preview: tab } }
return { tabs: { all: current.tabs.all.map((item, index) => (index === previewIndex ? tab : item)), active: tab, }, preview: tab, }}
export function openSessionTab(current: SessionTabState, tab: string): SessionTabState { const preview = sessionTabPreview(current) if (tab === "review") { return { tabs: { all: current.tabs.all.filter((item) => item !== tab), active: tab }, preview, } }
if (tab === "context") { return { tabs: { all: [tab, ...current.tabs.all.filter((item) => item !== tab)], active: tab }, preview, } }
const previewIndex = preview ? current.tabs.all.indexOf(preview) : -1 const existingIndex = current.tabs.all.indexOf(tab) if (existingIndex !== -1) { if (previewIndex === -1 || preview === tab) { return { tabs: { all: current.tabs.all, active: tab } } } return { tabs: { all: current.tabs.all.filter((item) => item !== preview), active: tab }, } }
if (previewIndex === -1) { return { tabs: { all: [...current.tabs.all, tab], active: tab } } }
return { tabs: { all: current.tabs.all.map((item, index) => (index === previewIndex ? tab : item)), active: tab, }, }}
export function closeSessionTab(current: SessionTabState, tab: string): SessionTabState { if (tab === "review") { if (current.tabs.active !== tab) return current return { tabs: { all: current.tabs.all, active: current.tabs.all[0] }, preview: current.preview, } }
const all = current.tabs.all.filter((item) => item !== tab) const preview = current.preview === tab ? undefined : current.preview if (current.tabs.active !== tab) return { tabs: { ...current.tabs, all }, preview }
const index = current.tabs.all.indexOf(tab) return { tabs: { all, active: current.tabs.all[index - 1] ?? current.tabs.all[index + 1] ?? all[0], }, preview, }}